1
I have a form in php where I have some fields (input) and a Submit button to send. One of the inputs does the javascript checking if it has equal link or not, if it has equal link it should not leave the save button. Only when I click the send button it sends the form anyway. I want that when the validation of the link is wrong it does not send, I need to make that after the validation it leave or not save.
Below is my javascript code where in ajax I put a validation:
function repeatLink(){
var link = $("#link").val();
if (link === null || link === '') {
$("#link").addClass("input-required");
}
$.ajax({
url: './model/functions/link_repeat.php',
type: 'POST',
data: {link: link},
success: function (data) {
if (data === 'true') {
$("#link").addClass("input-required");
$("#alert-link").append("<span style='color:red'><b>Esse link já existe! Escolha outro!</b></span>");
return false;
}
$("#more").submit()
}
});
}
$link = $_POST['link'];
$stmtLink = $conn->prepare("SELECT link FROM menu WHERE link LIKE :link");
$stmtLink->bindValue(':link', $link);
if($stmtLink->execute() && $stmtLink->rowCount() > 0){
echo 'true';
}else{
echo'false';
}
You checked the returned ajax value on
data
?– Flavio Misawa
Yes, it is returning true or false. If it returns true it falls into my validation in javascript.
– Ana Carolina Ribeiro
What actually happens is, if the person type in my input a link that already exists in my database will return true, ie fall into my javascript validation. When this happens to be true, you should not proceed. That is, give the Submit in the form.
– Ana Carolina Ribeiro
Ana, maybe there’s some extra character in the URL. I’m confused because it doesn’t work, but echo in PHP could have easily been done with
echo ($stmtLink->execute() && $stmtLink->rowCount() > 0)
– Klaider
Ahh got it, put after the
data: {link: link}
=>async: false,
.– Flavio Misawa
@Flaviomisawa Can explain why to use a synchronous request?
– Klaider
I just need to make my validation be used by the push button is not?
– Ana Carolina Ribeiro
Post the HTML of the button you call this Function too. Maybe the problem is there.
– Augusto
@Anacarolinaribeiro at what moment this function is called ? You can put the excerpt of the form, where calls the function ?
– MagicHat