我为登录系统编写了这个JavaScript代码。我在将其连接到PHP文件时遇到问题。我想从我的PHP脚本中显示一条消息并隐藏此表单。
$('#submit').click(function()
{
var email=$("#email").val();
var password=$("#password").val();
if(ck_email.test(email) && ck_password.test(password) )
{
$("#form").show().html("<h1>Thank you!</h1>");
}
return false;
});
PHP脚本
include ('database_connection.php');
if (isset($_POST['formsubmitted'])) {
if (empty($error)) //send to Database if there's no error '
{ // If everything's OK...
// Make sure the email address is available:
$query_verify_email = "SELECT * FROM members WHERE Email ='$Email'";
$result_verify_email = mysqli_query($dbc, $query_verify_email);
if (!$result_verify_email) { echo ' Database Error Occured ';
}
if (mysqli_num_rows($result_verify_email) == 0) { // IF no previous user is using this email .
// Create a unique activation code:
$activation = md5(uniqid(rand(), true));
$query_insert_user = "INSERT INTO `members` ( `Username`, `Email`, `Password`, `College`, `Activation`) VALUES ( '$name', '$Email', '$Password', '$College' '$activation')";
$result_insert_user = mysqli_query($dbc, $query_insert_user);
if (!$result_insert_user) {
echo 'Query Failed ';
}
if (mysqli_affected_rows($dbc) == 1) { //If the Insert Query was successfull.
// Send the email:
$message = " To activate your account, please click on this link:\n\n";
$message .= WEBSITE_URL . '/activate.php?email=' . urlencode($Email) . "&key=$activation";
mail($Email, 'Registration Confirmation', $message, 'From: cr@gmail.com');
// Flush the buffered output.
// Finish the page:
echo '<div class="success">Thank you for
registering! A confirmation email
has been sent to '.$Email.' Please click on the Activation Link to Activate your account </div>';
} else { // If it did not run OK.
echo '<div class="errormsgbox">You could not be registered due to a system
error. We apologize for any
inconvenience.</div>';
}
} else { // The email address is not available.
echo '<div class="errormsgbox" >That email
address has already been registered.
</div>';
}
} else {//If the "error" array contains error msg , display them
echo '<div class="errormsgbox"> <ol>';
foreach ($error as $key => $values) {
echo ' <li>'.$values.'</li>';
}
echo '</ol></div>';
}
mysqli_close($dbc);//Close the DB Connection
}
我知道我必须使用Ajax,但是,我无法获得正确的代码。
答案 0 :(得分:1)
您必须向运行php脚本的网络服务器地址发送帖子或获取请求。你得到的答案就是PHP脚本的echo部分。
例如,使用JQuery和post:
$.post(server+'index.php',{ email: $("#email").val(),formsubmitted:true, password: $("#password").val() }, function(data) {
$("#form").show().html(data);
});
在第一个post-parameter中,你会说出你的服务器所在的位置,以及你想要访问的php文件。通常是
server = "http://127.0.0.1/"
作为服务器的localhost。在第二个参数中,您可以为PHP脚本提供后变量,PHP可以在$ POST ['email']中使用,例如可以使用。第三个参数是回调,当服务器的回答到达时,将执行回调。
当然,您仍然需要编辑上面发布的一些PHP以使其正常工作。例如,在使用之前从Post字段加载$ Email-Variable:
$Email = $POST['email'];