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

Hi

I have a HTML form with selected option values. I am trying to get the currently selected item and store it inside a php variable to do something with it further down the page. I need to do all of this without refreshing/reloading the page (if possible).

<form method="post">
            <select name="MySelect">
                <option value="year 1">year 1</option>
                <option value="year 2">year 2</option>
                <option value="year 3">year 3</option>
            </select>
</form>

[php]

<?php if(!empty($_POST['MySelect'])) { $selected = $_POST['MySelect']; echo $selected; } [/php] To clarify the above "should" echo out the selected option everytime onchange of the selected option. Any help is appreciated, regards.

where is the code you’re calling when the onchange fires? or is that what you’re needing?

I think the OP is looking for more of JavaScript than PHP, for that will do want you want. An it is best to have a button or a submit button even if you are using JavaScript, for graceful degradation purposes.

Sorry you are right i missed of some code.

<form method="post">
      <select name="MySelect" onchange=run(sel)>
                <option value="year 1">year 1</option>
                <option value="year 2">year 2</option>
                <option value="year 3">year 3</option>
      </select>
</form>

[code]
function run(sel) {

var i = sel.selectedIndex;
if (i != -1) {
    $.ajax({
        type: "POST",
        url: "index.php",
        data: { MySelect: sel.options[i].text}

    }).done(function( msg ) {});
}

}[/code]

[php]

<?php if(!empty($_POST['MySelect'])) { $selected = $_POST['MySelect']; echo $selected; } ?>

[/php]

Here is the above code, though i am struggling to echo it out because i dont believe its being stored in the $selected" variable

Sponsor our Newsletter | Privacy Policy | Terms of Service