修订版:我正在尝试处理表单,对其进行验证并向这些收件人发送电子邮件。
<form>
<input name="name1"> <input email="email1">
<input name="name2"> <input email="email2">
<input name="name3"> <input email="email3">
....
<input type="submit" name="submit">
</form>
我试图做的不是做多次输入,而是像这样使用for循环。
<form method=GET action="">
<?php
for($i = 1; $i <= 10; $i++)
{
echo 'First name: <input name="firstname[$i]">';
echo 'Last name:<input name="lastname[$i]">';
echo 'Email: <input name="email[$i]"><br>';
}
?>
<input type="submit" name="Submit" value="Submit">
</form>
<?php
$msg = "a message";
$subject = "a subject";
foreach($_POST['email'] as $email){
mail($email, $subject,$msg,'From: ' . $_POST['sendername'] . "\n\r" );
}
?>
我的问题是我不确定验证这些字段的最佳方法是什么。任何指针都会有所帮助。我不是程序员,我只是一个初学者。
答案 0 :(得分:1)
以下答案是针对OP在编辑之前提出的原始问题。
原始问题:https://stackoverflow.com/revisions/18454820/1
这条线存在问题:
echo '<input type="text" name="firstname[]" size="20" value=".(if (isset($_POST['firstname'][$i])) { print htmlspecialchars($_POST['firstname'][$i]); })." />';
并被替换为:(使其工作)
echo '<input type="text" name="firstname[]" size="20" />';
另外,我将您的表单操作替换为action=""
<!DOCTYPE html>
<html>
<head>
<title>PHP FORM </title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div id="container">
<?php
// Print some introductory text:
print '<h2>Party Invitation Form</h2>
<p>Please enter list of people with first name, last name and email address to get an invitation by email.</p>';
// Check if the form has been submitted:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$problem = FALSE; // No problems so far.
// Check for each value...
for ($i = 0; $i < count($_POST['email']); $i++) {
if (empty($_POST['firstname'][$i])) {
$problem = TRUE;
echo '<input type="text" name="firstname[]" size="20" />';
}
if (empty($_POST['lastname'][$i])) {
$problem = TRUE;
}
if (empty($_POST['email'][$i]) || (substr_count($_POST['email'][$i], '@') != 1) ) {
$problem = TRUE;
}
}
if (!$problem) { // If there weren't any problems...
// Print a message:
echo '<p><b>Thank you for registering! We will send each one an invitation: <b> </b></p>';
for ($i = 0; $i < count($_POST['email']); $i++) {
echo $_POST['firstname'][$i]." ".$_POST['lastname'][$i]." ".$_POST['email'][$i]." <br/>";
// Send the email:
$body = "Thank you {$_POST['firstname'][$i]} for registering with the blah blah blah blah!";
mail($_POST['email'][$i], 'Party Invitation', $body, 'From: email@example.com');
}
// Clear the posted values:
$_POST = array();
} else { // Forgot a field.
print '<p id="error">* Required field! Please try again. Thank you.</p>';
}
} // End of handle form IF.
// Create the form:
?>
<form action="" method="post">
<table>
<tr>
<td>First name:</td>
<td>Last name:</td>
<td>Email:</td>
</tr>
<?php for ($i = 0; $i < 2; $i++) { ?>
<tr>
<td><?php if ($problem == TRUE) { echo '<p id="error">*'; } ?>
<input type="text" name="firstname[]" size="20" value="<?php if (isset($_POST['firstname'][$i])) { print htmlspecialchars($_POST['firstname'][$i]); } ?>" />
</td>
<td><?php if ($problem == TRUE) { echo '<p id="error">*'; } ?>
<input type="text" name="lastname[]" size="20" value="<?php if (isset($_POST['lastname'] [$i])) { print htmlspecialchars($_POST['lastname'][$i]); } ?>" />
</td>
<td><?php if ($problem == TRUE) { echo '<p id="error">*'; } ?><input type="text" name="email[]" size="20" value="<?php if (isset($_POST['email'][$i])) { print htmlspecialchars($_POST['email'][$i]); } ?>" />
</td>
</tr>
<?php } ?>
<tr><td><p><input type="submit" class="button" name="submit" value="Register!" /></td>
</tr>
</table>
</form>
</div>
</body>
</html>