Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Mar 2010
    Thanked 0 Times in 0 Posts

    JavaScript with PHP

    Hi Friends,

    I don't know if this request has to be posted on PHP forum or this forum is ok.

    I've drop down menu.

    <select name="sel_ship" id = "sel_ship" onchange="getShip();">
    <option value="1">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
    <option value="4">Four</option>
    <option value="5">Five</option>
    I needed to have selected option value in $_SESSION['shipid'] upon any change on drop down menu so i used JavaScript code.

    I wrote below in .php but its not working.

    function getShip() {
        var shipId;
        shipId = document.getElementById("sel_ship").value;
        <?php $_SESSION['shipid']=$_GET['shipId'];?>
    If i replace $_GET['shipId'] with some static value like "2", it works but not with $_GET[].
    JavaScript variable shipId is also working.

    I need help because already tried a lot but failed.

    Thanks in advance.


  2. #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Thanked 4,947 Times in 4,908 Posts
    You need to understand the fundamentals of client-server programming, especially as it applies to a browser-based client.

    *ANY* server-side code in your page (in your case, that means any PHP code) is execute LONG BEFORE the page is sent to the browser (well..."LONG" maybe be milliseconds, but that is forever for a computer).

    That means that, in your code
    function getShip() {
        var shipId;
        shipId = document.getElementById("sel_ship").value;
        <?php $_SESSION['shipid']=$_GET['shipId'];?>
    the PHP code set the SESSION value *BEFORE* the browser EVER SAW this page. It's already been done. Milliseconds or seconds or even minutes ago.

    NOTHING AT ALL is happening in PHP when the <select> is changed!!!

    *ONLY* when the ENTIRE <FORM> is sent back to the server will PHP get a chance to see the <select> value.



    Unless you use AJAX (or AJAX-like coding) to send the data back to a *separate* PHP page as soon as the selection is made.

    Yes, you *can* do that, but I question what the point of it is.

    So you change the session value when the user changes the <select>. And then a moment later he changes his mind, chooses another <option>, and you have to send another AJAX request to change the session value.

    But what's the point??? NO OTHER USER can see that session value. And the only time your PHP code is going to be able to use the session value (presumably) is on the next PHP page that the user asks for.

    So why not just change the session value only when the <form> is submitted???
    Be yourself. No one else is as qualified.


Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts