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.
6 Answers
Reset to default 6That'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
版权声明:本文标题:php - Why doesn't the die() function work? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741946764a2406463.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论