简体   繁体   中英

Email not storing in variable from ajax request

I'm attempting to use ajax to send input from an html form and try to fine the email in a MySQL database. I tested the search and it works just fine (using dummy data.) I have my PHP files running on WAMP. I checked Chrome to see if the email/password are showing and they are. Here is my code:

HTML & Ajax

<html>
    <head>
        <!--initialize jquery from js folder-->
        <script src ="js/jquery-3.3.1.min.js"></script>

    </head>
    <body>
            <!--output of the json-->
            <div>
                    <!--set the id to DOM to show output-->
                    <ul id="DOM">

                    </ul>
            </div>
            <form>
                <label><b>Email</b></label>
                <input type="text" placeholder="Enter email" id="email" 
    required>
                <br/>
                <label><b>Password</b></label>
                <input type="text" placeholder="Enter password" id="passwrd" 
    required>
                <br/>
                <button type="button" id="submit">Login</button>
            </form>
            <a href="insert.html">insert</a>
            <a href="delete.html">delete</a>
            <a href="index.html">show data</a>
            <a href="login.html">login</a>

                    <!--Implementation of jquery/ajax-->
        <script type="text/javascript">
            $('#submit').on('click',function(e){
                e.preventDefault()
                var data = {
                    email: $("#email").val(),
                    passwrd: $("#passwrd").val()
                    }
                $.ajax({
                    url : "http://localhost/api/login.php",
                    type : "POST",
                    dataType : "json",
                    data : JSON.stringify(data),
                    //on success it will call this function
                    success : function(data){
                        alert(data.toString());
                        //if fail it will give this error
                    }, error : function(e){
                        alert("failed to work:" +JSON.stringify(e));
                    }
                });
            });
        </script>
        </body>
    </html>

PHP

include "db.php";
    header('Content-type: application/json');
    //$con->escape_string
    $email = isset($_POST['email']);
    //$email = "fk5829@wayne.edu";
    $result = $con->query("SELECT * FROM users WHERE email='$email'");
    echo "$email";
    if($result->num_rows == 0){ //if the user doesnt exist
        $_SESSION['message'] = "user doesnt exist";
        echo ' / user not exist / ';
    }   
    else{ //user exists
        $user = $result->fetch_assoc();
        if(password_verify(isset($_POST['passwrd']), $user['passwrd'])){ 
    //Verify the password entered
            //if password correct, link information from DB to session 
            //variables
            $_SESSION['f_name']= $user['f_name'];
            $_SESSION['l_name']= $user['l_name'];
            $_SESSION['email']= $user['email'];
            $_SESSION['authorized']= $user['authorized'];

            //Will be used to check if users session is logged 
            //in/allowed to do things
            $_SESSION['logged_in'] = true;
            //return to Success
            return $_SESSION['logged_in'];
            exit("Success");
        }
        else{
            $_SESSION['message'] = "You have entered the wrong password, 
    please try again";
        }
        echo ' / user exists / ';
    }
     echo ' / After the check / ';

My question is this: Why is the email from the form id "email" not getting stored in $email? is it on my ajax request side? or is it in my PHP file when im trying to $_POST?

Any direction is appreciated.

I tried this function and got it to work. Maybe you can try this too.

       <script type="text/javascript">
        $('#submit').on('click',function(e){
            e.preventDefault()
        $.post('http://localhost/api/login.php', {email:$("#email").val(),passwrd:$("#passwrd").val()}, 
            function(data){
                alert(data.toString());
            }).fail(function(e){
                    alert("failed to work:" +JSON.stringify(e));
            });

        });
        </script>

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