I am trying to send form data to php using ajax but it’s not working and I searshed alot without fining anyy solution
`
this is the ajax code in my index.js
`
$("#reg_form").submit(function (event) {
alert("clicked")
var registerData = {
ajxfname: document.getElementById("fnameInput").value,
ajxlname: document.getElementById("lnameInput").value,
ajxemail: document.getElementById("emailInput").value,
test: "this is test text"
};
$.ajax({
type: "POST",
url: "server.php",
data: registerData,
dataType: "json",
success: function (response) {
alert("success");
}
});
event.preventDefault();
});
`
this is the code I use to print the data that I got in server.php
`
foreach ($_POST as $key => $value) {
echo $key;
echo " : ";
echo $value;
echo "<br>";
}
`
it just print the names and the data of the input in the form(fname,lname,email) in the html
and this is the form in the html
`
<form id="reg_form" action="server.php" method="post" >
<input id="fnameInput" name="fname" placeholder="First name" type="text">
<input id="lnameInput" name="lname" placeholder="Last name" type="text">
<input id="emailInput" name="email" placeholder="E-mail" type="text">
<button class="btn" type="submit" >Sign up</button>
`
thanks in advance
2
Answers
Jquery Ajax is to old i think you can use javascript Fetch try this
server.php file
if print_r is empty try this
Just try below eg:
Or
In PHP