Florin Dany Florin Dany - 2 months ago 9
Ajax Question

How to send data to the server via Ajax?

The registration form must be Ajax , to send data to the server via Ajax . When you click the submit appears a spinning gear . If the registration was successful then a message " You have successfully registered " If not occur with the error message " Invalid Email Address " or " username already exists " , etc.


  • We include the jQuery library page

  • JQuery add an event that no longer do submit the form

  • Added an event with jQuery , when making submit to execute ajax

  • Depending on the message came to Ajax , whether to show success or failure


Answer

This is all greatly simplified but on the javascript side, you could do this:

var params = {"email": $("input#email")
$.post(yourserver.php, params, validate, "json")

function validate(response) {

  if (response.success) {
    console.log("Allgood")
  } else {
    console.log(response.message)
  }

}

and on the php server side, your server.php could look like this:

<?
  if ( $_REQUEST["email"] ) {
    $response = array("success" => true)
  } else {
    $response = array("success" => false, "message" => "Missing email");
  }

  echo json_encode($response);
?>