admin管理员组

文章数量:1313139

I have an ajax call that sends data from a form to a php file that will then insert that data to the database. I put a call to die in said php file because I want to try something but it doesn't work.

addUserForm.php

<script>
    $(document).ready(function () {
        var $form = $('form');
        $form.submit(function (event) {
            event.preventDefault();

            var formData = $form.serialize(),
                url = $form.attr('action');

            $.ajax({
                type: "POST",
                url: url,
                data: formData,
                success: function () {
                    //$("#div1").load("table.php");
                    alert('User Successfully Added');
                    document.getElementById("form1").reset();
                }
            });
        });
    });
</script> 

Here is the php file:

addUser.php

<?php
    include('sqlconnection.php');
    die('here');
    $firstname = $_POST['fname'];
    $lastname = $_POST['lname'];
    $middlename = $_POST['mname'];
    $password = $_POST['pword'];
    $username = $_POST['uname'];
    $gender = $_POST['gender'];
    $utype = $_POST['utype'];

    $query = "INSERT INTO user (firstname,lastname,middlename,gender) VALUES ('$firstname','$lastname','$middlename','$gender')";   

    mysqli_query($con,$query);

    $result = mysqli_query($con,"SELECT id FROM user WHERE firstname = '$firstname'");

    $row = mysqli_fetch_assoc($result);

    $uid=$row['id'];

    $result = mysqli_query($con,"INSERT INTO accounts (u_id,username,password,account_type) VALUES ('$uid','$username',md5('$password'),'$utype');");

?>

Even when there is a die call in adduser.php it still alerts that the user was successfully added.

I have an ajax call that sends data from a form to a php file that will then insert that data to the database. I put a call to die in said php file because I want to try something but it doesn't work.

addUserForm.php

<script>
    $(document).ready(function () {
        var $form = $('form');
        $form.submit(function (event) {
            event.preventDefault();

            var formData = $form.serialize(),
                url = $form.attr('action');

            $.ajax({
                type: "POST",
                url: url,
                data: formData,
                success: function () {
                    //$("#div1").load("table.php");
                    alert('User Successfully Added');
                    document.getElementById("form1").reset();
                }
            });
        });
    });
</script> 

Here is the php file:

addUser.php

<?php
    include('sqlconnection.php');
    die('here');
    $firstname = $_POST['fname'];
    $lastname = $_POST['lname'];
    $middlename = $_POST['mname'];
    $password = $_POST['pword'];
    $username = $_POST['uname'];
    $gender = $_POST['gender'];
    $utype = $_POST['utype'];

    $query = "INSERT INTO user (firstname,lastname,middlename,gender) VALUES ('$firstname','$lastname','$middlename','$gender')";   

    mysqli_query($con,$query);

    $result = mysqli_query($con,"SELECT id FROM user WHERE firstname = '$firstname'");

    $row = mysqli_fetch_assoc($result);

    $uid=$row['id'];

    $result = mysqli_query($con,"INSERT INTO accounts (u_id,username,password,account_type) VALUES ('$uid','$username',md5('$password'),'$utype');");

?>

Even when there is a die call in adduser.php it still alerts that the user was successfully added.

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Aug 23, 2013 at 8:02 jmjassy27jmjassy27 871 gold badge5 silver badges10 bronze badges 0
Add a ment  | 

6 Answers 6

Reset to default 6

That's because die() only terminates/ends the PHP script. From an AJAX point of view the request was still successful.

You should echo the info in the PHP page and then output the content of the response in your AJAX.

You could also set the response header in your PHP Script to something other than 200/OK, such as 401/Unauthorized or 400/Bad Request. Basically all 400 and 500 status codes indicate error.

Since the PHP code executes successfully even thou die(), the ajax will trigger the success and you will recevie the success message.

to stop your javascript at any point you can add return false;

In your success block

success: function () {
    //$("#div1").load("table.php");
    alert('User Successfully Added');
    document.getElementById("form1").reset();
}

You can add these two line like this

success: function (data) {
    /* These two lines*/
    console.log(data);
    return false;

    //$("#div1").load("table.php");
    alert('User Successfully Added');
    document.getElementById("form1").reset();
}

So once you're done with your debugging you can remove those lines..

Die function doesn't stop javascript. It just stop PHP. For exemple, if you add the die() function before inserting datas, datas will not be inserted but the success funciton will be executed and you will have alert.

If you want to execute the Error function, you have to add Throw exception or header 403 in the php file.

The jQuery success function just make sure the page is loaded. A die in PHP doesn't change that. You will have to check with returned data.

success Type: Function( PlainObject data, String textStatus, jqXHR jqXHR ) A function to be called if the request succeeds. The function gets passed three arguments: >The data returned from the server, formatted according to the dataType parameter; a string >describing the status; and the jqXHR (in jQuery 1.4.x, XMLHttpRequest) object. As of jQuery >1.5, the success setting can accept an array of functions. Each function will be called in >turn. This is an Ajax Event.

ajax request php file, die ('here') is equivalent to echo 'here', return value, that successful implementation

mysqli_connect("localhost", "root", "", "student") or die("Connection Fail");

//after fail the connection die message not showing

本文标签: phpWhy doesn39t the die() function workStack Overflow