Validation input problem

Asked

Viewed 61 times

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 ?

  • Yes, it is returning true or false. If it returns true it falls into my validation in javascript.

  • 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, 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)

  • Ahh got it, put after the data: {link: link} => async: false,.

  • @Flaviomisawa Can explain why to use a synchronous request?

  • I just need to make my validation be used by the push button is not?

  • Post the HTML of the button you call this Function too. Maybe the problem is there.

  • @Anacarolinaribeiro at what moment this function is called ? You can put the excerpt of the form, where calls the function ?

Show 4 more comments

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';
}
  • You just changed the response next to the server. And why exactly return false if you already have the statement else in Javascript?

  • You could have made that a comment then

Browser other questions tagged

You are not signed in. Login or sign up in order to post.