Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

PHP: Make a field mandatory?

Posted on 2008-06-19
12
1,242 Views
Last Modified: 2013-12-13
Regarding code below:

I'd like to make at least 1 of the fields mandatory - but can't figure out (from googling) how to do it.

I'd also like to add another field 'description' that will become the SUBJECT of the email that gets sent after the user presses submit.

Many thanks.
html:
<form method="post" action="sendmail.php"><br>
							
                                             <table width="580" border="0" align="center">
                          <tr>
                            <td width="296"><div align="right">Name:</div></td>
                            <td width="268"><div align="left">
                              <input name="name" type="text" /></td
                          ></tr>
                          <tr>
                            <td><div align="right">Email Address:</div></td>
                            <td><div align="left"><input name="email" type="text" /></td>
                          </tr>
                          <tr>
                            <td><div align="right">ISS Username (if known):</div></td>
                            <td><div align="left"><input name="username" type="text" /></td>
                          </tr>
                          <tr>
                            <td><div align="right">Telephone Number:</div></td>
                            <td><div align="left"><input name="phone" type="text" /></td>
                          </tr>
                          <tr>
                            <td><div align="right">Location (e.g room number):</div></td>
                            <td><div align="left"><input name="location" type="text" /></td>
                          </tr>
                          <tr>
                            <td><div align="right">Please provide details of the problem, or leave any message here.<br>
                              <br>
                            Press 'submit' when done.</div></td>
                            <td><div align="left"><textarea name="message" rows="10" cols="40"></textarea></td>
                          </tr>
                        </table><br>
                        <input type="submit" VALUE="Submit" align="right"/><br>
						</form>
 
 
 
Php:
<?php
   $email = $_REQUEST['email'] ;
   $name = $_REQUEST['name'] ;
   $username = $_REQUEST['username'] ;
   $phone = $_REQUEST['phone'] ;
   $location = $_REQUEST['location'] ;
   
   $message = $_REQUEST['message'] ;
   $message .= "\nUsername****:$username\n";
   $message .= "\nContact Number****:$phone\n";
   $message .= "\nEmail Address****:$email\n";
   $message .= "\nLocation Details****:$location\n";
   
   
mail( "sample@leeds.ac.uk", "New helpdesk email", $message, "From: $name <$email>" ); 
   header( "Location: http://www.leeds.ac.uk/iss/contact_test/thankyou.html" );
?>

Open in new window

0
Comment
Question by:callmecheez
  • 4
  • 3
  • 3
12 Comments
 
LVL 49

Expert Comment

by:Roonaan
ID: 21820396
Hello callmecheez,

if(empty($_POST['email'])) {
  exit('You did not fill the email field. Please go back and correct the form');
}


Regards,

Roonaan
0
 
LVL 48

Expert Comment

by:hernst42
ID: 21820404
YOu can use Javascript to do the check like

<script>
function checkForm(f) {
   if (f.name.value == '' && f.email.value == '' && f.phone.value == '' ...) {
      alert('please fillout at least on field');
      return false;
   }
  return true;
}
</script>

Then use in the form -tag:

<form method="post" action="sendmail.php" onsubmit="return checkForm(this);">


for the subject in php use:
mail( "sample@leeds.ac.uk", $_REQUEST['subject'], $message, "From: $name <$email>" );
and add the element to your form.
0
 
LVL 3

Author Comment

by:callmecheez
ID: 21820406
Thanks Roonaan, where should I add that code?

Also any ideas on this bit:
"I'd also like to add another field 'description' that will become the SUBJECT of the email that gets sent after the user presses submit."

Cheers

0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 49

Expert Comment

by:Roonaan
ID: 21820426
You could have a single php file for both form and sending like this:
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST') {
	 $errors = 0;
	 
   $email = $_REQUEST['email'] ;
   $name = $_REQUEST['name'] ;
   $username = $_REQUEST['username'] ;
   $phone = $_REQUEST['phone'] ;
   $location = $_REQUEST['location'] ;
   $description = $_REQUEST['description'];
   
   
   if(empty($email)) {
     $errors++; //
     echo '<div>Please make sure you fill out the email field correctly</div>';
   }
   
   if(empty($description)) {
     $errors++; //
     echo '<div>Please make sure you fill out the problem description field correctly</div>';
   }
 
   if($errors == 0) {  
   	$message = $_REQUEST['message'] ;
   	$message .= "\nUsername****:$username\n";
   	$message .= "\nContact Number****:$phone\n";
   	$message .= "\nEmail Address****:$email\n";
   	$message .= "\nLocation Details****:$location\n";
   	
	 	mail( "sample@leeds.ac.uk", "New helpdesk email: $description", $message, "From: $name <$email>" ); 
	 	
   	header( "Location: http://www.leeds.ac.uk/iss/contact_test/thankyou.html" );
   }
}
?>
<form method="post" action="sendmail.php"><br>
                                                        
                     <table width="580" border="0" align="center">
  <tr>
    <td width="296"><div align="right">Name:</div></td>
    <td width="268"><div align="left">
      <input name="name" type="text" value="<?php echo htmlspecialchars(@$_REQUEST['name']);?>" /></td
  ></tr>
  <tr>
    <td><div align="right">Email Address:</div></td>
    <td><div align="left"><input name="email" type="text" value="<?php echo htmlspecialchars(@$_REQUEST['email']);?>"/></td>
  </tr>
  <tr>
    <td><div align="right">ISS Username (if known):</div></td>
    <td><div align="left"><input name="username" type="text" value="<?php echo htmlspecialchars(@$_REQUEST['username']);?>"/></td>
  </tr>
  <tr>
    <td><div align="right">Telephone Number:</div></td>
    <td><div align="left"><input name="phone" type="text" value="<?php echo htmlspecialchars(@$_REQUEST['phone']);?>"/></td>
  </tr>
  <tr>
    <td><div align="right">Location (e.g room number):</div></td>
    <td><div align="left"><input name="location" type="text" value="<?php echo htmlspecialchars(@$_REQUEST['location']);?>"/></td>
  </tr>
  <tr>
    <td><div align="right">Short problem description:</div></td>
    <td><div align="left"><input name="description" type="text" value="<?php echo htmlspecialchars(@$_REQUEST['description']);?>"/></td>
  </tr>
  <tr>
    <td><div align="right">Please provide details of the problem, or leave any message here.<br>
      <br>
    Press 'submit' when done.</div></td>
    <td><div align="left"><textarea name="message" rows="10" cols="40" ><?php echo htmlspecialchars(@$_REQUEST['message']);?></textarea></td>
  </tr>
