0

I'm using jQuery Ajax to send parameters to a PHP script. Below is the Jquery ajax script

  jQuery
<script>
$(document).ready(function () {
    $("#builder_group").change(function () {
        var selected_builder = $(this).val();
        alert(selected_builder);
        $.ajax({
            type: 'POST',
            url: 'getGroupzCode.php',
            data: 'selected_builder',
            datatype: 'json',
            success: function (data) {
                // Call this function on success                
                console.log(data);
                var yourArray = JSON.parse(data);
                console.log(yourArray);
                $.each(yourArray, function (index, yourArray) {
                    $('#builder_group1').append($('<option/>', {
                        value: yourArray.id,
                        text: yourArray.name,
                    }));
                });
            },
            error: function () {
                displayDialogBox('Error', err.toString());
            }
        });
    });
});
</script>

When I see in firebug console I see the parametr passed is correct as selected but in the PHP script I see undefined index

PHP
    $builder_id=$_POST['selected_builder'];
    error_log($builder_id);
1
  • You are sending datatype as json so PHP expects to read json from data, which is merely text. Change accordingly. Either change datatype to text or change data to a json object Commented Jul 16, 2014 at 19:48

1 Answer 1

3
data: 'selected_builder',

That is not proper format. You need:

data: { selected_builder: selected_builder }

The below indicates you're receiving a json, is that correct? If so the parameter is "dataType" like below.

dataType: 'json',

If so you are you would use this in your php file:

$encoded = json_encode($yourvariable);
echo $encoded;

Now if this wasn't the case you would call the variable in php by:

$variable = $_POST["selected_builder"];
Sign up to request clarification or add additional context in comments.

1 Comment

But can refresh the dropdown everytime

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.