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,

Norman Wei
  • 51
  • 5
  • Are you getting `why` everytime? Have you looked at the request being sent in the network tab of your developer console/firebug? – chris85 Apr 28 '15 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. – Sean Apr 28 '15 at 23:57
  • @chris85 yup I'm getting "why" everytime – Norman Wei Apr 29 '15 at 00: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, http://stackoverflow.com/questions/1820927/request-monitoring-in-chrome. – chris85 Apr 29 '15 at 00:03
  • @sean tried your fix but didn't seem to affect things one way or the other – Norman Wei Apr 29 '15 at 00:03
  • there is code missing. what is the usage of var $selectedContexts = []; you declare it but never use it. Also what is addedContext? where is it declared? Probably what you have to change is this data: { addedContext: hideField} –  Apr 29 '15 at 00:04
  • data: {'addedContext' : 'YourContext'} – zalex Apr 29 '15 at 00:06
  • @PeterDarmis still does not affect any change. =/ – Norman Wei Apr 29 '15 at 00:08
  • What does the request show? Your not giving enough information to help you. – chris85 Apr 29 '15 at 00:08

1 Answers1

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';
}

?>