www.webdeveloper.com
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
    Posts
    1

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

    Hi,

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


    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,

    regards.

  2. #2
    Join Date
    Sep 2008
    Location
    Akron, OH
    Posts
    1,143
    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!

  3. #3
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    859
    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?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 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



Recent Articles