</table><br>
<input type="submit" VALUE="Submit" align="right"/><br>
                        </form>

Open in new window

0
 
LVL 3

Author Comment

by:callmecheez
ID: 21820427
hernst42- again, thanks for the reply. I'm a totally beginner at this so you'll have to tell me where i'd put that java script!

Many thanks
0
 
LVL 3

Author Comment

by:callmecheez
ID: 21820544
Getting this when I test it:

"Please provide your ISS username (the login ID you use for logging into computers / email)

Warning: Cannot modify header information - headers already sent by (output started at /export/services/isswww/WWW/contact_test/sendmail.php:10) in /export/services/isswww/WWW/contact_test/sendmail.php on line 23"
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 21820558
What's on line 10?

Make sure that the validation part is at the topmost of your script and that html starts lower.
0
 
LVL 48

Expert Comment

by:hernst42
ID: 21820571
replace your formtag with the code snuppet and add you fields where the ... are the logic should be easy to understand.

 
<script>
function checkForm(f) {
   if (f.name.value == '' && f.email.value == '' && f.phone.value == '' ...) {
      alert('please fillout at least on field');
      return false;
   }
  return true;
}
</script>
<form method="post" action="sendmail.php" onsubmit="return checkForm(this);">

Open in new window

0
 
LVL 3

Author Comment

by:callmecheez
ID: 21820685
Sorry to be a pain guys, still getting an error:

"Parse error: parse error, unexpected '}' in /export/services/isswww/WWW/contact_test/sendmail.php on line 30"

Can't work out why. .?


sendmail.php:
<?php
   $email = $_REQUEST['email'] ;
   $description = $_REQUEST['description'] ;
   $name = $_REQUEST['name'] ;
   $username = $_REQUEST['username'] ;
   $phone = $_REQUEST['phone'] ;
   $summary = $_REQUEST['summary'] ;
 
   
   if(empty($email)) {
     $errors++; //
     echo '<div>Please make sure you fill out the email field correctly</div>';
   }
   
   if(empty($description)) {
     $errors++; //
     echo '<div>Please make sure you fill out the problem description field correctly</div>';
   }
 
if($errors == 0)   
   $message = $_REQUEST['message'] ;
   $message .= "\nUsername****:$username\n";
   $message .= "\nContact Number****:$phone\n";
   $message .= "\nEmail Address****:$email\n";
 
   
   
		mail( "helpdesk@leeds.ac.uk", $_REQUEST['summary'], $message, "From: $name <$email>" ); 
   header( "Location: http://www.leeds.ac.uk/iss/contact_test/thankyou.html" );
   }
 
?>
 
 
 
 
- - - - - - - - - - - - -
 
 
 
 
 
 
 
<form method="post" action="sendmail.php" onsubmit="return checkForm(this);">
                          <form method="post" action="sendmail.php"><br>
							
                                             <table width="580" border="0" align="center">
                          <tr>
                            <td width="296"><div align="right">Name:</div></td>
                            <td width="268"><div align="left">
                              <input name="name" type="text" /></td
                          ></tr>
                          <tr>
                            <td><div align="right">Email Address:</div></td>
                            <td><div align="left"><input name="email" type="text" /></td>
                          </tr>
                          <tr>
                            <td><div align="right">ISS Username (if known):</div></td>
                            <td><div align="left"><input name="username" type="text" /></td>
                          </tr>
                          <tr>
                            <td><div align="right">Telephone Number:</div></td>
                            <td><div align="left"><input name="phone" type="text" /></td>
                          </tr>
                          <tr>
                            <td><div align="right">Subject:</div></td>
                            <td><div align="left"><input name="subject" type="text" /></td>
                          </tr>
                          <tr>
                            <td><div align="right">Please provide details of the problem, or leave any message here.<br>
                              <br>
                            Press 'submit' when done.</div></td>
                            <td><div align="left"><textarea name="message" rows="10" cols="40"></textarea></td>
                          </tr>
                        </table><br>
                        <input type="submit" VALUE="Submit" align="right"/><br>
						</form>

Open in new window

0
 
LVL 48

Accepted Solution

by:
hernst42 earned 500 total points
ID: 21821422
You get the parse error because of

if($errors == 0)  
is not
if($errors == 0)   {

opening barce missing.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question