Data transfer in the form of sending via jquery / ajax

I want to submit a form with about 10 inputs using jquery / ajax, but I donโ€™t know how I can pass data to it through ajax.should data parameters do I serialize them?

+7
source share
2 answers

jQuery.serialize might be useful to you. It is important that you use the name property for all form fields that you want to submit. The corresponding code might be something like the following

 $("form#myFormId").submit(function() { var mydata = $("form#myFormId").serialize(); console.log(mydata); // it only for test $.ajax({ type: "POST", url: "myUrlToPostData.php", data: mydata, success: function(response, textStatus, xhr) { console.log("success"); }, error: function(xhr, textStatus, errorThrown) { console.log("error"); } }); return false; }); 
+14
source
 $.ajax({ type: "POST", url: "handle.php", data: "n="+name+"&e="+email, success: function() { alert('It worked'); } }); 

Here is the easiest way to use it. This would simply send the two post-variables $ _POST ['n'] and $ _POST ['e'] to handle.php. Your form will look like this if your ajax is in the send () function:

 <form id="form" onsubmit="send(this.name.value, this.email.value); return false;"> 
+3
source

All Articles