Input problem with validation

1

I have a form in php where I have some fields (input) and a submit button to send. One of the inputs I make the check in javascript if it has or does not equal link, if it has equal link it should not let the save button. But when I click the submit button it sends the form anyway. I want that when the validation of the link is wrong it does not send, I have to cause that after the validation it leaves or it does not save.

Below is my javascript code where in ajax I place 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';
       }
    
asked by anonymous 15.09.2016 / 13:56

1 answer

1

Try

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 == 'existe') {
                $("#link").addClass("input-required");
                $("#alert-link").append("<span style='color:red'><b>Esse link já existe! Escolha outro!</b></span>"); 
                return false;
            }else{
            $("#more").submit() ;
            }
        }
    });
}



$link = $_POST['link'];

$stmtLink = $conn->prepare("SELECT link FROM menu WHERE link = :link");
$stmtLink->bindValue(':link', $link);

$stmtLink->execute();

if($stmtLink->rowCount() > 0){
echo 'existe';
}else{
echo'nao_existe';
}
    
15.09.2016 / 14:40