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..

1
  • You are missing a $ when doing arg2: ("#exampleForm input[id='pac-input']").val(). It should be $arg2: ("#exampleForm input[id='pac-input']").val() Commented Oct 3, 2015 at 3:56

2 Answers 2

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); ?> );
Sign up to request clarification or add additional context in comments.

Comments

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.

6 Comments

You should not have to echo a print, print (and print_r) already prints a string
@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 - Console Output - [] only and window.alert is blank .. It think the variables are not going to POST.. Can you check why ??
@grim - Still the variable is blank ...Please check the video, i have uploaded here the error tinypic.com/r/2di5405/8
@user2921939 what do you get if you do console.log(arguments0); right before calling $.ajax?
|

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.