PHP Form Submission
Moderator: General Moderators
PHP Form Submission
ok heres my code http://www.mellonhosting.com/art/enter2.zip
my problem is how to make the form not submit if a field is left blank or returns an error
thanks for your help.
my problem is how to make the form not submit if a field is left blank or returns an error
thanks for your help.
-
alex.barylski
- DevNet Evangelist
- Posts: 6267
- Joined: Tue Dec 21, 2004 5:00 pm
- Location: Winnipeg
Well two ways:
1) Client side validation
2) Server side validation
and possibly a hybrid of both using AJAX...
My CMS (shameless plug) has a little library I designed for making client side validation as easy as pie...but I've since dropped it in favour for more standards compliant server side validation. My library used attributes to specify details, which makes it choke on validation services.
http://svn.sourceforge.net/viewvc/texoc ... iew=markup
Cheers
1) Client side validation
2) Server side validation
and possibly a hybrid of both using AJAX...
My CMS (shameless plug) has a little library I designed for making client side validation as easy as pie...but I've since dropped it in favour for more standards compliant server side validation. My library used attributes to specify details, which makes it choke on validation services.
http://svn.sourceforge.net/viewvc/texoc ... iew=markup
Cheers
i think i fergot to say that i am very very new to php
and would like it to be server side
i tried using if($_POST['submit_button'] && $clear==true)
then making it so every time i field would be blank or return and error have it
$clear=false
but that didnt work like i wanted it to
it wouldnt let the submission if fields were filled correctly or blank and when
we changed it to if($_POST['submit_button'] && $clear=true) it allowed submit even if they were left blank
so im going to need a bit of explanation and examples if possible
and would like it to be server side
i tried using if($_POST['submit_button'] && $clear==true)
then making it so every time i field would be blank or return and error have it
$clear=false
but that didnt work like i wanted it to
it wouldnt let the submission if fields were filled correctly or blank and when
we changed it to if($_POST['submit_button'] && $clear=true) it allowed submit even if they were left blank
so im going to need a bit of explanation and examples if possible
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
Making things a little complicated for Serik there Hockey.
For starters server-side validation is not optional but client-side validation is.
Here's a rough example of the validation i think you are after:
For starters server-side validation is not optional but client-side validation is.
Here's a rough example of the validation i think you are after:
Code: Select all
if (empty($_POST['fieldName'])) {
echo 'Don\'t leave fieldName blank. It makes me sad ';
} else {
// do stuff possibly involving a file or database
}
// output formi dont need validation, i have it all set look at the code http://www.mellonhosting.com/art/enter2.php
its not letting the form submit even if all fields are filled
its not letting the form submit even if all fields are filled
i have validation i have it all set look at the code http://www.mellonhosting.com/art/enter2.php
its not letting the form submit even if all fields are filled ( it wont return the $thanks page or generate the email
and on the $post Submit_buton && $clear==true if thats changed to $clear=true it will let the form through without all fields being filled
its not letting the form submit even if all fields are filled ( it wont return the $thanks page or generate the email
and on the $post Submit_buton && $clear==true if thats changed to $clear=true it will let the form through without all fields being filled
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
Enter something into "Marketing Makover" and click submit. You'll soon see that it is not repopulated.
And use this for your zip code validation:That's from ZF so I guess its pretty realiable.
And use this for your zip code validation:
Code: Select all
/**
* Returns value if it is a valid US ZIP, FALSE otherwise.
*
* @param mixed $value
* @return mixed
*/
public static function isZip($value)
{
return (bool) preg_match('/(^\d{5}$)|(^\d{5}-\d{4}$)/', $value);
}- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
-
SidewinderX
- Forum Contributor
- Posts: 407
- Joined: Fri Jul 16, 2004 9:04 pm
- Location: NY
im not sure you can do exactley what you want without using java script
what i would do is have the user enter the values and then submit the form, then on the next page have a validation thing and if all the values arnt filled in have something echo like "The fields you entered are not valid" and have it redirect back in like 5 seconds or something
what i would do is have the user enter the values and then submit the form, then on the next page have a validation thing and if all the values arnt filled in have something echo like "The fields you entered are not valid" and have it redirect back in like 5 seconds or something
feyd | Please use
feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
interesting the orig i was working with seems to have been lost this is what i have but i think it still needs the validation but have at er... im lostCode: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head><?
// only validate form when form is submitted
if(isset($submit_button))
{
$error_msg='';
$error_msg_name='';
$error_msg_org='';
$error_msg_add='';
$error_msg_city='';
$error_msg_state='';
$error_msg_zip='';
$error_msg_area='';
$error_msg_phone='';
$error_msg_email='';
$error_msg_email2='';
$error_msg_makeover='';
if(trim($first_name_input)=='') {
$error_msg_name.="Please Input Your Name<br>";
}
if(trim($organization_input)=='') {
$error_msg_org.="Please enter your Organization<br>";
}
if(trim($address_input)=='') {
$error_msg_add.="Please enter your Address<br>";
}
if(trim($city_input)=='') {
$error_msg_city.="Please enter your City<br>";
}
if(trim($state_input)=='' || strlen(trim($zip_input)) <2) {
if(!ereg("^[A-Za-z]", $state_input)) $error_msg_state.="Please enter your State<br>";
}
if(trim($zip_input)=='' || strlen(trim($zip_input)) <5) {
if(!ereg("^[0-9]", $zip_input)) $error_msg_zip.="Please Input Your Zip Code<br>";
}
if(trim($area_input)=='' || strlen(trim($area_input)) <3) {
if(!ereg("^[0-9]", $area_input)) $error_msg_area.="Please Input Your Area Code<br>";
}
if(trim($phone_input)=='' || strlen(trim($phone_input)) <3){
if(!ereg("^[0-9]", $phone_input)) $error_msg_phone.="Please Input Your Phone Number<br>";
}
if(trim($phone2_input)=='' || strlen(trim($phone2_input)) <4) {
if(!ereg("^[0-9]", $phone2_input)) $error_msg_phone.="Please Input Your Phone Number<br>";
}
if(trim($email_input)=='') {
$error_msg_email.="Please enter an Email Address<br>";
}
else {
// check if email is a valid address in this format username@domain.com
if(!ereg("[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]", $email_input)) $error_msg_email2.="Please enter a valid email address<br>";
}
if(trim($makeover_input)=='') {
$error_msg_makeover.="Please enter the reason you need a Marketing Makover<br>";
}
// display error message if any, if not, proceed to other processing
if($error_msg==''){
// other process here
} else {
echo "<font color=red>$error_msg</font>";
}
}
?>
<?php
//Your name
$recipientname = "first_name_input";
//Your email
$recipientemail = "angeldevoidv3@gmail.com";
//send Autoresponse?
$autoresponse = "yes";
//Subject of Autoresponse
$autosubject = "Mellon Technology Service Marketing Makover";
//Autoresponse message
$automessage ='Thank you for your entry. Contest ends Nov. 1st
Thank you,
Mellon Technology Service
http://www.mellontech.com';
//Thank you after submit
$thanks = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Contest: Corporate Identity Marketing Package by Mellon Technology Service, Olean, New York</title>
<meta name="description" content="Mellon Technology Service is an agency in Olean, New York offering creative and computer services including web site design, graphic design, corporate identity, integrated marketing communications, programming, networking, PC and Mac support and repairs." />
<meta name="keywords" content="Mellon, technology, service, Mellon Technology Service, Olean, New York, NY, web site design, web designer, web designers, graphic design, graphics, graphic artist, graphic artists, flash design, flash designer, flash, internet, search engine optimization, HTML, PHP, CSS, web, site, design, designs, corporate identity, marketing, integrated marketing communications, creative, communications, communication, programming, networking" />
<meta name="ABSTRACT" content="Mellon Technology Service is an agency in Olean, New York offering creative and computer services including web site design, graphic design, corporate identity, integrated marketing communications, programming, networking, PC and Mac support and repairs." />
<meta name="REVISIT-AFTER" content="19 Days" />
<meta name="DISTRIBUTION" content="Global" />
<meta name="ROBOTS" content="INDEX, FOLLOW" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link href="mellontech.css" rel="stylesheet" type="text/css">
<style type="text/css">
<!--
.style2 {font-size: 14px}
.style5 {font-size: 16px}
.style6 {font-size: 12px; }
-->
</style>
</head>
<body>
<table width="750" border="0" cellpadding="0" cellspacing="0" id="wrapper">
<tr>
<td width="9" rowspan="3" valign="top" class="silverbar"><img src="../images/silverbar.jpg" width="9px" height="356px"></td>
<td valign="top" class="tablebg">
<div id="masthead">
<div id="logo"><img src="../images/mellontech_logo2.jpg" width="179px" height="181px" alt="Mellon Technology Service" /></div>
<div id="flash"></div>
<div class="style2" id="mainNav"></div>
</div>
<div id="content">
<center>
<p align="center" class="style5"> </p>
<p align="center" class="style5"> </p>
<p align="center" class="style5"><strong>Thank you for entering. </strong></p>
<p align="center" class="style5"><strong>The Marketing Makeover Contest ends November 1st. </strong></p>
<p align="center" class="style5"><strong>We will contact you if you are a winner. </strong></p>
<p align="center" class="style5"><strong>Good luck! </strong></p>
<h1> </h1>
<p> </p>
</center>
</div>
<div id="leftmenubar">
<div id="subNav">
<ul>
<li>
<li><a href="http://contest.mellontech.com">Home</a></li>
<li><a href="http://contest.mellontech.com/examples.html">Corporate ID Examples</a></li>
<li><a target="_blank" href="http://www.whatisimc.com">What Is IMC?</a></li>
<li><a href="http://contest.mellontech.com/enter.php">Enter to Win </a></li>
<li><a href="http://contest.mellontech.com/terms.html">Terms & Conditions</a></li>
</li>
<li><a href="http://www.mellontech.com/tina.html"></a></li>
</ul>
</div>
<div align="center"><img src="../images/ReliabilitySeal.GIF" width="90px" height="32px" alt="BBB OnLine Reliability Program" /> </div>
</div> </td>
<td width="9" rowspan="3" valign="top" class="silverbar"><img src="../images/silverbar.jpg" width="9" height="356"></td>
</tr>
<tr valign="top">
<td colspan="2" class="footercell">
<div id="footer">Copyright © 2006 Mellon Technology Service</div> </td>
</tr>
</table>
</body>
</html> ';
//close php
?><
<title>ENTER: Corporate Identity Marketing Package Contest by Mellon Technology Service, Olean, New York</title>
<meta name="description" content="Mellon Technology Service is an agency in Olean, New York offering creative and computer services including web site design, graphic design, corporate identity, integrated marketing communications, programming, networking, PC and Mac support and repairs." />
<meta name="keywords" content="Mellon, technology, service, Mellon Technology Service, Olean, New York, NY, web site design, web designer, web designers, graphic design, graphics, graphic artist, graphic artists, flash design, flash designer, flash, internet, search engine optimization, HTML, PHP, CSS, web, site, design, designs, corporate identity, marketing, integrated marketing communications, creative, communications, communication, programming, networking" />
<meta name="ABSTRACT" content="Mellon Technology Service is an agency in Olean, New York offering creative and computer services including web site design, graphic design, corporate identity, integrated marketing communications, programming, networking, PC and Mac support and repairs." />
<meta name="REVISIT-AFTER" content="19 Days" />
<meta name="DISTRIBUTION" content="Global" />
<meta name="ROBOTS" content="INDEX, FOLLOW" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link href="../My%20Documents/web/form/test/mellontech.css" rel="stylesheet" type="text/css">
<style type="text/css">
<!--
.style2 {font-size: 14px}
.style3 {font-size: 16px}
-->
</style>
</head>
<body>
<?php
if($_POST['submit_button'])
{
$name1 = $HTTP_POST_VARS['first_name_input'];
$name2 = $HTTP_POST_VARS['organization_input'];
$email = $HTTP_POST_VARS['email_input'];
$address = $HTTP_POST_VARS['address_input'];
$address2 = $HTTP_POST_VARS['address2_input'];
$state = $HTTP_POST_VARS['state_input'];
$city = $HTTP_POST_VARS['city_input'];
$zipcode= $HTTP_POST_VARS['zip_input'];
$area= $HTTP_POST_VARS['area_input'];
$farea= $HTTP_POST_VARS['faxarea_input'];
$prefix=$HTTP_POST_VARS['phone_input'];
$number=$HTTP_POST_VARS['phone2_input'];
$fprefix=$HTTP_POST_VARS['fax_input'];
$fnumber=$HTTP_POST_VARS['fax2_input'];
$makeover=$HTTP_POST_VARS['makeover_input'];
$browser = $HTTP_USER_AGENT;
$ip = $REMOTE_ADDR;
//format message
$message = "Online Response for $name1 - $name2:
Name= $name1
Organization= $name2
Address= $address
$city
$state
$zipcode
Phone= ($area) $prefix-$number
Email= $email
Why we need a Marketing Makeover= $makeover
---------------------------------------
browser: $browser
IP: $IP";
//send mail and success message
//Subject of email to you
$subject = "Contest Submission for $name1 - $name2";
mail($recipientemail, "$subject", "$message", "From: $name1 $name2 <$email>");
if($autoresponse=="yes")
{
$autosubject = $autosubject;
$automessage = $automessage;
mail($email, "$autosubject", "$automessage", "From:Company or Persons Name<$recipientemail>");
}
echo "$thanks";
}
else
{
?>
<table width="750" border="0" cellpadding="0" cellspacing="0" id="wrapper">
<tr>
<td width="9" rowspan="3" valign="top" class="silverbar"><img src="../My%20Documents/web/form/images/silverbar.jpg" width="9px" height="356px"></td>
<td valign="top" class="tablebg">
<div id="masthead">
<div id="logo"><img src="../My%20Documents/web/form/images/mellontech_logo2.jpg" width="179px" height="181px" alt="Mellon Technology Service" /></div>
<div id="flash"> </div>
<div class="style2" id="mainNav">
<ul>
<li>
<p style="text-align: center"><a href="#" class="style3">Enter to Win! </a></li>
</ul>
</div>
</div>
<div id="content">
<center>
<h1>Marketing Makeover By Mellon Technology Service! </h1>
<form action="" method="post" name="form1" id="form1" onSubmit="return CheckRequiredFields()">
<p>Name:<input type="text" name="first_name_input" size="50" value="<? echo $first_name_input; ?>"><br>
<?php echo "<font color=red>$error_msg_name</font>";?></p>
<p>Organization: <input type="text" name="organization_input" size="40" value="<? echo $organization_input; ?>"><br>
<?php echo "<font color=red>$error_msg_org</font>"; ?></p>
<p>Street Address: <input type="text" name="address_input" size="37" value="<? echo $address_input; ?>"><br>
<?php echo "<font color=red>$error_msg_add</font>";?></p>
<p>City: <input type="text" name="city_input" size="20" value="<? echo $city_input; ?>">
State:
<input type="text" name="state_input" size="2" maxlength="2" value="<? echo $state_input; ?>"><br>
<?php echo "<font color=red>$error_msg_city</font>"; ?>
<?php echo "<font color=red>$error_msg_state</font>";?> </p>
<p>Zip Code: <input type="text" name="zip_input" size="5" maxlength="5" value="<? echo $zip_input; ?>">
Phone #:
<input type="text" name="area_input" maxlength="3" size="3" value="<? echo $area_input; ?>">-
<input type="text" name="phone_input" maxlength="3" size="3" value="<? echo $phone_input; ?>">-
<input type="text" name="phone2_input" maxlength="4" size="4" value="<? echo $phone2_input; ?>"><br>
<?php echo "<font color=red>$error_msg_zip</font>";?>
<?php echo "<font color=red>$error_msg_area</font>";echo "<font color=red>$error_msg_phone</font>";?></p>
<p>Email: <input type="text" name="email_input" size="50" value="<? echo $email_input; ?>"><br>
<?php echo "<font color=red>$error_msg_email</font>"; echo"<font color=red>$error_msg_email2</font>";?></p>
<p>Why do you need a marketing makeover?</p>
<p><textarea rows="7" name="makeover_input" cols="32" value="<? echo $makeover_input; ?>"></textarea><br>
<?php echo "<font color=red>$error_msg_makeover</font>";?></p>
<p> <input type="submit" value=" Submit " name="submit_button"><input type="reset" value="Reset" name="B2"></p>
</form>
<p align="center"><strong>You may also enter by submitting this information to: </strong></p>
<p align="center"><strong>1606 West State Street, Olean, NY 14760</strong></p>
<p align="center"><strong>716-372-5996 or toll free at 888-372-5996</strong></p>
<p align="center"><strong> e-mail: <a href="mailto:info@mellontech.com">info@mellontech.com</a></strong></p>
<p align="center"> </p>
<p align="center"> </p>
<p> </p>
</center>
</div>
<div id="leftmenubar">
<div id="subNav">
<ul>
<li><a href="http://contest.mellontech.com">Home</a></li>
<li><a href="http://contest.mellontech.com/examples.html">Corporate ID Examples</a></li>
<li><a target="_blank" href="http://www.whatisimc.com">What Is IMC?</a></li>
<li><a href="http://contest.mellontech.com/enter.html">Enter to Win </a></li>
<li><a href="http://contest.mellontech.com/terms.html">Terms & Conditions</a></li>
</ul>
</div>
<div align="center"><img src="../My%20Documents/web/form/images/ReliabilitySeal.GIF" width="90px" height="32px" alt="BBB OnLine Reliability Program" /> </div>
</div> </td>
<td width="9" rowspan="3" valign="top" class="silverbar"><img src="../My%20Documents/web/form/images/silverbar.jpg" width="9" height="356"></td>
</tr>
<tr valign="top">
<td colspan="2" class="footercell">
<div id="footer">Copyright © 2006 Mellon Technology Service</div> </td>
</tr>
</table>
<?php } ?>
</body>
</html>feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]