Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Unless you have some algorithms already in place, you will have problems on the server side handling the data. You may want to prepare the data and than stringify it to JSON. I would also keep the ajax functionality in its own function an use the <code>promise</code> feature. That way you can also use it for other calls within your script.</p> <p><strong>ajax function with deferred</strong></p> <pre><code>function ajaxsend(data, url) { var deferred = $.ajax({ type: 'POST', url: url, data: data, dataType: "json", }); return deferred.promise(); } </code></pre> <p><strong>form data handling and preparation</strong></p> <pre><code>$("form").submit(function (event) { event.preventDefault(); var formdata = $('form').serializeArray(); var formobject = {}; // transform data to prepare for JSON $(formdata).each(function (e) { formobject[formdata[e].name] = formdata[e].value; }); var data = { json: JSON.stringify(formobject) }; var url = 'http://www.domain.com/sendmail.php'; var url2 = 'some_other.php'; ajaxsend(data, url).done(function (response) { // handle returned results console.log(response); } ajaxsend(data, url2).done(function (response) { // handle returned results console.log(response); } } </code></pre> <p>At the server side you receive the value with:</p> <pre><code>$data = json_decode($_POST['json']); </code></pre> <p>Then you can access your data with the fieldnames of your form. For instance .. </p> <pre><code>$data -&gt; firstname; </code></pre> <p>You can send a response from the php file:</p> <pre><code>if(success == true) { $result = array("success" =&gt; true , "message" =&gt; "form submitted"); echo json_encode($result); } if(success == false) { $result = array("success" =&gt; false , "message" =&gt; "an error occured"); echo json_encode($result); } </code></pre> <p>At the javascript side you can catch the response values</p> <pre><code>console.log(response.success); console.log(response.message); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload