Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End HTML Element from GET Request

  • This topic is empty.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #151224

    Hello everybody,

    I’m having some trouble with an Ajax request. I have to send a HTML Element to the script but when I try to access it, nothing is there. However there is no problem with the request itsself, because when I type some text instead of the HTML Element, everything is perfectly fine.

    What I want to do is really basic, but it just doesn’t work

    $.ajax({url: '/script.php?element='+document.getElementById('paragraph').innerHTML});
    
    $text = $_GET['element'];
    

    Can anyone help me please?

    And I know it isn’t safe to insert an HTML Element dynamically, but in the real script an authentication is done beforehand and everything is nicely escaped.

    #151252
    __
    Participant

    Couple things:

    1) You’re not “inserting an HTML element dynamically” – at least, not in the code you posted. If you are subsequently using user-provided values as HTML output, then, yes, you should be careful.

    2) The innerHTML you are submitting should be (probably “needs to be”) url-encoded before being sent. Further, I would suggest using the POST method instead of GET.

    3) If you are expecting the string you submit to be available to your PHP script (as implied by your $text = $_GET['element'] line), you need to understand how your script is going to execute – PHP and Javascript cannot interact directly, no matter what order the commands are written in.

    … PHP happens first, on your server. It sends its output to the browser and then shuts down.

    … Javascript (HTML, et.al.) happens later, on the user’s browser. It has no idea that any such thing as “PHP” even exists. When you make your ajax request, the current script execution (the one we’re discussing right now) will be long gone. /script.php will run again, under a new request.

    . – – – – – – – – – – – – – – – – – – – – – – – –

    Now, as for helping you out, we would need to know more about what you are trying to do.

    #151303

    First of all thank you for your post, traq.

    But most of what you posted is irrelevant, since I only posted the most basic version I could think of.

    1) Yes, in the script I posted I’m not. But I thought someone might think so and just wanted to prevent such comments.

    2) I already tried escaping the string, but that didn’t do anything. But I haven’t used the urlencode function so far, so I guess I’ll give that a try. But even if I manually enter the escaped text in the adress bar, it gets omitted somehow in the $_GET variable and that is my real problem here.

    3) Maybe I should have seperated the two code lines more obviously. They are not in the same script and I guess I’m quite familiar with how PHP and Javascript work, so I would suggest you to concentrate more on my problem here and not to try teaching me how the web works, thank you very much.

    My script is far more complicated when the two lines I posted, but I narrowed it down to them and I just don’t understand why it doesn’t work, since this should be really easy.

    #151304

    It worked with encodeURlComponent() now. I still don’t know, why escape wouldn’t do the job, but at least everything’s functioning right now.

    #151347
    __
    Participant

    most of what you posted is irrelevant

    (…oh, except for that one part, which turned out to be the exact solution to the problem.)

    I would suggest you to concentrate more on my problem here and not to try teaching me how the web works, thank you very much.

    The two are not mutually exclusive.

    Further, there was nothing in my post that was not intended to help solve your problem – the fact that I didn’t know specifically what part was giving you trouble notwithstanding. That’s why I asked for more information.

    I guess I’ll just let you alone next time.

    It worked with encodeURlComponent() now. I still don’t know, why escape wouldn’t do the job

    have a nice day

    #151350
    Alen
    Participant

    @traq dude don’t teach him how web works! Teach him internet forum etiquette.

Viewing 6 posts - 1 through 6 (of 6 total)
  • The forum ‘Back End’ is closed to new topics and replies.