0

How to pass javascript variable that came from select option to a PHP variable? I want to set PHP variable depending on user selection.
I tried that code:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

<script>
    $(function(){
        $("select[name='sex']").change(function () {
            var submitSearchData = jQuery('#extended-search').serialize();
            var selectedValue=$('#sex').val();
            jQuery.ajax({
                type: "POST",
                data: 'selected=' + selectedValue
                url: "ajax.php",
                success: function () {
                    // alert(submitSearchData);
                    alert(selectedValue);
                }
            });
        });
    });
</script>
<form id="extended-search" >
    <div class="input-container">
        <select class="select" name="sex" id="sex">
            <option value="0">All</option>
            <option value="1">M</option>
            <option value="2">F</option>
        </select>
    </div>
</form>
<?php
var_dump ($_REQUEST['selected']); //that print NULL don't know why!
?>
3
  • And what is the problem.?Is there any error? Commented Dec 18, 2013 at 8:55
  • When I try to access selected variable with var_dump ($_REQUEST['selected']); it return null Commented Dec 18, 2013 at 8:57
  • 1
    Did you miss a "," after the "selectedValue"? Commented Dec 18, 2013 at 8:57

4 Answers 4

1

You are passing data in wrong format. Data is passed as an object. Please refer below.

 $("select[name='sex']").change(function () {
    var submitSearchData = jQuery('#extended-search').serialize();
    var selectedValue=$('#sex').val();
   jQuery.ajax({
    type: "POST",
    data: {'selected': selectedValue},
    url: "ajax.php",
    success: function (response) {
    // alert(submitSearchData);
    alert(response);
    }
   });
});
Sign up to request clarification or add additional context in comments.

2 Comments

Still can't access the selected variable in my PHP file. var_dump ($_REQUEST['selected']); //that print NULL don't know why!
Did you check output of console.log(selectedValue)
0

Is not possible in the same instance of time:

yourfile -> ajax -> yourfile (here is the value, but you can't see this in your current webpage except that instead of ajax, send post form)

4 Comments

Is there any other solutions?
@user3114378 : refer the solution above in my answer.
@ignacio, Can you please explain more?
What do you need to do with your select variable after post with Ajax to your php file?
0

I hope this will help you...

dataType: "html",
data: {'selected': selectedValue},

and then u can get it via $_POST/$_REQUEST array since you have set your type to post.

Comments

0

$_REQUEST is null because it is not related to the ajax request you send. Try this for example:

<?php
    if (isset($_POST["selected"])) {
    echo $_POST["selected"];
} else {
?>
<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    </head>
    <body>
        <form id="extended-search">
            <div class="input-container">
                <select class="select" name="sex" id="sex">
                    <option value="0">All</option>
                    <option value="1">M</option>
                    <option value="2">F</option>
                </select>
            </div>
        </form>
        <script>
        $(function() {
            $("select[name='sex']").change(function () {
                var selected = $(this).val();
                $.ajax({
                    type: "POST",
                    data: {
                        selected: selected
                    },
                    url: "ajax.php",
                    success: function (data) {
                        alert(data);
                    }
                });
            });
        });
        </script>
    </body>
</html>

<?php } ?>

EDIT:

I updated the script and tested it. You had some errors in your code. Hope this works for you.

3 Comments

thanks but that doesn't print the value of the selected option too. echo $_POST["selected"]; is never set!
Can you please help : (
again, data: {selected: selected},

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.