我认为解决方案很容易找到,但我找不到它。我希望我的注册表格可以提交到数据库,并将表格数据作为确认电子邮件同时发送到用户的电子邮件中,并使用同一个提交按钮。这种形式有两种行为似乎是合乎逻辑的,但我没有发现这种事情的例子。也许注册页面上的一些PHP代码会识别成功提交并将数据发送到一个php文件,该文件将在数据被插入数据库表的同时处理一封电子邮件?注册表单提交给数据库并通过他们的信息向用户发送确认电子邮件 - 2个操作,1个按钮
register.php中,HTML
<?php
session_start();
if (!isset($_SESSION['usr_id']) && empty($_SESSION['usr_id'])) {
} else {
header('Location: mustlogout.php'); #redirect URL
}
?>
<?php
include_once 'db.php';
//set validation error flag as false
$error = false;
//check if form is submitted
if (isset($_POST['signup'])) {
$name = mysqli_real_escape_string($con, $_POST['name']);
$user_name = mysqli_real_escape_string($con, $_POST['user_name']);
$email = mysqli_real_escape_string($con, $_POST['email']);
$password = mysqli_real_escape_string($con, $_POST['password']);
$cpassword = mysqli_real_escape_string($con, $_POST['cpassword']);
$name = stripslashes($name);
$user_name = stripslashes($user_name);
$email = stripslashes($email);
$password = stripslashes($password);
$cpassword = stripslashes($cpassword);
if (!preg_match("/^[a-zA-Z ]+$/",$name)) { /* name can contain only alpha characters and space */
$error = true;
$name_error = "Name must contain only letters";
}
if (!preg_match("/^[a-zA-Z-0-9 ]+$/",$user_name)) { /* letters and numbers */
$error = true;
$user_name_error = "User name can contain only letters and numbers";
}
if(!filter_var($email,FILTER_VALIDATE_EMAIL)) { /* will accept only email addresses */
$error = true;
$email_error = "Please Enter Valid Email ID";
}
if(strlen($password) <6) { /* must be 6 or more characters */
$error = true;
$password_error = "Password must be minimum of 6 characters";
}
if($password != $cpassword) { /* must match the first password entry */
$error = true;
$cpassword_error = "Password and Confirm Password doesn't match";
}
if (!$error) {
if(mysqli_query($con, "INSERT INTO forumusers(name,user_name,email,password) VALUES('" . $name . "', '" . $user_name . "', '" . $email . "', '" . md5($password) . "')")) {
$successmsg = "Successfully Registered! <a href='login.php'>Click here to Login</a>"; /* if register is successful */
} else {
$errormsg = "Error in registering...Please try again later!"; /* if register is not successful */
}
}
}
?>
<!doctype html>
<html><!-- InstanceBegin template="/Templates/index.dwt" codeOutsideHTMLIsLocked="false" -->
<head>
register.php以上的PHP,在HTML
<!-- InstanceBeginEditable name="EditRegion3" -->
<div class="title-bar"><n6>Forum Registration</n6></div>
<div class="main-content">
<div class="form-reg" style="margin-bottom: 4em;"><!-- Begin div to contain form -->
<table width="50%" style="padding-left: 20px;">
<form role="form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="signupform">
<fieldset>
<tr>
<td>
<div style="margin-top: 0px; margin-bottom: 5px;">
<label for="name" class="formfield-names">Name</label>
<input type="text" name="name" placeholder="Enter Your Full Name" required value="<?php if($error) echo $name; ?>" class="form-control" />
<span class="text-danger"><?php if (isset($name_error)) echo $name_error; ?></span>
</div>
</td>
</tr>
<tr>
<td>
<div style="margin-top: 0px; margin-bottom: 5px;">
<label for="name" class="formfield-names">User Name</label>
<input type="text" name="user_name" minlength="5" maxlength="15" placeholder="5 to 15 Letters/Numbers" required value="<?php if($error) echo $user_name; ?>" class="form-control" />
<span class="text-danger"><?php if (isset($user_name_error)) echo $user_name_error; ?></span>
</div>
</td>
</tr>
<tr>
<td>
<div style="margin-top: 10px; margin-bottom: 5px;">
<label for="name" class="formfield-names">Email</label>
<input type="text" name="email" placeholder="Enter a Valid Email" required value="<?php if($error) echo $email; ?>" class="form-control" />
<span class="text-danger"><?php if (isset($email_error)) echo $email_error; ?></span>
</div>
</td>
</tr>
<tr>
<td>
<div style="margin-top: 10px; margin-bottom: 5px;">
<label for="name" class="formfield-names">Password</label>
<input type="password" name="password" minlength="6" maxlength="16" placeholder="6 to 16 Chracters" required class="form-control" />
<span class="text-danger"><?php if (isset($password_error)) echo $password_error; ?></span>
</div>
</td>
</tr>
<tr>
<td>
<div style="margin-top: 10px; margin-bottom: 5px;">
<label for="name" class="formfield-names">Confirm Password</label>
<input type="password" name="cpassword" placeholder="Confirm Password" required class="form-control" />
<span class="text-danger"><?php if (isset($cpassword_error)) echo $cpassword_error; ?></span>
</div>
</td>
</tr>
<tr>
<td>
<div style="margin-top: 10px; margin-bottom: 5px;">
<input type="submit" name="signup" value="Register" class="button" />
</div>
</fieldset>
</form>
</td>
</tr>
<tr><td><div style="margin-top: 10px; margin-bottom: 5px;" class="formfield-names">Already Registered? <a href="login.php">Login Here</a></div></td></tr>
</table>
<span class="formfield-names"><?php if (isset($successmsg)) { echo $successmsg; } ?></span>
<span class="formfield-names"><?php if (isset($errormsg)) { echo $errormsg; } ?></span>
</div><!-- End div to contain form -->
</div>
<!-- InstanceEndEditable -->
我发现很多的 “形式有两个按钮” 和各种形式的“如何向数据库提交表格“和”如何使用表格发送电子邮件“并显示各种确认信息,而我在2个操作中找到的信息似乎并不完整。帮助将不胜感激。
您是否可以不发送来自同一个脚本的电子邮件来处理注册? – bugfroggy
只需在插入成功时添加电子邮件发送?这不是真正的火箭科学?在'$ successmsg'变量下,添加一个'mail(// info goes here)' – junkfoodjunkie
加油!确认数据库查询成功后,只需发送电子邮件即可。这不是什么大不了的,你不需要第二个动作。你可以在一个PHP文件中做多件事情 –