-1

REVISED: I'm trying to process a form, validates it and sends emails to these recipients.

<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>

What I was trying to do was rather than doing a multiple inputs, I'd use the for loop like so..

<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" );
}
?>

My question is I'm not sure what's the best way to validate these fields. Any pointers would be helpful. I'm not a programmer I'm just a beginner.

Black Sheep
  • 6,604
  • 7
  • 30
  • 51
mythoslife
  • 203
  • 1
  • 6
  • 20
  • I'm not sure what the question is – Andy G Aug 26 '13 at 23:50
  • and the question would be? –  Aug 26 '13 at 23:50
  • *"the code that processes the form is on the same php page"* --- then don't use `action="form.php"` use `action=""` --- – Funk Forty Niner Aug 26 '13 at 23:56
  • My form doesn't work the way I want it to be. Somebody enters name, email, then php processes it and sends an invitation via email. – mythoslife Aug 27 '13 at 00:02
  • **doesn't work** well that clears up the issue. –  Aug 27 '13 at 00:08
  • You'll have to be more specific as to where it's failing, what exactly do you want it to do as opposed to sending email? As if someone enters name and email and there are no 'problems' then your code will go on to send email based on your telling it to with `if (!$problem)` – James Aug 27 '13 at 00:08
  • So the form takes these fields: 'firstname', 'lastname', 'email'. On this form has validations to set $problem = TRUE if not then it processes the form. If there is $problem = TRUE fails, then it'll you can't submit the form. – mythoslife Aug 27 '13 at 00:15
  • @mythoslife Ok, right off the bat, you've got a `Parse error: syntax error, unexpected 'firstname' (T_STRING), expecting ',' or ';'`, I can tell you that much. So start by fixing that. – Funk Forty Niner Aug 27 '13 at 00:18
  • @mythoslife Fix this `value=".(if (isset($_POST['firstname'][$i])) { print htmlspecialchars($_POST['firstname'][$i]); })."` and your form will appear. – Funk Forty Niner Aug 27 '13 at 00:20
  • @mythoslife Actually, delete `value=".(if (isset($_POST['firstname'][$i])) { print htmlspecialchars($_POST['firstname'][$i]); })."` and your form will work, maybe not quite like you expect it to, but it did work for me. All you need to do now, is work on it and the rest of its functionality. – Funk Forty Niner Aug 27 '13 at 00:23
  • @mythoslife First you need to change your `
    ` to `
    ` because you're also using `foreach($_POST` and these must match.
    – Funk Forty Niner Aug 27 '13 at 01:04
  • @mythoslife You can use this snippet to verify `if (count(array_filter($_POST)) != count($_POST)) { echo "You have an empty field."; } else { echo "All is OK"; }` – Funk Forty Niner Aug 27 '13 at 01:07
  • @Dagon thanks anyways... Fred helped me. At least the form I posted was processed with no errors. I was trying to create a form in which I created a form, PHP code to process it and also uses the mail() function. But, then, the form has multiple same text fields 10 names, 10 emails so I used a for loop to echo the same HTML input tags. My form uses the same page and validates it to see if fields are empty or submitted. Here's my other question as now I was trying to use an external file to read it... http://stackoverflow.com/questions/18475243/get-contents-of-a-file-on-php-form-mail-function – mythoslife Aug 27 '13 at 20:59

1 Answers1

1

EDIT

The answer below is for the OP's original question before his EDIT.

Original question: https://stackoverflow.com/revisions/18454820/1


This line was problematic:

echo '<input type="text" name="firstname[]" size="20" value=".(if (isset($_POST['firstname'][$i])) { print htmlspecialchars($_POST['firstname'][$i]); })." />';

and was replaced by: (to get it working)

echo '<input type="text" name="firstname[]" size="20" />';
    

Plus, I replaced your form action to action=""

Working code:

<!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>
Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • @mythoslife You're welcome. And if it's satisfactory, please mark answer as answered by accepting as correct, cheers :) – Funk Forty Niner Aug 27 '13 at 00:42
  • Yes that works... Thanks. I can now move on to figuring out how to use a template. – mythoslife Aug 27 '13 at 00:54
  • @mythoslife I just thought of something. If someone comes along and fixes your NEW problem, then theoretically my answer will become null and void. You should have posted a new question instead because I fear this will cause a lot of confusion. – Funk Forty Niner Aug 27 '13 at 01:35
  • Oh I see... I'll make sure to post another and not void your answer. Thanks again. – mythoslife Aug 27 '13 at 18:48
  • Hello I just created one here. Maybe if you can take a look at it would be nice. If not, that's okay too; I understand. Thanks. http://stackoverflow.com/questions/18475243/get-contents-of-a-file-on-php-form-mail-function – mythoslife Aug 27 '13 at 20:55