After submitting there must exist 3 possible states:
- Data saved and php message is loaded onto where the form was.
- Data already exists and I load a different php informing the user.
- An error occurred, show an alert asking the user to please try again.
This is what my submit .php looks like:
$test="select * from launchpage where email_launchPage ='$email'";
$testing=$link->query($test);
$alreadyExists=$testing->num_rows;
if($alreadyExists ==1){
echo "<script>alert('already exists');window.history.go(-1);</script>";
} else{
$sqlInsertListingType="insert into launchpage(email_launchPage,language_launchPage) values('".$email."','".$language."')";
$link->query($sqlInsertListingType);
echo "<script>alert('saved');window.history.go(-1);</script>";
}
It works fine on it's own and this is my try at an ajax request to submit:
$(document).on("submit","#form",function(event){
event.preventDefault();
var formURL = $(this).attr("action");
var formType = $(this).attr("method");
var formData = $(this).serialize();
$.ajax({
url: formURL,
type: formType,
data: formData,
success:function(data, textStatus, jqXHR){
console.log('went through ajax');
},
error: function(jqXHR, textStatus, errorThrown){
console.log("i've ruined something");
}
});
});
Which also works, but because of the .preventDefault(); I "don't" know if the data was saved or not.
I have no idea what I should try to do to bring these 2 together, or if there's a better way to do this...
I'm guessing this must be rather easy to solve, I'm just missing something I haven't learned yet.