• Sign in to Neowin Faster!

    Create an account on Neowin to contribute and support the site.

  • 0
Sign in to follow this  

Make a simple PHP "required" field

Question

Mongrel    0

I've been recycling the same PHP script for a few years now for basic submission forms, which has been working fine. But I have a friend/client who needs to make one of the fields "required," as in "go back and fill that in." I've tried three very different solutions after spending three nights searching, and nothing has worked - everything ends up as a parsing error.

At the top right of this page is the existing form:

http://www.clearcreekconsulting.com/garnishment.html

This is the Sending.php file that initiates when Call Me is clicked. I need to make the Phone field a requirement before the field can be submitted.:

<html>
<head><br>
<title>Sending...</title>
</head>
<meta http-equiv="Refresh" content="1; URL=sent.html"> 
<?
//Text Boxes
$name = $_POST{"name"};
$phone = $_POST{"phone"};

//Dropdown Boxes
$liability = $_POST['liability'];
$callwhen = $_POST['callwhen'];

//Where you see the email change it to your email.
//Your may change  "Your sites comments" to anything you wish, this is the subject line
//Do not touch the rest

$to = "nospam@msn.com";
$from = $email;
$subject = "Free Consultation Request";
$message = "Name: $name
Liability: $liability\n
Phone: $phone
Best time to call: $callwhen\n";
$headers = "From: $from";
mail($to,$subject,$message,$headers);

?>

<style type="text/css">
<!--
.style1 {font-family: Arial, Helvetica, sans-serif}
.style7 {font-family: Arial, Helvetica, sans-serif;
	font-size: 12px;
}
-->
</style>
<title></title><span class="style1"><br>
<br>
</span>
</html>

Is there something about this method of scripting that would make a required field not work? Thanks a million in advance...

Share this post


Link to post
Share on other sites

9 answers to this question

Recommended Posts

  • 0
primexx    372

put this right after getting $phone

if (!isset($phone)) {header('Location:http://website.com/form.php?req=phone');}

and somewhere in the original form:

$error = isset($_GET['req']) ? $_GET['req'] : NULL;
if ('phone' == $error) {echo '<p>Please enter a phone number!</p>';}

Share this post


Link to post
Share on other sites
  • 0
zeroday    19

Also do some validation using Javascript, it saves having to submit it, check for errors, redirect, and submit again, etc.

Share this post


Link to post
Share on other sites
  • 0
Mongrel    0

Primexx - first, thank you for responding! Second, should your code for the form side have some sort of tag around it? Anywhere I place it in the form code, it appears as text on the page. Dreamweaver doesn't seem to recogize (color colde) it as valid.

This is my form code:

<form action="sending.php" method="post" target="_parent" class="freeConsult" id="consult">

<p>Name<input name="name" type="text" id="name" size="18" />

Phone<input name="phone" type="text" id="phone" size="18" />

Liability<br> <select name="liability" id="liability">
	<option value="10k - 50k">$10,000 - 50,000</option>
	<option value="50k - 100k">$50,000 - 100,000</option>
	<option value="100k - 500k">$100,000 - 500,000</option>
	<option value="Over 500k">Over $500,000</option>
	</select>

When to Call
	<select name="callwhen" id="callwhen">
	<option value="Morning">Morning</option>
	<option value="Afternoon">Afternoon</option>
	<option value="Evening">Evening</option>
	<option value="ASAP">ASAP</option>
	</select>
<p>
<label>
<div align="center">
<input type="submit" name="Submit" id="Submit" value="CALL ME!" />
<br />
</label>
</form>

Share this post


Link to post
Share on other sites
  • 0
Gangsta    1

Mongrel: is it inside the <?php ?> tags?

Share this post


Link to post
Share on other sites
  • 0
primexx    372

yea it needs to be a php file first of all, and the code on the form side needs to be in php tags. sry i didn't mention that part.

and javascript validation is a good idea too, but keep the php code there anyways in case javascript isn't enabled.

Share this post


Link to post
Share on other sites
  • 0
Mongrel    0

Great, wrapping it in <?php ?> tags did the trick, but the form is still sending with a blank phone field.

If this form exists on every page of the site, where should the following bit of code point to? Back to itself (sending.php), or should I make a catch-all form error page? That's what I tried but it doesn't seem to be catching it... (also I noticed your example is a .php page, but all mine except the Sending function are .html - is that a problem?)

if (!isset($phone)) {header('Location:http://website.com/form.php?req=phone');}

Edited by Mongrel

Share this post


Link to post
Share on other sites
  • 0
bolerodan    1

Im more of a Perl coder, but i have dabbled in PHP before so im not sure how much a help i can be. But could you not do something along the lines of, check to see if the phone variable is set.

$error = "";
$phone = $_POST['phone'];
if(!$phone) 
{ 
   $error .= "The phone field is required"; 
}

then inside your php script, output some html and echo the $error variable. Then do a check before you call the mail() function. If $error is NOT empty, do not run and display the $error variable, else run the mail() call and display a success message

I'm not too sure, but you could try something like that...just a suggestion, possibly not the best idea, but an idea non the less

overall i came up with this and it seems to work with a form box being submitted.

&lt;html&gt;
&lt;body&gt;
&lt;?php
$error= "";
$phone = $_POST['phone']; #grab the phone field

if(!$phone) #if the variable is empty, put the specific error into the error variable
{
		$error .= "Phone field is required";
}

echo "$error\n"; #output the error message

if (!$error) #if the error variable is empty, everything is good, continue on.do what you want.
{ 
	  #do some other calls such
	  # mail( $blah, $blah, $blah)
	  echo " The phone # is $phone"; 
}
?&gt;
&lt;/body&gt;
&lt;/html&gt;

Now i know you can do better control over error messages, when to display them, and some better logic. For example, i wouldnt normally have echo "$error\n" be displayed always, even when it is empty. I would put some other checking in there.. but this is just a quick rough draft to give you an idea. Seems to work for me

Edited by bolerodan

Share this post


Link to post
Share on other sites
  • 0
rpgfan    0

Yeah, checking if $phone is set is a good idea, though you would use if (!isset($phone)) instead of checking whether $phone = 0 or NULL or FALSE with !$phone (that's probably the annoying part about some languages like PHP, but it is useful at times).

I hope bolerodan's reply helped!

Share this post


Link to post
Share on other sites
  • 0
primexx    372
Great, wrapping it in <?php ?> tags did the trick, but the form is still sending with a blank phone field.

If this form exists on every page of the site, where should the following bit of code point to? Back to itself (sending.php), or should I make a catch-all form error page? That's what I tried but it doesn't seem to be catching it... (also I noticed your example is a .php page, but all mine except the Sending function are .html - is that a problem?)

if (!isset($phone)) {header('Location:http://website.com/form.php?req=phone');}

I devised a method of tracking which page a request came from a year or so ago, when i had multiple pages using the same mail script. Basically you have a hidden field in your form which contains the document name as a value. and you take that value and prepend it to the extension and etc. so something like $return_addres = $page.'.php?blah=bleh'; and having html pages would be a problem because you won't be able to use any server side scripting in them. you could get around it though by treating html pages as php on the server side.

Share this post


Link to post
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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.