1

If I missed a fundamental part tell me. But after googling here I still dont understand why the Ajax code does not work properly.

My java script code

 $(document).ready(function(){ 
       $("#tform").submit(function() {
       var varUserName=$("#UserName").val();
       var varUserEmail=$("#UserEmail").val();
       var varUserPassword=$("#UserPassword").val();
       alert("email: " + varUserEmail + " name: " + varUserName + " pass: " + varUserPassword);
       $.ajax({
        type       : "POST",
        url:"http://mydomain/tesfolder/Insert.php",
        crossDomain: true,
        data       : {UserName : varUserName, UserPassword : varUserPassword,UserEmail:varUserEmail},
        dataType   : 'json',
        success    : function(response) {
            //console.error(JSON.stringify(response));
            alert('Works!');
        },
       error: function(xhr, status, error) {
        //alert("Didnt work");
           //console.error(JSON.stringify(response));
       }
    
    }); 
    
  });
      });
 <form id="tform">
                <input class="app-input" type="text" name="UserName" placeholder=" Votre Pseudo" id="UserName">
                <input class="app-input" type="email" name="UserEmail" placeholder="Votre Email" id="UserEmail">
                <input class="app-input" type="password" name="UserPassword" placeholder=" Votre Mot de passe" id="UserPassword">
             <input name="MSubmit" type="submit" value="Submit" id="Msubmit">            
            </form>

The PHP codes works properly when I run at localhost.

I also add the following line at the beginning of the php code

<?php 
header("Access-Control-Allow-Origin: *");
...
?>

The alert in the java script showed that the form send the data.

I will appreciate your help.

Adjeiinfo

Adjeiinfo
  • 159
  • 1
  • 2
  • 15
  • 1
    I would suggest looking at the payload in your browser's inspector. It also wouldn't help to have a live feed of the access logs to see if a connection is being made. – Daerik Jun 28 '17 at 00:09
  • One important thing missing is you don't prevent the default form submit....your page is likely refreshing. For more concise CORS implementation see https://stackoverflow.com/a/9866124/1175966 – charlietfl Jun 28 '17 at 00:11

2 Answers2

0

You url is not correct: Changing it to

url:"http://mydomain/tesfolder/Insert.php",
0

Thank you very much, I missed this

async: false,

It works fine

Adjeiinfo
  • 159
  • 1
  • 2
  • 15