我有一个通过ajax验证的表单,当提交调用updatepassword.php时,会使用Password2的输入更新数据库。但是,调用updatepassword.php时出现了问题,因为它赢了& #39;工作。请告诉我!谢谢。 PHP文件低于所有其他文件。
<script>
function checkForm() {
var pass = document.getElementById("oldpassword").value;
var pass1 = document.getElementById("passwordnew1").value;
var pass2 = document.getElementById("passwordnew2").value;
if (pass == '' || pass1 == '' || pass2 == '') {
alert("Fill All Fields");
} else {
var passcheck = document.getElementById("editpassword");
var passcheck1 = document.getElementById("equalpasswords1");
var passcheck2 = document.getElementById("equalpasswords2");
if (passcheck.innerHTML == 'Must be 3+ letters' || passcheck1.innerHTML == 'Password too short' || passcheck2.innerHTML == 'Password too short') {
alert("Fill Valid Information");
} else {
document.getElementById("myForm").submit();
}
}
}
function validate(field, query) {
var xmlhttp;
if (window.XMLHttpRequest) { // for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else { // for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState != 4 && xmlhttp.status == 200) {
document.getElementById(field).innerHTML = "Validating..";
} else if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById(field).innerHTML = xmlhttp.responseText;
} else {
document.getElementById(field).innerHTML = "Error Occurred. <a href='index.html'>Reload Or Try Again</a> the page.";
}
}
xmlhttp.open("GET", "validation.php?field=" + field + "&query=" + query, false);
xmlhttp.send();
}
</script>
<script type="text/javascript">
function submitForm() {
$("document").ready(function(){
$(".ajaxcall").submit(function() {
var data = {
"action": "test"
};
data = $(this).serialize() + "&" + $.param(data);
$.ajax({
type: "POST",
dataType: "json",
url: "updatepassword.php",
data: { Password2: $('#passwordnew2').val()} ,
success: function(data) {
$("#check").html(
data["Password2"]
);
}
});
return false;
});
});
}
</script>
这是我的HTML:
<body>
<form action="#" id="myForm" name="myForm" method="post" onsubmit="return submitForm();" class="ajaxcall">
<div class="container">
<table>
<tr>
<td><p><b>My email:</b></p></td>
<td></td>
<td></td>
</tr>
<tr>
<td><p><b>My password:</b></p></td>
<td><p id="editpassword"></p><p id="insertpassword"><input id="oldpassword" name="inputedPassword" onblur="validate('editpassword', this.value)" type="password"></p></td>
<td></td>
</tr>
<tr>
<td><p><b>My new password:</b></p></td>
<td><p id="equalpasswords1"></p><input id="passwordnew1" name="Password1" onblur="validate('equalpasswords1', this.value)" type="password"></td>
<td></td>
</tr>
<tr>
<td><p><b>Confirm password:</b></p></td>
<td><p id="equalpasswords2"></p><input id="passwordnew2" name="Password2" onblur="validate('equalpasswords2', this.value)" type="password" > <input type="button" value="Save" onclick="checkForm()"></td>
</tr>
</table>
</div>
</form>
<p id="check"></p>
</body>
</html>
这是updatepassword.php:
<?php
session_start();
$email = $_SESSION['email'];
?>
<?php
if (is_ajax()) {
if (isset($_POST["action"]) && !empty($_POST["action"])) {
$action = $_POST["action"];
switch($action) {
case "test": test_function($email); break;
}
}
}
function is_ajax() {
return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
}
function test_function($email) {
$inData = json_decode(file_get_contents("php://input"));
$x = $inData["Password2"];
define('DB_NAME', 'Name');
define('DB_USER', 'User');
define('DB_PASSWORD', 'Password');
define('DB_HOST','HOST');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected) {
die('Can\'t use'. DB_NAME. ': ' . mysql_error());
}
$result = "UPDATE `Name`.`Students` SET `Password` = '$x' WHERE `Students`.`StudEmail` = '$email' ";
if (!mysql_query($result)) {
die('error: ' .mysql_error());
}
$return["json"] = json_encode($return);
echo json_encode($return);
$_SESSION['Password2'] = $x;
}
?>
答案 0 :(得分:0)
您通过POST发送的JSON数据在您的PHP中无法像您尝试访问它那样直接提供:
$return["Password2"]
你需要做
$inData = json_decode(file_get_contents("php://input"));
然后您可以访问$inData["Password2"]