Jump to content

[SOLVED] Mandatory field in a form


seasexsun

Recommended Posts

I'm trying to make a field mandatory and it's still letting you fill out the form without filling in the email address, which is the field I want to be mandatory.  I would really appreciate anyone's input on what is wrong with my script.  That would be great.  It's a very short script.

 

Thanks

 


	<?php
	if (isset($_POST['Submit']) &&
	    isset($_POST['email']) &&
	    trim($_POST['email'])  != '') { 
		echo '<h1>Thank you for filling out this form!</h1>';
		} else {


$to = "email@mydomain.com";
$subject = "Website Email";
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
$headers = "From: $email";
$sent = mail($to, $subject, $message, $headers) ;
if($sent)
{print ""; }
else
{print "We encountered an error sending your mail"; }
?>
<?php
}
?>

Link to comment
Share on other sites

Try

 

<?php

if (isset($_POST['Submit'])){

   if (isset($_POST['email']) && $_POST['email'] != "")
      echo '<h1>Thank you for filling out this form!</h1>';
   else
      echo "Please fill the email field out";
   
} else {
    
    $to = "email@mydomain.com";
    $subject = "Website Email";
    $email = $_REQUEST['email'] ;
    $message = $_REQUEST['message'] ;
    $headers = "From: $email";
    $sent = mail($to, $subject, $message, $headers) ;
    if ($sent) {
        print "";
    } else {
        print "We encountered an error sending your mail";
    }

}

?>
[code]

[/code]

Link to comment
Share on other sites

Sure... Here's the form with the script how I originally had it.  And thanks for your input...

 

<html>

<head>

</head>

<body bgcolor="#FFCC00"> <div>
		<font face="Arial" size="2">
		<table border="0" width="100%" id="table1" bgcolor="#FFFFFF">
			<tr>
				<td width="126">
				<table border="0" width="107%" id="table2">


					</tr>
				</table>
				</td>
				<td width="797" valign="top">

	<td> </td>
	<td width="434"> </td>
</tr>
</table>
<font face="Arial" size="2">
		<table border="0" width="67%" id="table4">
			<tr>
				<td> </td>
				<td width="434"><font face="Arial" size="2">
				Please place your email address here: 
				<input name="email" type="text">  <br>
				<br>
				Please write your message to here, and we <br>
				will contact you shortly:<br>
<textarea name="message" rows="2" cols="40"></textarea><br>
<input type="submit" value="Click here to send your email!">
<br><br>(If the text has cleared from the form fields, <br>your email has been sent successfully.  <br>Please do not reclick.)
		</font>
	<p align="left"><font face="Arial" size="2">

						</td>
			</tr>
</table>
		</font>
<p align="center"></p>
</form>
				<p align="center"> </p>
				<p></td>
			</tr>
		</table>
		</font></div>

	<?php
	if (isset($_POST['Submit']) &&
	    isset($_POST['email']) &&
	    trim($_POST['email'])  != '') { 
		echo '<h1>Thank you for filling out this form!</h1>';
		} else {


$to = "email@mydomain.com";
$subject = "Website Email";
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
$headers = "From: $email";
$sent = mail($to, $subject, $message, $headers) ;
if($sent)
{print ""; }
else
{print "We encountered an error sending your mail"; }
?>
<?php
}
?>
</body>
</html>

Link to comment
Share on other sites

You had quite a bit of missing stuff. You didn't have an opening form tag, and your submit button didn't have a name. Also, your PHP logic was a bit messed up. So try this and see if it is what you want

 

<html>
<head>
</head>

<body bgcolor="#FFCC00"> 
<div>
<font face="Arial" size="2">
<table border="0" width="100%" id="table1" bgcolor="#FFFFFF">
<tr>
<td width="126">
<table border="0" width="107%" id="table2">


</tr>
</table>
</td>
<td width="797" valign="top">

<td> 
</td>
<td width="434"> 
</td>
</tr>
</table>
<font face="Arial" size="2">
<form method="post">
<table border="0" width="67%" id="table4">
<tr>
<td> 
</td>
<td width="434"><font face="Arial" size="2">
Please place your email address here:
<input name="email" type="text"> 
<br>
<br>
Please write your message to here, and we <br>
will contact you shortly:<br>
<textarea name="message" rows="2" cols="40"></textarea><br>
<input type="submit" name="Submit" value="Click here to send your email!">
<br><br>(If the text has cleared from the form fields, <br>your email has been sent successfully.  <br>Please do not reclick.)
</font>
<p align="left"><font face="Arial" size="2">

</td>
</tr>
</table>
</font>
<p align="center"></p>
</form>
<p align="center"> 
</p>
<p></td>
</tr>
</table>
</font></div>

<?php

if (isset($_POST['Submit'])){

   if (isset($_POST['email']) && $_POST['email'] != ""){
    
       $to = "email@mydomain.com";
       $subject = "Website Email";
       $email = $_REQUEST['email'] ;
       $message = $_REQUEST['message'] ;
       $headers = "From: $email";
       $sent = mail($to, $subject, $message, $headers) ;
       
       if ($sent) {
           print "";
       } else {
           print "We encountered an error sending your mail";
       }

   } else {
      echo "You didn't fill in your email!";
   }
}

?>
</body>
</html>

 

Also note that any messages will appear at the BOTTOM of your script since your PHP code is at the bottom. I wasn't sure if you wanted it like that, so I left it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.