Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

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!
?>
share|improve this question
    
And what is the problem.?Is there any error? – 웃웃웃웃웃 Dec 18 '13 at 8:55
    
When I try to access selected variable with var_dump ($_REQUEST['selected']); it return null – user3114378 Dec 18 '13 at 8:57
1  
Did you miss a "," after the "selectedValue"? – mrmoment Dec 18 '13 at 8:57

4 Answers 4

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);
    }
   });
});
share|improve this answer
    
Still can't access the selected variable in my PHP file. var_dump ($_REQUEST['selected']); //that print NULL don't know why! – user3114378 Dec 18 '13 at 9:02
    
Did you check output of console.log(selectedValue) – Nishu Tayal Dec 18 '13 at 9:09

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)

share|improve this answer
    
Is there any other solutions? – user3114378 Dec 18 '13 at 8:57
    
@user3114378 : refer the solution above in my answer. – Nishu Tayal Dec 18 '13 at 8:58
    
@ignacio, Can you please explain more? – user3114378 Dec 18 '13 at 9:05
    
What do you need to do with your select variable after post with Ajax to your php file? – Ignacio Ocampo Dec 18 '13 at 9:06

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.

share|improve this answer

$_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.

share|improve this answer
    
thanks but that doesn't print the value of the selected option too. echo $_POST["selected"]; is never set! – user3114378 Dec 18 '13 at 10:46
    
Can you please help : ( – user3114378 Dec 18 '13 at 10:48
    
again, data: {selected: selected}, – SparK Dec 18 '13 at 11:31
    
Thank you! I updated it. – smiggle Dec 18 '13 at 11:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.