Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form Processing

Status
Not open for further replies.

WebStuck

Programmer
Joined
Apr 12, 2003
Messages
79
Location
US
Hi,

I am working on a form that displays a country drop down box. When a country is selected, my php script displays any applicable states/provinces in a state/provinces drop down box or if the country doesn't have states it displays cities in a city drop down box. I have all of that working but am unsure of how to know when the form is actually ready for submission so that I can pass control to a php ProcessForm function. I could check to make sure that each form field has a value set and if so then process the form. However, I feel that the user should be shown an error message if all fields aren't set instead of just being shown the form again. My code is below:

<html>
<head>
<title>Submit Your Site</title>
</head>

<body>
<p align="center">
<font face="arial" size="2">
<?php
mysql_connect('localhost', 'user', 'password') OR die("Can't connect to database!");

mysql_select_db('database');

print <<<END
<form name="joinform" method="POST" action="URLOFTHISPAGE" enctype="multipart/form-data">
<b>Webmaster Information</b><br><br>
Your Name:
<input type="text" name="member_name" size="20" value="$member_name">
<br>
Your Email Address:
<input type="text" name="member_email" size="20" value="$member_email">
<br>
Would you like to join our newsletter:
END;
if ($member_newsletter == "Y")
{
print <<<END
<input type="radio" name="member_newsletter" value="Y" checked>Yes
END;
}
else
{
print <<<END
<input type="radio" name="member_newsletter" value="Y">Yes
END;
}
if ($member_newsletter == "N")
{
print <<<END
<input type="radio" name="member_newsletter" value="N" checked>No
END;
}
else
{
print <<<END
<input type="radio" name="member_newsletter" value="N">No
END;
}

print <<<END
<br>
<br><br><b>Site Information</b><br><br>
Site Name:
<input type="text" name="member_site_name" size="20" value="$member_site_name">
<br>
Site URL:
<input type="text" name="member_site_url" size="20" value="$member_site_url">
<br>
Site Description:
<input type="text" name="member_site_description" size="20" value="$member_site_description">
<br>
Site Category:
<select name="member_site_category">
<option value="">Please Choose</option>
END;
$query = "SELECT CATEGORY_NAME FROM CATEGORY";

$result = mysql_query($query) OR die("Invalid query!");

while ($row = mysql_fetch_array($result))
{
if ($row[CATEGORY_NAME] == "$member_site_category")
{
print <<<END
<option value="$row[CATEGORY_NAME]" selected>$row[CATEGORY_NAME]</option>
END;
}
else
{
print <<<END
<option value="$row[CATEGORY_NAME]">$row[CATEGORY_NAME]</option>
END;
}
}

print <<<END
</select>
<br><br><br>
<b>
Regional Information
</b>
<br>
<i>
Select your site's targeted country, state, or city.
</i>
<br><br>
Site Country:
<select name="member_site_country" onchange="joinform.submit();">
<option value="">Please Choose</option>
END;
if ($member_site_country == "All")
{
print <<<END
<option value="All" selected>All</option>
END;
}
else
{
print <<<END
<option value="All">All</option>
END;
}

$query = "SELECT DISTINCT LOCATION_COUNTRY FROM LOCATION";

$countryresult = mysql_query($query) OR die("Invalid query!");

while ($row = mysql_fetch_array($countryresult))
{
if ($row[LOCATION_COUNTRY] == "$member_site_country")
{
print <<<END
<option value="$row[LOCATION_COUNTRY]" selected>$row[LOCATION_COUNTRY]</option>
END;
}
else
{
print <<<END
<option value="$row[LOCATION_COUNTRY]">$row[LOCATION_COUNTRY]</option>
END;
}
}

if ($member_site_country == "Other")
{
print <<<END
<option value="Other" selected>Other</option>
END;
}
else
{
print <<<END
<option value="Other">Other</option>
END;
}
print <<<END
</select>
<br>
END;

if ($member_site_country == "Other")
{
print <<<END
Other Country Name:
<input type="text" name="member_site_country_other" size="20" value="$member_site_country_other">
<br>
END;
}

$query = "SELECT DISTINCT LOCATION_STATE FROM LOCATION WHERE LOCATION_COUNTRY = '$member_site_country'";

