Results 1 to 3 of 3

Thread: Get Selected Option Value and Insert To PHP Variable - With/Without refreshing.

  1. #1
    Join Date
    Jul 2014

    Get Selected Option Value and Insert To PHP Variable - With/Without refreshing.


    I am trying to get the selected option value and insert into a php variable, without refreshing the page or submitting a button, but only by the on-change event.

    Here is what i have done so far after some research though struggling to get it to work.

    HTML Code:
    <select id="dropdown1" class="select" tabindex="2" onchange="run(this)">
        <option value="volvo">Volvo</option>
        <option value="saab">Saab</option>
        <option value="mercedes">Mercedes</option>
        <option value="audi">Audi</option>
    function run(sel) {
        var i = sel.selectedIndex;
        if (i != -1) {
            //document.getElementById("car").value = sel.options[i].text;
                type: "POST",
                url: "home.php",
                data: { car: sel.options[i].text}
            }).done(function( msg ) {});
    PHP Code:
    $_SESSION['car'] = $_POST["car"];
    $myCar $_SESSION['car'] ;

    Again just to clarify the user will select an option from the drop down it will be stored in a php variable to use later down the page for something else. This needs to be done without page refreshing (if possible). If possible provide solution with and without page refresh.

    Any help is appreciated,


  2. #2
    Join Date
    Sep 2008
    Akron, OH
    I *think* you'll need to create a PHP function that will store a session variable. So you'll use JavaScript to listen for the change, then using AJAX, you'd send the information to the PHP function to create the session variable. I'm not sure if that's the best way or not, but it should work!
    I'm always up for networking with fellow web professionals. Connect with me on LinkedIn if you like!

  3. #3
    Join Date
    Jul 2013
    Voorheesville NY USA
    Very confusing post.

    1 - You apparently want to use JS to grab a just-changed dropdown.
    2 - Then you want to set a php variable. That's kind of hard to do since you have no immediate access to php from js
    3 - You then show us some ajax code that is going to pass your new option to a php script.
    4 - Your script then returns(?) a value for something on your current web page? Your JS can then update that html value with the returned value.

    Now that I've laid out your apparent activities your post makes sense, although the nomenclature is still incorrect.

    Is this a correct visualization of your intent?
    PS - If you're posting here you should be using:

    ini_set('display_errors', '1');

    at the top of ALL php code while you develop it!

Thread Information

Users Browsing this Thread

There are currently 2 users browsing this thread. (0 members and 2 guests)

Posting Permissions

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