0

我有一个表格,用户可以输入名字、姓氏和电子邮件。您可以多次提交此表单,以防您希望通过单击提交来发送给更多用户(当然,只要使用卫生和验证正确输入了字段)。

if (isset($_POST['Submit'])) {
//sanitize if field (like email) is not empty, then validate it using a function
// such as FILTER_VALIDATE_EMAIL and FILTER_SANITIZE_STRING
//like if ($_POST['email'] != "") { do the sanitation and validation here }
//then if no error send the email
//then $_POST = array(); to clear up form for the next entry
}

<form> form here</form>

你们有一个想法,只是提出这个概念吗?还是您需要示例代码?谢谢你的帮助。

试图使用乔的建议但没有奏效。有什么进一步的帮助吗?

session_start(); 
 if (isset($_POST['Submit'])) {
   if (isset($_SESSION['submission_count'])) {
     if ($_SESSION['submission_count'] > 10) {
      echo "You can't submit so many!";
      exit;
     }
   }
else {
  $_SESSION['submission_count'] = 0;
}
$_SESSION['submission_count'] += 1;
   // Form validation and sanitation
   // Send email 

  }

 ?>

<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
First Name:
<input type="text" name="firstname" value="<?php echo $_POST['firstname']; ?>" size="50" /><br />
Last Name:
<input type="text" name="lastname" value="<?php echo $_POST['lastname']; ?>" size="50" /><br />
Email Address: 
<input type="text" name="email" value="<?php echo $_POST['email']; ?>" size="50"/> <br/><hr />

<br/>
<input type="submit" class="button" name="Submit" />
</form>
4

1 回答 1

1

您可以将计数器存储在$_SESSION.

http://www.php.net/manual/en/intro.session.php

<?php 
// Starting the session 
session_start(); 
if (isset($_POST['Submit'])) {
  if (isset($_SESSION['submission_count'])) {
    if ($_SESSION['submission_count'] > 5) {
      echo "You can't submit so many!";
      exit;
    }
  }
  else {
    $_SESSION['submission_count'] = 0;
  }
  $_SESSION['submission_count'] += 1;
  // Do the rest of your form submission
}
于 2013-09-18T22:57:50.700 回答