1 Replies - 4756 Views - Last Post: 05 November 2008 - 09:56 AM Rate Topic: -----

#1 TheMonkII   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 05-November 08

Checking for empty form fields

Post icon  Posted 05 November 2008 - 08:35 AM

Hi Folks.

I'm new here. I'm a novice, as such, you'll probably see a fair bit of me. I've been working on trying to get this script to check and ensure form fields have a value in them before going on to the next page. It's not checking and simply going to the next page. Any help would be greatly appreciated. Here is the script. As you can see, there is built in error detection that i would like to use, just can't seem to get it working. Thanks so much in advance.

   // If the Submit button was pressed we start this routine
   if (isset($_POST['step_2']) 
   && $_POST['step_2'] == $lang['step_2'])
	{
	 // Check for the empty or incorrect required fields
	if (($form['style'])&&($form['saletype'])&&($form['terms_and_conditions'])){
	header("Location:step2.php");
	} else {
	echo "You didn't fill in all required fields";
	}

	 // Printing the Information table header
	 // to output errors or confirmaion messages
	 echo table_header ( $lang['Information'] );

	 // Initially we think that no errors were found
	 $count_error = 0;
	 
	 
	 // Check if this user have reached the maximum
	 // number of possible listings set by admin
	 $sql = 'SELECT * FROM ' . PROPERTIES_TABLE . ' WHERE userid = "' . $f_res['id'] . '" ';
	 $r = $db->query($sql) or error ('Critical Error', mysql_error () );

	 if ($db->numrows($r) >= $f_package['listings'])
	  { echo $lang['Maximum_Number_Of_Listings_Reached'] . '<br>'; $count_error++;}

	 // Check if this listing title is already exist
	 $sql = 'SELECT title FROM ' . PROPERTIES_TABLE . ' WHERE title = "' . $form['title'] . '" ';
	 $r = $db->query($sql) or error ('Critical Error', mysql_error () );

	 if ($db->fetcharray($r) > 0 )
	  { echo $lang['Title_Used'] . '<br>'; $count_error++;}

	 //

	 // If there were any errors found we start printing them out
	 if ($count_error > 0)
	  echo '<br><span class="warning">' . $lang['Errors_Found'] . ': ' . $count_error . '</span><br>';

	 // If no errors were found during the above checks we continue
	 if ($count_error == 0)
	  {
	   // Update user details in the database

	   // Get the user IP address
	   $user_ip = $_SERVER['REMOTE_ADDR'];
	   // If there is more than one IP
	   // get the first one from the 
	   // comma separated list
	   if ( strstr($user_ip, ', ') ) 
		{
		 $ips = explode(', ', $user_ip);
		 $user_ip = $ips[0];
		}

	   // If listing needs to be approved by the administrator
	   if ($conf['approve_listings'] == 'ON')
		$approved = 0;
	   else
		$approved = 1;

	   // Create a mysql query


		$db->query($sql) or error ('Critical Error', mysql_error ());

		// Fetch the last auto incremented listing id
		$id = mysql_insert_id();


	// If do not need admin approval we
	// add this new listing into the category
		if ($conf['approve_listings'] != 'ON')
		 update_categories ('' , $form['type']);

	  }

	 if (isset($uploaded) && $uploaded)
	  echo '<p align="center"><span class="warning">' . $lang['Listing_Image_Uploaded'] . '</span></p>';

	 // If image was not uploaded because of the image
	 // size problems etc.
	 if (isset($uploaded) && !$uploaded)
	  echo '<p align="center"><span class="warning">' . $lang['Listing_Image_NOT_Uploaded'] . '</span></p>';

	 if ($count_error == 0)
	  {
	   echo '<p align="center"><br><br><a href="' . URL . '/editgallery.php?id=' . $id . '"><img src="' . URL . '/templates/' . $cookie_template . '/images/icons/editgallery.png" valign="top" border="0"><br>' . $lang['Photo_Gallery'] . '</a><br><br></p>';
	  }

	 echo table_footer ( );

	}

   // Navigation Menu
   echo table_header ( $lang['Menu_User_Login'] );
   echo ' <table cellpadding="5" cellspacing="0" border="0"> <tr>';
   // Go To Edit Listings Form

   // Fetching the listings number from the table
   $sql = 'SELECT id FROM ' . PROPERTIES_TABLE . ' WHERE userid = "' . $f_res['id'] . '"';
   $r_listings = $db->query( $sql );
   $res_listings = $db->numrows( $r_listings );

   // If we've got more than 0 listings we print out the
   // link
   if ($res_listings > 0)
	echo '<td width="80" align="center" valign="top"><a href="' . URL . '/viewuserlistings.php"><img src="' . URL . '/templates/' . $cookie_template . '/images/icons/editlistings.png" border="0" alt=""><br>' . $lang['Edit_Listings'] . '</a> (' . $res_listings . ') </td>';

   // Back To User Control Panel
   echo '<td width="80" align="center" valign="top"><a href="' . URL . '/login.php"><img src="' . URL . '/templates/' . $cookie_template . '/images/icons/login.png" border="0" alt=""><br>' . $lang['Menu_User_Login'] . '</a></td>';
   echo ' </tr></table> ';
   echo table_footer();

   //

   // Main form

   // Submit Listing Form Table Header
   echo table_header ( $lang['Menu_Submit_Property'] );

   // Define the form variables if the form was not updated
   // or the previous data submission had no errors
   if (!isset($form) || (isset($count_error) && $count_error == '0'))
	{
	 $form = array();

	 $first = preg_split('//', $f_res['first_name'], -1, PREG_SPLIT_NO_EMPTY);
	 $last = preg_split('//', $f_res['last_name'], -1, PREG_SPLIT_NO_EMPTY);

	 $form['hblisting'] = strtoupper($first[0] . $last[0] . rand(100000, 999999));
	 $form['type'] = '1';
	 $form['style'] = '';
	 $form['terms_and_conditions'] = '';
	}

   // Output the form

  
  //Edit part starts here


