简体   繁体   中英

submit ajax form with condition

hi i am working on an authentification page , so my code is the following

    $(document).ready(function(){

       var form = $("#connexion");
       var login =$("#logins");
       var password=$("#passe");

          $("#go").click(function(e){
             e.preventDefault();
            $.ajax({type: "POST",
                    url: "check_con.php",
                    data: { email:login.val() , password:password.val() },
                    success:function(result){

                    if(result == 'true') 
                    {
                       alert(result);
                    }


            }});
          });
        });

i get the form , the login and password and i pass them to my php script .

    <?php
    //data connection file
    //require "config.php";
    require "connexion.php";
    extract($_REQUEST);


        $pass=crypt($password);

        $sql = "select * from Compte where email='$email'";
        $rsd = mysql_query($sql);
        $msg = mysql_num_rows($rsd); //returns 0 if not already exist

        $row = mysql_fetch_row($rsd);

        if($msg == 0)
        {
           echo"false1";
        }
        else if($row[1] == crypt($password,$row[1]))
        {  
           echo"true";
        }
        else
        {
           echo"false2";
        }




    ?>

everything is goood , when i give the good email and password i get true otherwise i get false, that's not the problem , the problem is i am trying to redirect the user to another page called espace.php if the result is true so i've tried this .

    $(document).ready(function(){

       var form = $("#connexion");
       var login =$("#logins");
       var password=$("#passe");

          $("#go").click(function(e){

            $.ajax({type: "POST",
                    url: "check_con.php",
                    data: { email:login.val() , password:password.val() },
                    success:function(result){

                    if(result == 'true') 
                    {
                      form.submit(true);
                    }
                    else form.submit(false);


            }});
          });
        });

now even if the login and password are not correct the form is submitted , how could i manage to do that i mean , if the informations are correct i go to another page , otherwise i stay in the same page.

use json to get result from authanication page

<?php
    //data connection file
    //require "config.php";
    require "connexion.php";
    extract($_REQUEST);


        $pass=crypt($password);

        $sql = "select * from Compte where email='$email'";
        $rsd = mysql_query($sql);
        $msg = mysql_num_rows($rsd); //returns 0 if not already exist

        $row = mysql_fetch_row($rsd);
        $result = array();
        if($msg == 0)
        {
           $result['error'] = "Fail";
        }
        else if($row[1] == crypt($password,$row[1]))
        {  
           $result['success'] = "success";
        }
        else
        {
          $result['error'] = "try again";
        }

   echo json_encode($result); die;


    ?>

And in the ajax,, check what is the response.

$(document).ready(function(){

       var form = $("#connexion");
       var login =$("#logins");
       var password=$("#passe");

          $("#go").click(function(e){

            $.ajax({type: "POST",
                    url: "check_con.php",
                    data: { email:login.val() , password:password.val() },
                    success:function(result){
                      var response = JSON.parse(result);
                       if(response.error){
                             //here provide a error msg to user.
                             alert(response.error);
                        }
                      if(response.success){
                             form.submit();
                       }



            }});
          });
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM