How Do I Make Form Fields Compulsory?

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
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

oops.. I noticed a problem with my original example.. need to move the 2 arrays outside the foreach..

Code: Select all

<?php

//.........

$data = array();
$valid = array();
foreach($_POST as $k => $v) 
{ 

//......

?>
the default in your switch is probably the error.. check your error logs..
furiousweebee
Forum Commoner
Posts: 69
Joined: Sun Jul 11, 2004 7:38 am
Location: Brisbane, Australia
Contact:

Post by furiousweebee »

Ok I put those vars outside the foreach but still the page is blank when it loads. I don't know much about the $k switch but I have no error logs so this is my code now:

Code: Select all

$data = array(); 
$valid = array(); 

foreach($_POST as $k => $v) 
{ 
  switch($k) 
  { 
    case 'street_team_data': 
      break;
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I was actually referring to this being the real error:

Code: Select all

default: 
      $data[$k] = $v; 
      $valid[$k] = preg_match                     # <-------- this line
      break; 
  }
furiousweebee
Forum Commoner
Posts: 69
Joined: Sun Jul 11, 2004 7:38 am
Location: Brisbane, Australia
Contact:

Post by furiousweebee »

lol ok so what can I do about it?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

default: 
      $data[$k] = $v; 
      $valid[$k] = true; //preg_match                     # <-------- this line 
      break; 
  }
furiousweebee
Forum Commoner
Posts: 69
Joined: Sun Jul 11, 2004 7:38 am
Location: Brisbane, Australia
Contact:

Post by furiousweebee »

Ok, I've done that and now the form is displayed, but it doesn't actually check for errors - even a completely empty form is submitted and then I get the "Submission successful" message.

One thing I haven't pointed out is that I have two pages to make this form. 1 contains normal html ("street_team.php") with just this in the middle:

Code: Select all

<?php

if(!$done){ $done = $HTTP_GET_VARS['done']; }

if($done=="" or $done=="n")
{
include("street_team_form.php");
}
elseif($done=="y")
{
echo "<div class="confirmation" align="center">You have successfully joined the Ten Foot Pole Street Team!</div>";
}

?>
The other page is "street_team_form.php" and contains all the code I've shown you previously. Would this matter?
furiousweebee
Forum Commoner
Posts: 69
Joined: Sun Jul 11, 2004 7:38 am
Location: Brisbane, Australia
Contact:

Post by furiousweebee »

My form now loads correctly and submits data, but it still doesn't check for errors. Also, it submits the data around 20 times instead of just once. Here is my document's entire code. Can anyone help me solve this once and for all please? I really need to get it done asap.

Code: Select all

&lt;form enctype="multipart/form-data" action="street_team.php" method="post"&gt;
&lt;table width="100%"  border="0" cellspacing="0" cellpadding="2"&gt;
  &lt;tr valign="top"&gt;
    &lt;td width="34%"&gt;First Name:&lt;/td&gt;
    &lt;td width="66%"&gt;&lt;input class="input" name="first_name" type="text" size="25"&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr valign="top"&gt;
    &lt;td&gt;Last Name:&lt;/td&gt;
    &lt;td&gt;&lt;input class="input" name="last_name" type="text" size="25"&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr valign="top"&gt;
    &lt;td&gt;Birth Year:&lt;/td&gt;
    &lt;td&gt;&lt;input class="input" name="birth_year" type="text" size="4"&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr valign="top"&gt;
    &lt;td&gt;Email Address:&lt;/td&gt;
    &lt;td&gt;&lt;input class="input" name="email" type="text" size="35"&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr valign="top"&gt;
    &lt;td&gt;Postal Address:&lt;/td&gt;
    &lt;td&gt;&lt;input class="input" name="address" type="text" size="35"&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr valign="top"&gt;
    &lt;td&gt;City:&lt;/td&gt;
    &lt;td&gt;&lt;input class="input" name="city" type="text" size="20"&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr valign="top"&gt;
    &lt;td&gt;State / Province:&lt;/td&gt;
    &lt;td&gt;&lt;input class="input" name="state_province" type="text" size="20"&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr valign="top"&gt;
    &lt;td&gt;Zip / Post Code: &lt;/td&gt;
    &lt;td&gt;&lt;input class="input" name="zip" type="text" size="8"&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr valign="top"&gt;
    &lt;td&gt;Country:&lt;/td&gt;
    &lt;td&gt;&lt;select size="1" name="country" class="input"&gt;
      &lt;option selected&gt;Other&lt;/option&gt;
      &lt;option&gt;Argentina&lt;/option&gt;
      &lt;option&gt;Australia&lt;/option&gt;
      &lt;option&gt;Belgium&lt;/option&gt;
      &lt;option&gt;Brazil&lt;/option&gt;
      &lt;option&gt;Canada&lt;/option&gt;
      &lt;option&gt;Chile&lt;/option&gt;
      &lt;option&gt;China&lt;/option&gt;
      &lt;option&gt;Colombia&lt;/option&gt;
      &lt;option&gt;Croatia&lt;/option&gt;
      &lt;option&gt;Czech Republic&lt;/option&gt;
      &lt;option&gt;Denmark&lt;/option&gt;
      &lt;option&gt;Ecuador&lt;/option&gt;
      &lt;option&gt;England&lt;/option&gt;
      &lt;option&gt;Finland&lt;/option&gt;
      &lt;option&gt;France&lt;/option&gt;
      &lt;option&gt;Germany&lt;/option&gt;
      &lt;option&gt;Great Britain&lt;/option&gt;
      &lt;option&gt;Hungary&lt;/option&gt;
      &lt;option&gt;Ireland&lt;/option&gt;
      &lt;option&gt;Italy&lt;/option&gt;
      &lt;option&gt;Japan&lt;/option&gt;
      &lt;option&gt;Mexico&lt;/option&gt;
      &lt;option&gt;N Ireland&lt;/option&gt;
      &lt;option&gt;Netherlands&lt;/option&gt;
      &lt;option&gt;New Zealand&lt;/option&gt;
      &lt;option&gt;Norway&lt;/option&gt;
      &lt;option&gt;Portugal&lt;/option&gt;
      &lt;option&gt;Russia&lt;/option&gt;
      &lt;option&gt;Scotland&lt;/option&gt;
      &lt;option&gt;South Africa&lt;/option&gt;
      &lt;option&gt;Spain&lt;/option&gt;
      &lt;option&gt;Sweden&lt;/option&gt;
      &lt;option&gt;Switzerland&lt;/option&gt;
      &lt;option&gt;Uruguay&lt;/option&gt;
      &lt;option&gt;USA&lt;/option&gt;
      &lt;option&gt;Wales&lt;/option&gt;
    &lt;/select&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr valign="top"&gt;
    &lt;td&gt;Continent&lt;/td&gt;
    &lt;td&gt;&lt;input class="input" name="continent" type="text" size="20"&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr valign="top"&gt;
    &lt;td&gt;Phone Number:&lt;/td&gt;
    &lt;td&gt;&lt;input class="input" name="phone" type="text" size="20"&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr valign="top"&gt;
    &lt;td&gt;Nearest Major City:&lt;/td&gt;
    &lt;td&gt;&lt;input class="input" name="major_city" type="text" size="20"&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr valign="top"&gt;
    &lt;td&gt;Favorite Venues/Hangouts:&lt;/td&gt;
    &lt;td&gt;&lt;textarea rows="5" name="faves" cols="30" class="input"&gt;&lt;/textarea&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr valign="top"&gt;
    &lt;td&gt;Comments:&lt;/td&gt;
    &lt;td&gt;&lt;textarea rows="5" name="comments" cols="30" class="input"&gt;&lt;/textarea&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr valign="top"&gt;
    &lt;td colspan="2" align="center"&gt;&lt;input type="submit" value="Join The Team!" name="street_team_data"&gt;
    &lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;

&lt;/form&gt;

Code: Select all

<?PHP

$database_name = "my_database"; 

$dbh = mysql_connect("localhost","my_username","my_password"); 

if (!mysql_select_db($database_name)) { 
  echo "Unable to select "$database_name" database"; 
}

$first_name = $_POST['first_name']; 
$date=date("Y-m-d"); 
$last_name = $_POST['last_name']; 
$birth_year = $_POST['birth_year']; 
$email = $_POST['email']; 
$address = $_POST['address']; 
$city = $_POST['city']; 
$state_province = $_POST['state_province']; 
$country = $_POST['country']; 
$zip = $_POST['zip']; 
$continent = $_POST['continent']; 
$phone = $_POST['phone']; 
$major_city = $_POST['major_city']; 
$faves = $_POST['faves']; 
$comments = $_POST['comments']; 

$data = array(); 
$valid = array(); 

foreach($_POST as $k => $v) 
{ 

  switch($k) 
  { 
    case 'street_team_data': 

      break; 
	case 'first_name': 
      $data[$k] = $v; 
      $valid[$k] = !preg_match('#[0-9]#',$v); 
      if (!$valid[$k]) { 
          echo "You must enter your first name"; 
      } 
      break;
	case 'last_name': 
      $data[$k] = $v; 
      $valid[$k] = !preg_match('#[0-9]#',$v); 
      if (!$valid[$k]) { 
          echo "You must enter your last name"; 
      } 
      break;
	case 'birth_year': 
      $data[$k] = $v; 
      $valid[$k] = !preg_match('#[0-9]#',$v); 
      if (!$valid[$k]) { 
          echo "You must enter your birth year"; 
      } 
      break;
	case 'email': 
      $data[$k] = $v; 
      $valid[$k] = !preg_match('#[0-9]#',$v); 
      if (!$valid[$k]) { 
          echo "You must enter a working email address"; 
      } 
      break;
	case 'address': 
      $data[$k] = $v; 
      $valid[$k] = !preg_match('#[0-9]#',$v); 
      if (!$valid[$k]) { 
          echo "You must enter your address"; 
      } 
      break;
	case 'city': 
      $data[$k] = $v; 
      $valid[$k] = !preg_match('#[0-9]#',$v); 
      if (!$valid[$k]) { 
          echo "You must enter your city"; 
      } 
      break;
	case 'state_province': 
      $data[$k] = $v; 
      $valid[$k] = !preg_match('#[0-9]#',$v); 
      if (!$valid[$k]) { 
          echo "You must enter your state or province"; 
      } 
      break;
	case 'zip': 
      $data[$k] = $v; 
      $valid[$k] = !preg_match('#[0-9]#',$v); 
      if (!$valid[$k]) { 
          echo "You must enter your zip or post code"; 
      } 
      break;
	case 'country': 
      $data[$k] = $v; 
      $valid[$k] = !preg_match('#[0-9]#',$v); 
      if (!$valid[$k]) { 
          echo "You must enter your country"; 
      } 
      break;
	case 'continent': 
      $data[$k] = $v; 
      $valid[$k] = !preg_match('#[0-9]#',$v); 
      if (!$valid[$k]) { 
          echo "You must enter your continent"; 
      } 
      break; 	  

    default: 
      $data[$k] = $v; 
      $valid[$k] = true; // preg_match 
      break; 
  } 

$banned = false;
$banlist[] = '';

if (isset($_SERVER["HTTP_X_FORWARDED_FOR"]))
 $ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
else
 $ip = $_SERVER["REMOTE_ADDR"];

$octets1 = explode(".", $ip);
foreach ($banlist as $val){
	$octets2 = explode(".", $val);
	if (	($octets1[0] == $octets2[0] || $octets2[0] == "*") && ($octets1[1] == $octets2[1] || $octets2[1] == "*") && 
		($octets1[2] == $octets2[2] || $octets2[2] == "*") && ($octets1[3] == $octets2[3] || $octets2[3] == "*")){
			$banned = true;
			break;
	}
}

$sql = "insert into street_team (date, first_name, last_name, birth_year, email, address, city, state_province, country, zip, continent, phone, major_city, faves, comments, ip) 
values ('$date', '$first_name', '$last_name', '$birth_year', '$email', '$address', '$city', '$state_province', '$country', '$zip', '$continent', '$phone', '$major_city', '$faves', '$comments', '$ip')";

  if (!$banned){
  $res = mysql_query($sql,$dbh);

  if (!$res) {
		echo mysql_errno().": ".mysql_error ()."";
		return 0;
	 }
  }
  echo"Submission successful";
 
}
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

your preg matches just check if 1 single number is in each of the fields.. The way it's written now, it's always inserting data. You need to check for invalid fields, and not insert when those happen.. use a [php_man]foreach[/php_man] for that.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

While it is good to check form validity server side as well, a more immediate check is to use the onsubmit method when submitting the form, like explained here. In the javascript function, instead of putting in a confirm, you can do some testing on the form, output alerts, and return false if not all the fields are filled in.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

(not harping on you here, Pickle)

don't use Javascript as a substitute for validation, but as an optional augmentation. If the data being posted is anything important, which most stuff that posts is, then you must check validity server-side.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

I totally agree. Like I said, validation should also be done server side. However, a system seems more responsive (at least to me), if there is a pop up explaining problems with the form, before I even need to submit it.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
furiousweebee
Forum Commoner
Posts: 69
Joined: Sun Jul 11, 2004 7:38 am
Location: Brisbane, Australia
Contact:

Post by furiousweebee »

feyd wrote:your preg matches just check if 1 single number is in each of the fields.. The way it's written now, it's always inserting data. You need to check for invalid fields, and not insert when those happen.. use a [php_man]foreach[/php_man] for that.
If I had more time, I'd read up on all this to work it out but I was supposed to have the form finished a couple of days ago - it's the last item left in a rather large site. But with limited time and limited (read: zero) programming knowledge, I can't afford to tinker with things too much. Would someone be able to show me how to change my code so that it works? I know this is asking a lot but I'm pretty desperate to get this thing finished right now. I can pay you for your time if need be.
Post Reply