这是我的HTML表单代码。当我点击注册时,它会转到另一页。但是,我希望消息显示在同一模态窗体上。
<div class="popup">
<h2>Sign Up</h2>
<p>Please enter your details here</p>
<form action="script.php" method="post" class="registration_form" id="postForm">
<div id="flash_success">
<label for="email">Login (Email)</label>
<input type="text" id="e-mail" name="e-mail" value="" />
</div>
<div>
<label for="Password">Password</label>
<input type="password" id="Password" name="Password" value="" />
</div>
<div>
<label for="name">name</label>
<input type="text" id="name" name="name" value="" />
</div>
<div>
<label for="College">College</label>
<input type="text" id="College" name="College" value="" />
</div>
我使用Ajax脚本执行相同操作,但无法获得所需的结果。
&GT;
<script>
var options = {
> target: '#flash_success', // your response show in this ID
> // beforeSubmit: callValidationFunction,
> success: YourResponseFunction
> };
> // bind to the form's submit event
> jQuery('#postForm').submit(function() {
> jQuery(this).ajaxSubmit(options);
> return false;
> });
>
>
> }); function callValidationFunction() { // validation code for your
> form HERE }
>
> function YourResponseFunction(responseText, statusText, xhr, $form) {
> if(responseText=='success')
> {
> $('#flash_success').html('Your Success Message Here!!!');
> $('body,html').animate({scrollTop: 0}, 800);
>
> }else
> {
> $('#flash_success').html('Error Msg Here');
>
> } } </script>
我的PHP脚本是这样的:
if (isset($_POST['formsubmitted'])) {
$error = array();//Declare An Array to store any error message
if (empty($_POST['name'])) {//if no name has been supplied
$error[] = 'Please Enter a name ';//add to array "error"
} else {
$name = $_POST['name'];//else assign it a variable
}
if (empty($_POST['e-mail'])) {
$error[] = 'Please Enter your Email ';
} else {
if (preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $_POST['e-mail'])) {
//regular expression for email validation
$Email = $_POST['e-mail'];
} else {
$error[] = 'Your EMail Address is invalid ';
}
}
if (empty($_POST['Password'])) {
$error[] = 'Please Enter Your Password ';
} else {
if (preg_match("/^([0-9])+$/", $_POST['Password'])) {
$Password = $_POST['Password'];
} else {
$error[] = 'Please enter a valid phone number ';
//$Password = $_POST['Password'];
}
if (empty($_POST['College'])) {
$error[] = 'Please Enter Your College ';
} else {
$College = $_POST['College'];
}
}
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: creative.virus23@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>';
}
答案 0 :(得分:0)
您可以在html文件中包含注册脚本( - >将其更改为php)并将您的网站设置为表单中的操作。像这样,您可以通过echo轻松地在同一页面上显示注册状态。
答案 1 :(得分:-1)
创建iframe,将表单目标设置为提交者,将操作设置为脚本文件
<iframe name="submitter" id="submitter"></iframe>
<form action="script.php" target="submitter" method="post" class="registration_form" id="postForm">
</form>
最后在你的“script.php”中包含jquery +这段代码。
<script>
parent.$('#postForm').html('Successfully submitted.');
</script>
BTW:没看过你的完整剧本......但我认为这就是你想要的......