[英]Jquery ajax no response from php
我有一個模式對話框中的窗體。 當我提交表格時,我沒有得到php的響應。 我知道表單和腳本正在工作,因為我可以在對話框外運行它們,並且一切正常。
這是我的表單html代碼:
<div id="add_user">
<form action="resetProcess.php" method="post">
<input type="hidden" name="action" value="Reset Password" />
<table width="385" border="0" cellspacing="0" cellpadding="3">
<tr>
<td colspan="3">
</td>
</tr>
<tr>
<td width="191" align="right"><label for="firstname2">First name *</label></td>
<td width="194" colspan="2"><input type="text" name="firstname" id="firstname2" value="" /></td>
</tr>
<tr>
<td align="right"><label for="lastname2">Last name *</label></td>
<td colspan="2"><input type="text" name="lastname" id="lastname" value="" /></td>
</tr>
<tr>
<td align="right"><label for="email2">Email address *</label></td>
<td colspan="2"><input type="text" name="email" id="email" value="" /></td>
</tr>
<tr>
<td colspan="3" style="padding-top:20px;">
<input type="submit" name="action1" id="requestButton" value="Get Email" /></tr>
</table>
</form>
</div>
這是php進程文件。 請記住,這是正常的,因為它是在自己的瀏覽器窗口中提交的。
<?php
// Required files
include("includes/common.inc.php");
require_once( "users.class.php" );
session_start();
// check if the reset password form has been submitted.
if ( isset( $_POST["action1"] ) and $_POST["action"] == "Reset Password" ) {
$user = new User( array(
"firstname" => isset( $_POST["firstname"] ) ? preg_replace( "/[^ \-\_a-zA-Z0-9]/", "", $_POST["firstname"] ) : "",
"lastname" => isset( $_POST["lastname"] ) ? preg_replace( "/[^ \-\_a-zA-Z0-9]/", "", $_POST["lastname"] ) : "",
"email" => isset( $_POST["email"] ) ? preg_replace( "/[^ \-\_a-zA-Z0-9]@/", "", $_POST["email"] ) : "",
) );
$existingUser = User::getByEmailAddress( $user->getValue( "email"));
if ($existingUser) {
var_dump($existingUser);
echo "Success!! Your Request has been sent!";
} else {
echo "That email address does not match anyone in our system. Please go back and re-enter your information.";
}
}
?>
這是頭文件中包含的js代碼:
<script>
// increase the default animation speed to exaggerate the effect
$.fx.speeds._default = 1000;
$(function() {
$( "#dialog" ).dialog({
autoOpen: false,
show: "fade",
hide: "fade",
width: "400px",
});
$( "#reset-form" ).click(function() {
$( "#dialog" ).dialog( "open" );
return false;
});
// Hide Form error labels
$('.error').hide();
$('#requestButton').click(function(e){
e.preventDefault();
var firstname = $('#firstname').val();
if (firstname == "") {
$('label#firstname_error').show();
$('label#firstname').focus();
return false;
}
var lastname = $('#lastname').val();
if (lastname == "") {
$('label#lastname_error').show();
$('label#lastname').focus();
return false;
}
var email = $('#email').val();
if (email == "") {
$('label#email_error').show();
$('label#email').focus();
return false;
}
var dataString = 'firstname=' + firstname + '&lastname=' + lastname + '&email=' + email;
// ajax call
$.ajax({
type: "POST",
url: "resetProcess.php",
data: dataString,
success: function(result){
//$("#passRequest").fadeOut(500, function(){
console.log("Result: " + result);
//});
},
});
return false;
});
});
</script>
最后,我將包括來自類文件的查詢方法:
public static function getByEmailAddress( $email ) {
$conn = parent::connect();
$sql = "SELECT * FROM " . TBL_USERS . " WHERE email = :email";
try {
$st = $conn->prepare( $sql );
$st->bindValue( ":email", $email, PDO::PARAM_STR );
$st->execute();
$row = $st->fetch();
parent::disconnect( $conn );
if ( $row ) return new User( $row );
} catch ( PDOException $e ) {
parent::disconnect( $conn );
die( "Query failed: " . $e->getMessage() );
}
}
謝謝您的幫助!!
為什么不也發送這樣的數據?
$.ajax({
type: "POST",
url: "resetProcess.php",
data: {
'firstname': firstname,
'lastname': lastname,
'email': email
},
success: function(result) {
//$("#passRequest").fadeOut(500, function(){
console.log("Result: " + result);
//});
},
});
我認為您實際上是在執行get請求,但要指定post。
在您的ajax調用中,傳遞此字符串而不是字符串:
var data = {
'firstname': firstname,
'lastname': lastname,
'email': email
}
或者,您更改方法以發布並保留get-query-string不變。
如果正確inlcuded /所需的必需文件我會檢查,
// Required files
include("includes/common.inc.php");
require_once( "users.class.php" );
因為也許您訪問ajax的方式與訪問resetProcess.php文件的方式有所不同。 您可以通過檢查User類是否確實存在來檢查它。
問題是php腳本正在檢查以確保通過了Submit按鈕,並且還設置了其他隱藏字段。
// check if the reset password form has been submitted.
if(isset($ _POST [“ action1”]]和$ _POST [“ action”] ==“重置密碼”)
這不是與AJAX發布請求一起發送的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.