mythoslife
mythoslife

Reputation: 213

Allow Submission of Form Multiple but Up to Allowed Times on PHP

I have a form that a user can enter in first name, last name and email. You can submit this form multiple times in case you would like to send to more users by simply clicking on submit (of course, so long as the fields are entered correctly using sanitation and validation).

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>

Do you guys have an idea by just laying out this concept? Or do you need a sample code? Thanks for your help.

Was trying to use what Joe suggested but didn't work. Any further help?

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>

Upvotes: 0

Views: 246

Answers (1)

000
000

Reputation: 27247

You can store a counter in $_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
}

Upvotes: 1

Related Questions