I'm trying to send form values through ajax request to store in database but the file is not invoked through ajax. On submitting through button it goes to servr.js file and then through ajax it stored into database.
<button id="submit" class="btn btn-primary" type="submit" onclick="myFunction()" >Create an Account</button>
And js file code as follow.
function myFunction(){
var name = document.getElementById("firstName").value;
var lname = document.getElementById("lastName").value;
var mail = document.getElementById("email").value;
var password =document.getElementById("password").value;
var confpass= document.getElementById("passwordConfirmation").value;
if(password != confpass)
{
alert ('password doesnot match!');
}
// var form_data = $('#edit_user').serialize();
var datastring= 'name1=' + name + 'name2=' + lname +'email='+mail+ 'pass='+password;
$.ajax({
url: "learnapi.php",
type: "post",
dataType: "json",
data: {type: "signup", datastring:datastring },
ContentType: "application/json",
success: function (response) {
alert(JSON.stringify(response));
},
error: function (err) {
alert(JSON.stringify(err));
}
});
};
It goes to another php file which performs insert function .But this file not get call when I checked through inspect element(Q). learnapi.php
<?php
header('Access-Control-Allow-Origin: *');//Should work in Cross Domaim ajax Calling request
mysql_connect("localhost","root","");
mysql_select_db("learnapp");
if(isset($_POST['type']))
{
$res = [];
if($_POST['type'] =="signup"){
$name = $_POST ['name1'];
$lname = $_POST['name2'];
$passW = $_POST['pass'];
// $passW1 = $_POST['Pass1'];
$mail = $_POST ['email'];
var_dump($_POST);
$query1 = "insert into signup(firstname,lastname,password,email) values('$name','$lname','$passW','$mail')";
$result1 = mysql_query($query1);
if($result1)
{
$res["flag"] = true;
$rest["message"] = "Data Inserted Successfully";
}
else
{
$res["flag"] = false;
$rest["message"] = "Oppes Errors";
}
}
else{
$res["flag"] = false;
$rest["message"] = "Invalid format";
}
echo json_encode($rest);
}
?>
I tried but my ajax request is not called. What did I missed in this