Stupid little error checking

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Stupid little error checking

Post by John Cartwright »

I made this little snipplet to check if parts of a form have been left blank. The array are all the values that have to be filled in.

Code: Select all

<?php
$required =      array($type,$budget,$time_start,$time_frame,$description,$firstname,$lastname,$organization,$city,$state,$country,$email);

$req[]="";
$req2 ="";

foreach($required as $req2){
if (req2 == ""){
$req[$req2] = "1";
$error=1;
}
}
?>
And on the form itself I have this at each required field. In this example "type" is the value of this field.

Code: Select all

<?php
if ($req[$type]==1){ echo "class="error" "; }
?>
It changed the CSS style if it is not filled in.
The problem is that this:

Code: Select all

<?php
$req[$req2] = "1";
?>
isn't functioning the way I think it is supposed to. It's like its being ignored. If you gotto see the whole script I'll show you but I think this is enough.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Not sure if this helps or not, but i tend to do something like this to check for missing required form fields.

Code: Select all

$required = array(
  'type', 'budget', 'time_start', 'time_frame',
  'description', 'firstname', 'lastname', 'organization',
  'city', 'state', 'country', 'email'
);

foreach($required as $req){
  if(empty($_POST[$req])){
    $missing[] = $req;
  }
}
if(!empty($missing)){
  echo 'Missing required fields : '.join(' ', $missing);
} else {
  echo 'No missing fields';
}
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

but take a look at this

Code: Select all

<?php

if ($req[$type]==1){ echo "class="error" "; } 


?>
This is where each field checks it is had an error set on it or not. This part is very crucial... thats why I'm trying to do it my way...