echo '<P><B><font color=990000>Step 1 </font></B> - Step 2 - Step 3 - Step 4
<P>
<B><font color=990000>Property Type & Listing Type Selection</font></B>
<div align=center>
What would you like to do with your property? <BR>
<form method="post" action="temp2.php">
<input type="radio" name="saletype" value="rent">I would like to <B>rent</B> my property<BR>
<input type="radio" name="saletype" value="sell">I would like to <B>sell</B> my property<BR>
<P>
What type of property would you like to list?<BR>';
echo '<table width=100% align=center>
		<TR>
			<TD width=100% align=center>';
echo '<select name="type">' . generate_options_list(TYPES_TABLE, $form['type'] . '</select>');
echo '		</td>
		</TR>
		<TR>
			<TD align=center>';
			echo ('<input type="checkbox" name="terms_and_conditions" value="terms_and_conditions">Check to agree to our Terms and Conditions<BR>');
			echo '</TD>
			</TR>
		</TABLE>';

echo ('<input type="Submit" name="step_2" value="Step 2">');
echo '</form> 

</div>';


What is done on this form is passed to the next page using a session.

Is This A Good Question/Topic? 0
  • +

Replies To: Checking for empty form fields

#2 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3826
  • View blog
  • Posts: 13,945
  • Joined: 08-August 08

Re: Checking for empty form fields

Posted 05 November 2008 - 09:56 AM

View PostTheMonkII, on 5 Nov, 2008 - 07:35 AM, said:

if (isset($_POST['step_2']) 
   && $_POST['step_2'] == $lang['step_2'])
    {
     // Check for the empty or incorrect required fields
    if (($form['style'])&&($form['saletype'])&&($form['terms_and_conditions'])){
    header("Location:step2.php");
    }


You're missing a ( in the first if statement, and the second if statment merely checks to see if $form['style'], $form['saletype'], and $form['terms_and_conditions'] exist. Try it this way:

if ((isset($_POST['step_2']) 
   && $_POST['step_2'] == $lang['step_2'])
    {
     // Check for the empty or incorrect required fields
    if (($form['style'] != '')&&($form['saletype'] != '')&&($form['terms_and_conditions'] != '')){
    header("Location:step2.php");
    }


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1