Forums

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

Home Forums Back End sending variable from JS to PHP

  • This topic is empty.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #154213
    Rohithzr
    Participant

    the problem i am facing is that i an loading server-side datatable with a tools option. in there is a <li> which onclick opens a popup.

    now every row in the table has a different ID and according to that ID i have to display data in the popup.

    Now the problem is that i am getting no where near to the method of doing so.

    tried <iframe> too but not working.

    CATCH: The page customerlist.php should not re;oad.

    #154219
    __
    Participant

    sending variable from JS to PHP

    This is a very common tripping point, so apologies if you already know what I am about to say. Many people don’t.

    It is not possible for javascript to directly interact with php.

    PHP happens first, on your server. At this point, there is no such thing as “javascript.” It’s nothing more than text, which the user’s browser will later run as javascript.

    When PHP is done, it sends the completed webpage to the browser, and then it STOPS.

    JavaScript happens next, on the user’s browser. At this point, there is no such thing as “PHP.” Like I said above, PHP stopped. It’s gone. Won’t come back.

    If you don’t want to reload the page, then you need to use ajax (xhr). It’s not too complex, but because of differences across browsers, it’s often easiest to use a library. jQuery, for example, has pretty nice ajax methods.

    Basically, what is going to happen is this:

    1 . Your PHP script writes your initial webpage, outputs it, and then goes away.

    2 . The browser displays the webpage and runs the javascript.

    3 . JavaScript decides it need some data (we’ll call it data).

    4 . Javascript creates uses ajax to make a new request to the server, without reloading the page.

    5 . On the server, a new PHP script runs, gets data, outputs it, and then goes away.

    6 . Javascript gets data back, and does whatever it needs to do.

    7 . Things go back to normal.

    The only alternative to ajax would be to include all of the data you could possibly need with the initial webpage (and just hide the data that’s not needed right away). If the amount of data is small enough, this might work just fine; but ajax is probably a better way to go.

    #154320
    Rohithzr
    Participant

    thanks, I guess i will first try the easy way, :) though i don’t know if it would work. after that i will go to ajax method. uptill now i have only used simple ajax (get http request), and i do not know how to receive data through jquery ajax.

    another reason is that the data i have to bring back is like one ID has one, two or three boxId then from each boxId i get back the details of the box.

    currently i am doing it by opening another page (a new small window looks like popup).

    Is there any way by which i can call an iframe inside a popup with the url apended with ?boxId=31 ….. because then i can re-design a small page that load in the popup.

    and if it will get the URL as ?boxId=xx then the php in that iframe will response correctly.

    when i tried the above method the url of iframe didnot change in … means the iframe data got saved.

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