thanks for ur help tho but sorta gotto try and keep this format...unless of course u got a better way : )
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Well, you have ...
if (req2 == ""){
$req[$req2] = "1";
$error=1;
}
if $req2 == '' then your doing $req[] = "1" .. which means if ($req[$type]==1){ will never be true.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

hmmmmmmmmmmmmmmmmm point made... gotto rethink this

any ideas :S?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Tried the code in the second post ?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

But I still gotto find a way to do something like this for each field:

Code: Select all

<? if ($req[$type]==1){ echo "class="error" "; } ?>
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Code: Select all

$required = array( 
  'type', 'budget', 'time_start', 'time_frame', 
  'description', 'firstname', 'lastname', 'organization', 
  'city', 'state', 'country', 'email' 
); 

$missing = array();
foreach($required as $req){ 
  if(empty($_POST[$req])){ 
    $missing[] = $req; 
  } 
} 

//then in the form you could do...
if(in_array($type, $missing)){
    echo 'class="error"';
}
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Getting this error

Warning: in_array(): Wrong datatype for second argument in /home/****/public_html/order.php on line 101
height="19" colSpan=2>

which is the part of the form where I put the in_array thing

Code: Select all

<?php
 if(in_array($type, $missing)){  echo 'class="error"'; } 
?>
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Well, i'm not sure what your form looks like, or how it is displayed in relation to the required checking. Maybe a more complete example would help?

Code: Select all

<?php

$missing = array();
if(!empty($_POST)){
  $required = array('one', 'two', 'three');
  foreach($required as $req){
    if(empty($_POST[$req])){
      $missing[] = $req;
    }
  }
}

?>
<html>
<head>
<style type="text/css">
<!--
.error {
  color: #f00;
}
-->
</style>
</head>
<body>
<form method="post" action="">
<span<?php if(in_array('one', $missing)) echo ' class="error"' ?>>one</span> <input type="text" name="one" value=""><br />
<span<?php if(in_array('two', $missing)) echo ' class="error"' ?>>two</span> <input type="text" name="two" value=""><br />
<span<?php if(in_array('three', $missing)) echo ' class="error"' ?>>three</span> <input type="text" name="three" value=""><br />
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

This is my whole script... the HTML is VERY messy because I made the form in Dreamweaver and havn't cleaned up the code yet :S

Code: Select all

<?php


   include "inc/design1.php"; ?>

<?
$error=0;

if ($submit){

# # # # # # # # # # ERROR CHECKING # # # # # # # # # #  
					   
$required =      array($type,$budget,$time_start,$time_frame,$description,$firstname,$lastname,$organization,$city,
					   $state,$country,$email);

$missing = array(); 
foreach($required as $req){ 
  if(empty($_POST[$req])){ 
    $missing[] = $req; 
  $error=1;
  } 
} 
# # # # # # # # # # # # # # # # # # # # # # # # # # # # 



if ($error==0){

$formvariables = array($type,$type_other,$logodesign,$bannerdesign,$graphicdesign,$shoppingcart,
					   $interactivefeatures,$flashanimationandpresentation,$webhosting,$domainnameregistration,$sitemaintenance,
					   $editingandwritting,$merchantaccount,$customscripts,$creditcardprocessing,$searchengineoptimization,
					   $searchcapabilitiesonthewebsite,$databaseprogramming,$budget,$time_start,$time_frame,$description,
					   $firstname,$lastname,$organization,$address,$city,$state,$country,$phone,$fax,$email,$icq,
					   $contact_email,$contact_phone,$contact_icq,$tog);
					   
$feature =       array($logodesign,$bannerdesign,$graphicdesign,$shoppingcart,
					   $interactivefeatures,$flashanimationandpresentation,$webhosting,$domainnameregistration,$sitemaintenance,
					   $editingandwritting,$merchantaccount,$customscripts,$creditcardprocessing,$searchengineoptimization,
					   $searchcapabilitiesonthewebsite,$databaseprogramming);



# # # # # # # # # # # # # # # # # # # # # # # # # # # # 
########################################################
# # # # # # # # # # MAKING THE EMAIL # # # # # # # # # # 

$message = '<b><font size=3><u>Information about your project</u></font></b><br><br>';
$message .= '<b>Type of site: </b>'.$type.'<br>';		   
if ($type_other != ""){ 
$message .= '<b>Other: </b>'.$type_other.'<br>'; 
} 
$message .= '<b>Features for the site: </b><br>';
foreach($feature as $feat){
if(isset($feat)){
$message .= '- '.$feat.'<br>';
}
}
if (isset($features_other1)){
$message .= $features_other1.'<br>';
}
$message .= '<b>Estimated Budget:</b> '.$budget.'<br>';
$message .= '<b>Time until we can start project: </b>'.$time_start.'<br>';
$message .= '<b>Time Frame: </b>'.$time_frame.'<br>';
$message .= '<b>Overview: </b>'.$description.'<br>'; 

echo "<br>";

echo "<table border="1" width="452" border="1" cellpadding="0" cellspacing="0" bordercolor="9A9A9A" align="center">\n".
     		"<tr class="porttext" bordercolor="EFEFEF" bgcolor="EFEFEF"><td width="5%">&nbsp;<td width="90%">\n";	 
echo "<br>".$message."<br>";
echo "<td width="5%">&nbsp;</td></td></td></tr></table>\n";

# # # # # # # # # # # # # # # # # # # # # # # # # # # # 
}
}else{

?>

<FORM action=order.php method=post>
  
  <div align="center"><br>
    
	<table class="porttext" width="95%" border="0">
      <tr>
        <td>Please fill out the following form that will give us a good idea about 
          your project. We will get back to you within 24 hours with an estimate 
          or additional questions about your site. Thank you! </td>
      </tr>
    </table>
    
	<br></div>
  
  <TABLE class="porttext" cellSpacing=1 cellPadding=2 width=95% align=center bgColor=#9A9A9A border=0>
    <TBODY>
      <TR bgcolor="#CCCCCC" > 
        <TD height="23" colSpan=2 bgcolor="#CCCCCC"><B>Information about your project:</B></TD>
      </TR>
      <TR> 
        <TD colSpan=2 bgcolor="#ffffff"> <TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
            
		  <TBODY class="porttext">
              <TR> 
                <TD <? if(in_array($type, $missing)){  echo 'class="error"'; } ?> height="19" colSpan=2><B>Are you looking for: <font color="#FF0000">* </font></B> </TD>	  
			  </TR>
              <TR> 
                <TD width="6%"><INPUT type=radio value="New Web site design" name=type></TD>
                <TD width="94%">New Web site design </TD>
              </TR>
              <TR> 
                <TD><INPUT type=radio value="Re-design of the current Web site" name=type></TD>
                <TD>Re-design of the current Web site</TD>
              </TR>
              <TR> 
                <TD><INPUT type=radio value="Other" name=type></TD>
                <TD>Other (please specify below):</TD>
              </TR>
              <TR> 
                <TD></TD>
                <TD><INPUT size=50 name=type_other><br>
                  <br>
                </TD>
              </TR>
            </TBODY>
          </TABLE>
          
		  <TABLE class="porttext" cellSpacing=0 cellPadding=0 width="100%" border=0>
            <TBODY>
              <TR> 
                <TD colSpan=2><B>Please select features and services you are interested in:</B></TD>
              </TR>
              <TR> 
                <TD><INPUT name=logodesign type=checkbox id="logodesign" value="Logo Design"></TD>
                <TD>Logo Design</TD>
              </TR>
              <TR> 
                <TD><INPUT name=bannerdesign type=checkbox id="bannerdesign" value="Banner Design"></TD>
                <TD>Banner Design</TD>
              </TR>
              <TR> 
                <TD><INPUT name=graphicdesign type=checkbox id="graphicdesign" value="Graphic Design"></TD>
                <TD>Graphic Design</TD>
              </TR>
              <TR> 
                <TD><INPUT name=shoppingcart type=checkbox id="shoppingcart" value="Shopping Cart"></TD>
                <TD>Shopping Cart</TD>
              </TR>
              <TR> 
                <TD><INPUT name=interactivefeatures type=checkbox id="interactivefeatures" value="Interactive Features"></TD>
                <TD>Interactive Features (Chat, Forum, Forms)</TD>
              </TR>
              <TR> 
                <TD><INPUT name=flashanimationandpresentation type=checkbox id="flashanimationandpresentation" value="Flash Animation & Presentation"></TD>
                <TD>Flash Animation & Presentation</TD>
              </TR>
              <TR> 
                <TD><INPUT name=webhosting type=checkbox id="webhosting" value="Web Hosting"></TD>
                <TD>Web Hosting</TD>
              </TR>
              <TR> 
                <TD><INPUT name="domainnameregistration" type=checkbox id="domainnameregistration" value="Domain Name Registration"></TD>
                <TD>Domain Name Registration</TD>
              </TR>
              <TR> 
                <TD><INPUT name=sitemaintenance type=checkbox id="sitemaintenance" value="Site Maintenance"></TD>
                <TD>Site Maintenance</TD>
              </TR>
              <TR> 
                <TD><INPUT name=editingandwritting type=checkbox id="editingandwritting" value="Editing and Writing"></TD>
                <TD>Editing and Writing</TD>
              </TR>
              <TR> 
                <TD><INPUT name=merchantaccount type=checkbox id="merchantaccount" value="Merchant Account"></TD>
                <TD>Merchant Account</TD>
              </TR>
              <TR> 
                <TD><INPUT name=customscripts type=checkbox id="customscripts" value="Custom Scripts"></TD>
                <TD>Custom Scripts</TD>
              </TR>
              <TR> 
                <TD><INPUT name=creditcardprocessing type=checkbox id="creditcardprocessing" value="Credit Card Processing"></TD>
                <TD>Credit Card Processing</TD>
              </TR>
              <TR> 
                <TD><INPUT name=searchengineoptimization type=checkbox id="searchengineoptimization" value="Search Engine Optimization"></TD>
                <TD>Search Engine Optimization</TD>
              </TR>
              <TR> 
                <TD><INPUT name=searchcapabilitiesonthewebsite type=checkbox id="searchcapabilitiesonthewebsite" value="Search Capabilities on the Web Site"></TD>
                <TD>Search Capabilities on the Web Site</TD>
              </TR>
              <TR> 
                <TD><INPUT name=databaseprogramming type=checkbox id="databaseprogramming" value="Database Programming"></TD>
                <TD>Database Programming</TD>
              </TR>
              <TR> 
                <TD><INPUT type=checkbox value=Other name="Other"></TD>
                <TD>Other (please specify below):</TD>
              </TR>
              <TR> 
                <TD></TD>
                <TD><TEXTAREA name="features_other" cols=42 rows=3 id="features_other"></TEXTAREA></TD>
              </TR>
            </TBODY>
          </TABLE>
          <br>
          <table class="porttext" width="95%" border="0" cellpadding="0" cellspacing="0">
            <tr> 
              <td width="80%" height="24"><div align="left">Estimate Budget: <font color="#FF0000">*</font></div></td>
              <td width="44%"> 
			      <select name="budget">
                  <option value="Under $750" selected>Under $750 
                  <option value=$750-1,500>$750-1,500 
                  <option value=$1500-2500>$1,500-2,500 
                  <option value=$2500-4000>$2,500-4,000 
                  <option value=$4000-7000>$4,000-7,000 
                  <option value=$7000-10000$>$6,000-10,000 
                  <option value="$10,000 or higher">$10,000 or higher</option>
                  </select>
			  </td>
            </tr>
            <tr> 
              <td><div align="left">How soon do you need to start the project?: <font color="#FF0000">*</font></div></td>
              <td>
			  	  <select name="time_start">
                  <option value=A.S.A.P. selected>A.S.A.P. 
                  <option value="Within 2 weeks">Within 2 weeks 
                  <option value="Within 2-4 weeks">Within 2-4 weeks 
                  <option value="Within 4-8 weeks">Within 4-8 weeks 
                  <option value="Within 8-12 weeks">Within 8-12 weeks 
                  <option value="12+ weeks">12+ weeks</option>
                </select>
			  </td>
            </tr>
            <tr> 
              <td><div align="left">Time Frame: <font color="#FF0000">*</font>&nbsp;</div></td>
              <td>
			      <select name="time_frame">
                  <option value="Less than a week" selected>Less than a week 
                  <option value="1-2 weeks">1-2 weeks 
                  <option value="2-3 weeks">2-3 weeks 
                  <option value="3-5 weeks">3-5 weeks 
                  <option value="5-8 weeks">5-8 weeks 
                  <option value="8-12 weeks">8-12 weeks 
                  <option value="12+ weeks">12+ weeks</option>
                  </select>
			  </td>
            </tr>
          </table>
          
		  <br><br>
 
           <TABLE class="porttext" cellSpacing=0 cellPadding=0 width="100%" border=0>
            <TBODY>
              <TR> 
                <TD height="40" colspan="2">
				<B><br>Please provide a brief overview of the project:</B> <font color="#FF0000">*</font>
				</TD>
              </TR>
              <TR> 
                <TD width="6%"></TD>
                <TD width="94%"e><textarea name="description" rows=5 cols=45></textarea></TD>
              </TR>
            </TBODY>
          </TABLE>
          <br>
        </TD>
      </TR>
    </TBODY>
  </TABLE>
  <BR>
  <TABLE class="porttext" cellSpacing=1 cellPadding=2 width=95% align=center bgColor=#9A9A9A 
      border=0>
    <TBODY>
      <TR bgcolor="#CCCCCC"> 
        <TD colSpan=2><B>Your contact information</B></TD>
      </TR>
      <TR bgColor=#ffffff> 
        <TD width=240>Your first name <font color="#FF0000">*</font></TD>
        <TD>&nbsp; <INPUT size=25 name="firstname">
        </TD>
      </TR>
      <TR bgColor=#ffffff> 
        <TD>Last name <font color="#FF0000">*</font></TD>
        <TD>&nbsp; <INPUT size=25 name="lastname"></TD>
      </TR>
      <TR bgColor=#ffffff> 
        <TD>Organization (Company Name) <font color="#FF0000">*</font></TD>
        <TD>&nbsp; <INPUT size=25 name="organization"></TD>
      </TR>
      <TR bgColor=#ffffff> 
        <TD>Address </TD>
        <TD>&nbsp; <INPUT maxLength=255 size=25 name="address"></TD>
      </TR>
      <TR bgColor=#ffffff> 
        <TD>City <font color="#FF0000">*</font></TD>
        <TD>&nbsp; <INPUT size=25 name="city"></TD>
      </TR>
      <TR bgColor=#ffffff> 
        <TD>Area (province, state) <font color="#FF0000">*</font></TD>
        <TD>&nbsp; <INPUT size=25 name="state"></TD>
      </TR>
      <TR bgColor=#ffffff> 
        <TD>Country <font color="#FF0000">*</font></TD>
        <TD>&nbsp; <INPUT size=25 name="country"></TD>
      </TR>
      <TR bgColor=#ffffff> 
        <TD>Phone number including area code</TD>
        <TD>&nbsp; <INPUT maxLength=25 size=25 name="phone"></TD>
      </TR>
      <TR bgColor=#ffffff> 
        <TD>Fax number (optional)</TD>
        <TD>&nbsp; <INPUT maxLength=25 size=25 name="fax"></TD>
      </TR>
      <TR bgColor=#ffffff> 
        <TD>Email <font color="#FF0000">*</font></TD>
        <TD>&nbsp; <INPUT maxLength=255 size=25 name="email"></TD>
      </TR>
      <TR bgColor=#ffffff> 
        <TD>ICQ</TD>
        <TD>&nbsp; <INPUT maxLength=255 size=25 name="icq"></TD>
      </TR>
      <TR align=left bgColor=#ffffff> 
        <TD>Prefered way to contact you:</TD>
        <TD> <TABLE class="porttext" cellSpacing=0 cellPadding=0 align=left border=0>
            <TBODY>
              <TR> 
                <TD><INPUT name="contact_email" type=checkbox value=YES></TD>
                <TD>E-Mail</TD>
              </TR>
              <TR> 
                <TD><INPUT type=checkbox value=YES name="contact_phone"></TD>
                <TD>Phone</TD>
              </TR>
              <TR> 
                <TD><INPUT type=checkbox value=YES name="contact_icq"></TD>
                <TD>ICQ</TD>
              </TR>
            </TBODY>
          </TABLE></TD>
      </TR>
    </TBODY>
  </TABLE>
  <br>
  <table class="porttext" width="95%" border="0" align="center" cellpadding="0" cellspacing="0">
    <tr>
      <td><div align="left"> Do you accept the <a href="legal/tog.php" class="portlink3 href="legal/tog/">terms of agrement</a>? 
            <select name=select>
            <option value="yes">yes</option>
            <option value="no">no</option>
            </select>
            <font color="#FF0000">*</font> </div></td>
    </tr>
  </table>
  <CENTER>
    <BR>
    <INPUT name="submit" type=submit value="Send request">
    <INPUT name="reset"  type=reset value="Clear form">
  </CENTER>
</FORM>

<? 
}

include "inc/design2.php";
 

?>
or click my signature URL and click on ORDER links..
its my portfolio in progress :)
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Well, the first thing i can see is that you have variables in the $required array, that needs to contain a list of field names, like $required = array('type', 'budget', etc...);

Also, your code requires register_globals to be On, i'm sure you know that, but i'm just checking ;)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

$required = array('type', 'budget', etc...);

is that the same thing as using $type, $budget, $etc ?

because their values are changed depending on what the user enters in the form.. they are not predefined.

btw yes my register_globals is on :)

edit* i still got the same error
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Naw, it's not the same. The values don't matter when checking is required fields have been filled in, as if they wern't filled in then no value is even submitted. So by using 'type', 'budget' etc you are saying "was the budget field submitted" .. you don't care what the value is, just that it has a value...and if it has a value then $_POST['budget'] won't be empty..

if that all makes sense :o
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

still getting this error

Warning: in_array(): Wrong datatype for second argument in /home/jcart/public_html/order.php on line 104
height="19" colSpan=2>

edit* yes it does make sense thanks for clarifying :)
Post Reply