Pass a JavaScript array to PHP via Ajax

Solved
abirgl Posted messages 147 Status Member -  
Pitet Posted messages 2845 Status Member -
Hello,
I need to pass a JavaScript array to PHP, so I tried using AJAX but it displays an error message "Undefined index: tab in ...", here is the code from my testajax7.php file:

<script type="text/javascript">
var tab=['a','b','c','d'];
</script>
<script type="text/javascript">
$.ajax({
type: "POST",
url: "testajax7.php",
data: { tab : tab},
success: function() {
alert("Success");
}
});
</script>

<?php
$myTable = $_POST['tab'];
print_r($myTable);
?>

I set testajax7.php in the url: of the AJAX function because I need the array on the same page (all processing is done on the same page)
Please, how can I modify this code to retrieve the array in PHP?
Thank you in advance

Configuration: Windows / Chrome 55.0.2883.87

1 answer

Pitet Posted messages 2845 Status Member 530
 
Hello,

Your code works, but on the first page load, the variable $_POST['tab'] does not exist yet (it is created later by the ajax request), hence the error message.
You need to check for the existence of this variable with isset or empty.

By the way:
- in HTML5, it's not necessary to specify the type attribute for JS code in script tags
- there's no need to close and reopen script tags if there's nothing in between
- you can target the current page using the # symbol

Example of correction:
 <?php if (isset($_POST['tab'])) { $myTable = $_POST['tab']; print_r($myTable); } else { ?> <script> var tab=['a','b','c','d']; $.ajax({ type: "POST", url: "#", data: { tab : tab}, success: function(data) { alert(data); } }); </script> <?php } ?> 


Have a nice day,
0
abirgl Posted messages 147 Status Member 1
 
Hi Pitet,
thank you for the comments ^^ ,
I tested the code you posted but it didn't display anything, I pressed F12 to see what shows up in the console and I found the error: "Uncaught ReferenceError: $ is not defined at ..."
:(
0
Pitet Posted messages 2845 Status Member 530
 
Yes, the $.ajax function is part of jQuery.
Since you didn't specify it in your first code, I thought you had already included it.
Otherwise:
 <?php if (isset($_POST['tab'])) { $myTable = $_POST['tab']; print_r($myTable); } else { ?> <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script> <script> var tab=['a','b','c','d']; $.ajax({ type: "POST", url: "#", data: { tab : tab}, success: function(data) { alert(data); } }); </script> <?php } ?> 

PS: without the
"quot;"
0
abirgl Posted messages 147 Status Member 1 > Pitet Posted messages 2845 Status Member
 
Thank you very much Pitet, it displays the table in alert, but for the processing I am going to do in PHP, I need a PHP array. I tried using AJAX to store the content of the JavaScript table in a PHP array, is that feasible? And how is it done please? Thank you very much.
0
Pitet Posted messages 2845 Status Member 530
 
And what about the $_POST['tab'] array?
The array displayed in the alert comes from the PHP array display. If you don't need to retrieve the result of your PHP processing via JavaScript, then the AJAX request is unnecessary and you could simply use a form.
0
abirgl Posted messages 147 Status Member 1
 
Hello Pitet,
I believe I didn't explain my need clearly, so I'll try to clarify:
I have a JavaScript array (it is not the result of a PHP process, it's just a simple JavaScript array)

var tab=['a','b','c','d'];

Now, I want to use this array for a PHP process, but I haven't found out how to pass it from JavaScript to PHP. I was told that AJAX is the solution, so I’m trying to send my JavaScript array via AJAX to finally retrieve a PHP array.
The PHP array I need should look like this:
$tab=array('a','b','c','d');
Is it possible to do this with AJAX or should I look for another way to achieve this?
PS: All processing must occur on the same page (the AJAX function URL must remain #)
Thank you so much for your answers and your efforts.
0