0

I'm storing an array and sending this via ajax...

var heart = [31,32,33,34,35,36,37,38,39,42,43];
// Sending this data via ajax to php file/ 
 $.ajax({
 type: "POST",
 data:{ 'system': heart }, 
 url: "login-function.php",
 success: function(msg){
 alert('yes');
}
});

I believe its sending correctly because i'm getting a alert saying yes. This is my php file:

$system = $_POST['system'];
echo $system;

Am I storing this into a variable right ? How can i echo this value onto the page?

3
  • php.net/manual/en/function.var-dump.php Commented Jan 14, 2016 at 13:24
  • if( isset( $_POST['system'] ) ){$system = $_POST['system'];} use check it using isset if it is available . Commented Jan 14, 2016 at 13:24
  • use print_r() or var_dump() because it is array instead of echo Commented Jan 14, 2016 at 13:27

3 Answers 3

1

You have 2 issues. A: you are not using the ajax return value, in your code msg - this contains the output from php, so to display it, add it to the page somehow:

html:

<div id="result"></div>

js:

 var heart = [31,32,33,34,35,36,37,38,39,42,43];
 // Sending this data via ajax to php file/ 
 $.ajax({
     type: "POST",
     data:{ 'system': heart }, 
     url: "login-function.php",
     success: function(msg){
         $('#result').html(msg);
    },
     error: function(jqXHR, textStatus, errorThrown){
         console.log(errorThrown);
    }
});

B: You cant just echo an array, its not a string. to display ist contents you can use a number of php functions, for example var_dump():

$system = $_POST['system'];
var_dump($system);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your response, its very strange because i'm not getting alert messages. Its just for testing purposes so displaying an array in a alert box would do the job
@Ryan there isnt an alert() call in my code, so im not sure where you are going wrong. If you have an error in your php, the server may send a non 200 status code, in which case the success function will not get called. Ill amend my answer to cover that
See amended code - look in your js console for any output indicating a server error
0

you can use print_r( $system ); or use a foreach statement:

foreach ( $system as $itm ) {
    echo $itm . '<br />';
}

edit: echoing in PHP is only available for strings, intergers etc; not arrays or objects.

Comments

0
$system = $_POST['system'];
print_r($system);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.