0

I believe I have the right syntax but am missing something important. Been searching on here for a while but can't figure out why the POST variable is not being detected. Basically my .ajax is firing because my test statement has been changing due to the value but some reason can't receive variable via $_POST (i.e. my echo in php echo that it is not firing) Also the native file and php that I am sending it to are the same file blankFormTemplate.php but don't think that should be an issue.

$(document).ready(function()
{
    var $selectedContexts = [];

    $('.allContextField').change(function(){
        //alert($(this).val());
        hideField = $(this).val();
        $('#'+hideField).remove();

        $.ajax
        ({
            type: "POST",
            url: "blankFormTemplate.php",
            data: addedContext=hideField,
            cache: false,
            success: function(addedContext)
            {
                $('#test').html($('#test').html()+hideField);
            } 
        });
    });
});

in my PHP blankFormTemplate.php:

<?php

if(isset($_POST['addedContext']))
{
    echo 'hello';
}
else
{
    echo 'why';
}

?>

Any help would be greatly appreciated.

Thanks,

9
  • Are you getting why everytime? Have you looked at the request being sent in the network tab of your developer console/firebug? Commented Apr 28, 2015 at 23:55
  • 1
    change data: addedContext=hideField, to data: 'addedContext='+hideField,. you need to set data to a sting, but you are just doing an assignment. Commented Apr 28, 2015 at 23:57
  • @chris85 yup I'm getting "why" everytime Commented Apr 29, 2015 at 0:02
  • See the @Sean comment. Also print_r($_POST); might be useful in the future to see what is being sent, or inspecting the request in the developer console, stackoverflow.com/questions/1820927/…. Commented Apr 29, 2015 at 0:03
  • @sean tried your fix but didn't seem to affect things one way or the other Commented Apr 29, 2015 at 0:03

1 Answer 1

0

Your javascript

$(document).ready(function()
{

    $('.allContextField').change(function(){
        hideField = $(this).val();
        $('#'+hideField).remove();
});
if (hideField.trim()) {
 $.ajax
        ({
            type: "POST",
            url: "blankFormTemplate.php",
            data: (addedContext:hideField},
            cache: false,
            success: function(msg)
            {
                $('#test').html(msg);
            } 
        });
}  
});

Your PHP

<?php

if(isset($_POST['addedContext']))
{
    echo 'hello';
}
else
{
    echo 'why';
}

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

Comments

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.