$stateresult = mysql_query($query) OR die("Invalid query!");

if ($member_site_country != "All")
{
if (mysql_num_rows($stateresult) > 1)
{
if ($member_site_country != "Canada")
{
print <<<END
Site State:
END;
}
else
{
print <<<END
Site Province:
END;
}
print <<<END
<select name="member_site_state" onchange="joinform.submit();">
<option value="">Please Choose</option>
END;
if ($member_site_state == "All")
{
print <<<END
<option value="All" selected>All</option>
END;
}
else
{
print <<<END
<option value="All">All</option>
END;
}

while ($row = mysql_fetch_array($stateresult))
{
if ($row[LOCATION_STATE] == "$member_site_state")
{
print <<<END
<option value="$row[LOCATION_STATE]" selected>$row[LOCATION_STATE]</option>
END;
}
else
{
print <<<END
<option value="$row[LOCATION_STATE]">$row[LOCATION_STATE]</option>
END;
}
}
print <<<END
</select>
<br>
END;
}
else
{
$member_site_state = "";
}
}

if ($member_site_country != "")
{
if (($member_site_country != "All") AND ($member_site_state != "All"))
{
if ($member_site_country != "Other")
{
if (($member_site_state != "") OR (($member_site_country != "Canada") AND ($member_site_country != "United States")))
{
if ($member_site_state != "")
{
$query = "SELECT DISTINCT LOCATION_CITY FROM LOCATION WHERE LOCATION_STATE = '$member_site_state'";
}
if (($member_site_country != "Canada") AND ($member_site_country != "United States"))
{
$query = "SELECT DISTINCT LOCATION_CITY FROM LOCATION WHERE LOCATION_COUNTRY = '$member_site_country'";
}
$cityresult = mysql_query($query) or die("Invalid query!");

print <<<END
Site City:
<select name="member_site_city">
<option value="">Please Choose</option>
END;
if ($member_site_city == "All")
{
print <<<END
<option value="All" selected>All</option>
END;
}
else
{
print <<<END
<option value="All">All</option>
END;
}

while ($row = mysql_fetch_array($cityresult))
{
if ($row[LOCATION_CITY] == "$member_site_city")
{
print <<<END
<option value="$row[LOCATION_CITY]" selected>$row[LOCATION_CITY]</option>
END;
}
else
{
print <<<END
<option value="$row[LOCATION_CITY]">$row[LOCATION_CITY]</option>
END;
}
}

if ($member_site_city == "Other")
{
print <<<END
<option value="Other" selected>Other</option>
END;
}
else
{
print <<<END
<option value="Other">Other</option>
END;
}
print <<<END
</select>
<br>
END;
}
}
}
}

if (($member_site_country == "Other") OR ($member_site_city == "Other"))
{
print <<<END
Other City Name:
<input type="text" name="member_site_city_other" size="20" value="$member_site_city_other">
<br>
END;
}

print <<<END
<br><br>
<input type="submit" value="Submit">
</form>
END;
?>
</font>
</p>
</body>
</html>


Thanks!
Ben
 
What about checking for submission via the submit button?
You depend on client side scripting with the onChange events. You might as well add a button that sets a hidden field which can be used as "trigger" for validation.
I'd make it
Code:
<input type="button" onClick="document.form.trigger.value='1';document.form.submit();>
Then check for $_POST['trigger'] and write that program branch with validation. If errors are detected put them into an associative array and introduce structures like this:
Code:
print "</select>";
if ($error['city']){
   print "<span class="error">A city selection is required</span>";
   //etc.
Define the style for .error with CSS.
 
Well, currently the form doesn't depend on client side scripting because a visitor could click the submit button to have the form bring up associated states or cities. I added the onChange form submit so that the visitor wouldn't have to click the submit button to see more of the form display.

It seems like a form that displays a country drop down box that when selected displays associated states or cities would be pretty common. However, I can't figure out a really good way to do it. I am starting to wonder if my form shouldn't be split into multiple php files, but then a lot of the files would contain similiar code and I believe it would be more confusing to work on.

My best solution so far that I am implementing is if $member_site_city contains a value then process the form. I don't think it is a perfect solution because if the visitor forgets to choose a city and submits the form then it will just display the form again instead of displaying an error message.

- Ben
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top