Serializing and Submitting a Form with jQuery and PHP
Problem:
A serialized form using jQuery fails to send data to a PHP script. The server receives no data.
HTML Form:
`
`JavaScript:
<code class="javascript">$("#contactForm").submit(function() { $.post("getcontact.php", $("#contactForm").serialize()) .done(function(data) { // handle response }); return false; });</code>
Server-Side PHP (getcontact.php):
<code class="php">$nume = $_REQUEST["nume"]; // empty $email = $_REQUEST["email"]; // empty $telefon = $_REQUEST["telefon"]; // empty $comentarii = $_REQUEST["comentarii"]; // empty</code>
Solution:
Replace the jQuery.post() code with the following:
<code class="javascript">var datastring = $("#contactForm").serialize(); $.ajax({ type: "POST", // method url: "your url.php", // action data: datastring, // form data dataType: "json", // expected response type success: function(data) { // handle response }, error: function() { // handle error } });</code>
Additional Notes:
Update
The above is the detailed content of Why is my jQuery serialized form not sending data to my PHP script?. For more information, please follow other related articles on the PHP Chinese website!