Pass a JavaScript array to PHP via Ajax
Solved
abirgl
Posted messages
147
Status
Member
-
Pitet Posted messages 2845 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:
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
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
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:
Have a nice day,
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,
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 ..."
:(
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
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.
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)
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.