0

I am struggling/trying since last 24 hours with a simple thing, which i am not able to understand why i am not able to access the PHP variable. I know am doing something wrong and i have no idea what's that..

window.alert("Variable" + <?php  echo $_POST; ?> );

Its giving me output as Function Array() {[native code]}, How can i print the values ? and i think the POST attribute is blank, Can anyone check ? Why POST variable is blank ?

I am sending data to the file via POST method as

<script type="text/javascript">
function callAjaxAddition() {
    arguments0 = {
        arg1: $("#exampleForm input[id='pac-input']").val(),
        arg2: ("#exampleForm input[id='pac-input']").val()
    };
    $.ajax({
        type: "POST",
        url: "processAjax.php",
        data: {
            arguments: arguments0
        },
        success: function(data) {
            $("#answer").html('<ul><li>' + data + '</li></ul>');
            send_apptn_req();
        }
    });
    return false;
}
</script>

and ProcessAjax.php file is

<?php $a=0;foreach($_POST['arguments'] as $v) $a= $v;echo $a;?>

Thanks in advance please..

Amul Bhatia
  • 154
  • 1
  • 11
  • You are missing a `$` when doing `arg2: ("#exampleForm input[id='pac-input']").val()`. It should be `$arg2: ("#exampleForm input[id='pac-input']").val()` – grim Oct 03 '15 at 03:56

2 Answers2

1

$_POST is an associative array of variables passed to the current script.

So you need to use print_r instead of echo .

window.alert("Variable" + <?php  print_r($_POST); ?> );
taka0
  • 27
  • 6
Al Amin Chayan
  • 2,460
  • 4
  • 23
  • 41
0

$_POST is an array so you should use print_r() or var_dump() instead of echo:

window.alert("Variable" + <?php  print_r($_POST); ?> );

If debugging the $_POST variable in Javascript is what you want to do I suggest you do this:

console.log(<?php echo json_encode($_POST); ?>);

And you'll see the content in your developer tools on your browser.

For reference you can look at the answers to this question.

EDIT:

<form method="POST">
  <input type="text" name="first"/>
  <input type="text" name="second" />
  <input type="submit" value="submit">
</form>

<?php if (isset($_POST)): ?>
  <script type="application/javascript">
    console.debug(<?php echo json_encode($_POST); ?>);
  </script>
<?php endif; ?>

EDIT 2: (after you updated your code)

Change the type option in the ajax jQuery function to method like so:

<script type="text/javascript">
function callAjaxAddition() {
    arguments0 = {
        arg1: $("#exampleForm input[id='pac-input']").val(),
        arg2: $("#exampleForm input[id='pac-input']").val()
    };
    $.ajax({
        method: "POST",
        url: "processAjax.php",
        data: {
            arguments: arguments0
        },
        success: function(data) {
            $("#answer").html('<ul><li>' + data + '</li></ul>');
            send_apptn_req();
        }
    });
    return false;
}
</script>

Also note that after the AJAX POST, your $_POST variable will contain the data object that you passed to the $.ajax function and therefore what you are passing:

 data: {
   arguments: {
    arg1: $("#exampleForm input[id='pac-input']").val(),
    arg2: ("#exampleForm input[id='pac-input']").val()
   }
 }

will translate into:

Array (
  'arguments' => Array (
    'arg1': 'value of arg1'
    'arg2': 'value of arg2'
  )
)

So the loop in processAjax.php is quite useless since you're not looping through the inside array.

Community
  • 1
  • 1
grim
  • 6,669
  • 11
  • 38
  • 57
  • 3
    You should not have to echo a print, print (and print_r) already prints a string – Jesse Oct 03 '15 at 02:53
  • @Jesse I totally agree, I'm going to edit to better reflect my intentions there (which was to have a visual representation of the $_POST) – grim Oct 03 '15 at 02:55
  • @grim - Console Output - [] only and window.alert is blank .. It think the variables are not going to POST.. Can you check why ?? – Amul Bhatia Oct 03 '15 at 03:02
  • @grim - Still the variable is blank ...Please check the video, i have uploaded here the error http://tinypic.com/r/2di5405/8 – Amul Bhatia Oct 03 '15 at 03:53
  • @user2921939 what do you get if you do `console.log(arguments0);` right before calling `$.ajax`? – grim Oct 03 '15 at 03:55
  • @user2921939 If `console.log(arguments0)` gives you `[]` then you are not properly fetching the values from the fields. Make sure that your jQuery selectors target the element you want `$("#exampleForm input[id='pac-input']")`. You are not providing your HTML so I cannot help you any further. – grim Oct 03 '15 at 04:06