Jump to content

[SOLVED] Required Field In Form


kerrya

Recommended Posts

 

What I want to do is make sure that the name and the email address are completed before the form is submitted. This is my code for the form. How do i do it?

 

<!-- <form>  -->

<table border="1">

<tr><td>Your Name </td><td><input type="text" name="Name" /></td></tr>

<tr><td>Email Address</td><td><input type="text" name="Email" /></td></tr>

<tr><td colspan="2">

<input type="submit" value="Submit for Quote" /></td></tr>

</table>

 

</form>

 

Link to comment
Share on other sites

If you want to make sure before it's submitted you'll have to use JavaScript. Either way you will need to do PHP validation (Because people can turn off JavaScript). Here's an example of how to do it:

 

if(!isset($_POST['Name']) || !isset($_POST['Email']))
{
     echo "Required fields were left blank";
}
else
{
     //Process as usual.
}

 

isset() Use this to check if the variables are set and aren't NULL.

Link to comment
Share on other sites

Well, in the code you posted you didn't even have an action set for the form (the action attribute for the form tells the form where to submit the data too.) Additionally you need to specify the method, which in your case would be post.

 

Use this:

 

<form action="somefile.php" method="post">
<table border="1">
<tr><td>Your Name </td><td><input type="text" name="Name" /></td></tr>
<tr><td>Email Address</td><td><input type="text" name="Email" /></td></tr>
<tr><td colspan="2">
<input type="submit" value="Submit for Quote" /></td></tr>
</table>
</form>

 

Then inside "somefile.php" (Use a different filename if you want, just make sure to edit that in the form then).

 

somefile.php:

 

if(!isset($_POST['Name']) || !isset($_POST['Email']))
{
     echo "Required fields were left blank";
}
else
{
     echo "Name: {$_POST['Name']} <br />\n Email: {$_POST['Email']}";
}

Link to comment
Share on other sites

I am getting a little confused now because i tried something different and now have this

 

My form page

<form method="post" action="xxxxxx/Scripts/ptrquoteemail.php" enctype="multipart/form-data">

            <div>

<input type="hidden" name="redirect" value="http://xxxxxx/quotethankyou.php" /></div>

<!-- <form>  -->

<table border="1">

<tr><td>Your Name </td><td><input type="text" name="Name" /></td></tr>

<tr><td>Email Address</td><td><input type="text" name="Email" /></td></tr>

  <tr><td colspan="2">

  <input type="submit" value="Submit for Quote" /></td></tr>

</table>

 

On the ptrquoteemail page I have

<?php

require '/home/xxxxxxxxxxxxx/Includes/connect.php';

 

/* Assign all variables passed from the form */

$client = mysql_real_escape_string($_POST['Name']);

$email = mysql_real_escape_string($_POST['Email']);

 

/* Sending email with information to us */

$to = "kerry@xxxxxxx.net.nz";

$subject = "Quote";

$random_hash = md5(date('r', time()));

$headers = 'From: kerry.nz';

$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";

ob_start();

?>

--PHP-alt-<?php echo $random_hash; ?>

Content-Type: text/html; charset="iso-8859-1"

Content-Transfer-Encoding: 7bit

 

<!-- Email Content -->

<? include '/home/xxxxxxxxx/Includes/ptrquote.inc'; ?>

<!-- End of Email Content -->

 

--PHP-alt-<?php echo $random_hash; ?>--

<?php

$html = ob_get_contents();

ob_end_clean();

$body = $html;

mail($to, $subject, $body, $headers);

 

/* Once Complete return to............*/

header("Location: https://xxxxxx/quotethankyou.php");

?>

 

The ptrquote.inc page has

<table width="100%" cellspacing="0">

<tr bgcolor="#EDEEEA">

</table>

<table width="100%" height="100%" cellpadding="50">

<tr>

<td>This quote was submitted by <? echo $client ?> at <? echo date('r', time()); ?><br/><br/>

Name: <? echo $client;?><br/><br/>

Email: <? echo $email;?><br/><br/>

 

 

I know all this might sound silly but I am at a lost where to go with ensuring the name and email are entered.

Link to comment
Share on other sites

Firstly, for future reference please post all code either inside of [ code ] or [ php ] (without the spaces), just makes it easier to read. :)

 

Now, here's how you'd apply what I just posted:

 

<?php
require '/home/xxxxxxxxxxxxx/Includes/connect.php';

if(!isset($_POST['Name']) || !isset($_POST['Email']))
{
     echo 'Required fields missing';
     exit;
}

/* Assign all variables passed from the form */
$client = mysql_real_escape_string($_POST['Name']);
$email = mysql_real_escape_string($_POST['Email']);

/* Sending email with information to us */
$to = "kerry@xxxxxxx.net.nz";
$subject = "Quote";
$random_hash = md5(date('r', time())); 
$headers = 'From: kerry.nz';
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\""; 
ob_start();
?>

 

Usually using exit like that is a sign of bad logic and planning, but in less complicated situations like this it doesn't really matter.

Link to comment
Share on other sites

My Mistake  :-\ Use a empty() to make sure it has a value (And not that it was just submitted like you'd do with isset()).

 

<?php
require '/home/xxxxxxxxxxxxx/Includes/connect.php';

if(empty($_POST['Name']) || empty($_POST['Email']))
{
     echo 'Required fields missing';
     exit;
}

/* Assign all variables passed from the form */
$client = mysql_real_escape_string($_POST['Name']);
$email = mysql_real_escape_string($_POST['Email']);

/* Sending email with information to us */
$to = "kerry@xxxxxxx.net.nz";
$subject = "Quote";
$random_hash = md5(date('r', time())); 
$headers = 'From: kerry.nz';
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\""; 
ob_start();
?>

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.