Jump to content

Required data for form


owosso
 Share

Recommended Posts

I'd like my email form to require the user's name and email for successful submission. How do I code for this? Here's my html

 <form method="post" action="contact.Mailer.php"">	  <p>Name:	    <input type="text" name="name" size="19">	    <br>	    <br>	E-Mail:	<input type="text" name="email" size="19">	<br>	<br>		<input type="checkbox" name="check[]" value="blue_color"> 	Donations <br>	<input type="checkbox" name="check[]" value="green_color"> 	Prayer Request <br>	<input type="checkbox" name="check[]" value="orange_color"> 	Street Team <br>		<br>	Message:<br>	<textarea rows="9" name="message" cols="30"></textarea>	<br>	<br>	<input type="submit" value="Submit" name="submit">      </p>	</form>

Here's my php

<?phpif(isset($_POST['submit'])) { $to = "di@adunate.com";$subject = "Contact Living the Promise Ministries";$name_field = $_POST['name'];$email_field = $_POST['email'];$message = $_POST['message']; foreach($_POST['check'] as $value) {$check_msg .= "Checked: $value\n";} $body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message\n $check_msg"; echo "Thank you! Your message has been sent.";mail($to, $subject, $body); } else {echo "Sorry, your form did not go through. Please check that you've entered your information correctly.";}?>

Link to comment
Share on other sites

if (empty($_POST['name'])) {// do something}

You might consider combining the form and the php that processes it into one file. Then, if the form elements come back empty, you can just reprint the form with some kind of error message at the top explaining what's wrong, and/or get fancy and make some red arrows appear at appropriate locations.Write back if you like that idea, but you're not sure how to do it.

Link to comment
Share on other sites

Well, you know a bunch, so I won't give you everything. So here's a grossly simplified thing to give you ideas. I actually tested it and it works fine.

<?php    if ($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_POST['name']) ) {      #process your form data      echo "Okay"; // Just a debugging check   } else {      # if you've never seen this before, don't panic. I'm saying, if the conditions above are not met, print the html document below, which falls inside the 'else' clause. We'll add a closing brace for the else clause AFTER the html doc.   ?><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html>    <head>        <meta http-equiv="content-type" content="text/html;charset=iso-8859-1">        <title>Whatever</title>        <style type="text/css">        </style>    </head>    <body>       <form method="post" action="formtest.php">          <p>Name:          <input type="text" name="name" size="19" <?php if ($_SERVER['REQUEST_METHOD'] == 'POST' && empty($_POST['name'])) {echo "value=\"REQUIRED DATA\" ";}?>>                    <button type="submit">Submit</button>       </form>    </body></html><?php } ?>

Notice the closing brace in the final php tag.Of course, you could jazz this up a lot more, like I said, but you can probably see how to do that now. Inside the form itself, just open up some php tags and echo any extra text or widget you want.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...