1

I have looked for 2 days to find out how to send a variable with jquery to php. I have searched for over 100 posts on stackoverflow, i tried multiple examples. But i just get it done. So i really hope someone over here would be kind enough to help me find out what i am doing wrong....

<?php
print_r(json_decode($_POST['country']));
?>
<html>
<head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
    <script type="text/javascript">

        $(function() {
            $('#country').change(function() {

                var country = $('#country').val();

                $.ajax({
                    type: 'POST',
                    data: country,
                    dataType: 'json',
                    url: 'timezone/timezone.php',
                })

                alert("Country: " + country);

            });
        });

    </script>

</head>

<body>

    <form method = "post" name="form1">
        <select name="country" class="country" id="country">
            <option value="NL">Nederland</option>
            <option value="BE">Belgie</option>
        </select>
    </form>

</body>

</html>

1 Answer 1

2

Data either needs to be a query string, or an object with a key and value.

As an object

           $.ajax({
                type: 'POST',
                data: { 'country': country }
                dataType: 'json',
                url: 'timezone/timezone.php', 
                success: function( data ) {
                   var element = data.wrap('<div />');  // wrap the response in a div 
                   $('body').append( element );   // append it to the body
                }
            }); 

as a query string

            $.ajax({
                type: 'POST',
                data: 'country='+country, 
                dataType: 'json',
                url: 'timezone/timezone.php'
            }); 

Whenever in doubt always go to the jquery docs. Here is an excerpt from the ajax documentation.

The data option can contain either a query string of the form key1=value1&key2=value2, or a map of the form {key1: 'value1', key2: 'value2'}. If the latter form is used, the data is converted into a query string using jQuery.param() before it is sent. This processing can be circumvented by setting processData to false. The processing might be undesirable if you wish to send an XML object to the server; in this case, change the contentType option from application/x-www-form-urlencoded to a more appropriate MIME type.

Sign up to request clarification or add additional context in comments.

13 Comments

I tried both, but still something is not right. Could it be, that it has something to do with php?
I Just noticed you're json decoding <?php print_r(json_decode($_POST['country'])); ?> You shouldn't have to do that, you can just echo $_POST['country'], try that and see the result.
So you're posting country to the same script ? In firebug go to your Net tab and see the request and response from the server, is it a 200 ok ?
Yes, it says its 200kb. What does this mean? That jquery has sended the string successfully? But somehow php fails?
You know what? I think this is working, your page will NOT refresh because this is an AJAX request, hence you will not see the echo $_POST['country']; I will edit the answer to inject the response from the server into a div so you can see what the server is sending back. Let me know what happens.
|

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.