Problem:
A Javascript array cannot be sent to a PHP script using POST. The POST request fails to pass the array data.
Resolution:
The issue stems from a misunderstanding of the AJAX workflow. While jQuery simplifies the process, it does not automate it entirely. To send an array to PHP and display the output:
<code class="html"><script> $(document).ready(function(){ $('#btn').click(function(){ var txt=$('#txt').val(); if(txt == '') alert("Enter some text"); else{ $.post('catcher.php', {'text': txt}, function(data) { $('#response').text(data.message); }, 'json'); } }); }); </script> ...</code>
<code class="php">if(!empty($_POST)){ $output['message'] = "Success!"; echo json_encode($output); }</code>
Explanation:
By following this approach, you can successfully send and process Javascript arrays via POST requests and receive the results in your PHP script.
The above is the detailed content of Why Isn\'t My JavaScript Array Sending to PHP via POST?. For more information, please follow other related articles on the PHP Chinese website!