After submitting there must exist 3 possible states:
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.
Don't return JS or other html. return some json:
<?php
... process form data
if (processing succeeds) {
$msg = array(
'code' = 1,
'message' = 'Success'
);
} else {
$msg = array(
'code' => 0,
'message' => 'Failed, something dun blowned up'
);
}
echo json_encode($msg);
Then in your success handler:
success: function(data) {
if (data.code == 1) {
do_success_stuff();
} else {
alert('requested failed with error #' + data.code + " and message '" + data.message + "'");
}
}