Page 2 of 2
Posted: Mon Jul 12, 2004 2:28 am
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..
Posted: Mon Jul 12, 2004 3:21 am
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;
Posted: Mon Jul 12, 2004 3:26 am
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;
}
Posted: Mon Jul 12, 2004 3:27 am
by furiousweebee
lol ok so what can I do about it?
Posted: Mon Jul 12, 2004 3:36 am
by feyd
Code: Select all
default:
$data[$k] = $v;
$valid[$k] = true; //preg_match # <-------- this line
break;
}
Posted: Mon Jul 12, 2004 3:43 am
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?
Posted: Tue Jul 13, 2004 8:57 am
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
<form enctype="multipart/form-data" action="street_team.php" method="post">
<table width="100%" border="0" cellspacing="0" cellpadding="2">
<tr valign="top">
<td width="34%">First Name:</td>
<td width="66%"><input class="input" name="first_name" type="text" size="25"></td>
</tr>
<tr valign="top">
<td>Last Name:</td>
<td><input class="input" name="last_name" type="text" size="25"></td>
</tr>
<tr valign="top">
<td>Birth Year:</td>
<td><input class="input" name="birth_year" type="text" size="4"></td>
</tr>
<tr valign="top">
<td>Email Address:</td>
<td><input class="input" name="email" type="text" size="35"></td>
</tr>
<tr valign="top">
<td>Postal Address:</td>
<td><input class="input" name="address" type="text" size="35"></td>
</tr>
<tr valign="top">
<td>City:</td>
<td><input class="input" name="city" type="text" size="20"></td>
</tr>
<tr valign="top">
<td>State / Province:</td>
<td><input class="input" name="state_province" type="text" size="20"></td>
</tr>
<tr valign="top">
<td>Zip / Post Code: </td>
<td><input class="input" name="zip" type="text" size="8"></td>
</tr>
<tr valign="top">
<td>Country:</td>
<td><select size="1" name="country" class="input">
<option selected>Other</option>
<option>Argentina</option>
<option>Australia</option>
<option>Belgium</option>
<option>Brazil</option>
<option>Canada</option>
<option>Chile</option>
<option>China</option>
<option>Colombia</option>
<option>Croatia</option>
<option>Czech Republic</option>
<option>Denmark</option>
<option>Ecuador</option>
<option>England</option>
<option>Finland</option>
<option>France</option>
<option>Germany</option>
<option>Great Britain</option>
<option>Hungary</option>
<option>Ireland</option>
<option>Italy</option>
<option>Japan</option>
<option>Mexico</option>
<option>N Ireland</option>
<option>Netherlands</option>
<option>New Zealand</option>
<option>Norway</option>
<option>Portugal</option>
<option>Russia</option>
<option>Scotland</option>
<option>South Africa</option>
<option>Spain</option>
<option>Sweden</option>
<option>Switzerland</option>
<option>Uruguay</option>
<option>USA</option>
<option>Wales</option>
</select></td>
</tr>
<tr valign="top">
<td>Continent</td>
<td><input class="input" name="continent" type="text" size="20"></td>
</tr>
<tr valign="top">
<td>Phone Number:</td>
<td><input class="input" name="phone" type="text" size="20"></td>
</tr>
<tr valign="top">
<td>Nearest Major City:</td>
<td><input class="input" name="major_city" type="text" size="20"></td>
</tr>
<tr valign="top">
<td>Favorite Venues/Hangouts:</td>
<td><textarea rows="5" name="faves" cols="30" class="input"></textarea></td>
</tr>
<tr valign="top">
<td>Comments:</td>
<td><textarea rows="5" name="comments" cols="30" class="input"></textarea></td>
</tr>
<tr valign="top">
<td colspan="2" align="center"><input type="submit" value="Join The Team!" name="street_team_data">
</td>
</tr>
</table>
</form>
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";
}
?>
Posted: Tue Jul 13, 2004 9:57 am
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.
Posted: Tue Jul 13, 2004 11:25 am
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.
Posted: Tue Jul 13, 2004 11:40 am
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.
Posted: Tue Jul 13, 2004 11:47 am
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.
Posted: Tue Jul 13, 2004 9:30 pm
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.