Forum Moderators: coopster
all of the values will be available in the $_POST array, as you already know
so, at the top of your script you need to check the submitted values
what are the form element names you want to be required? Check each of those with empty first. Use a single var to store your error messages. Initialize it at the beginning
$errmsg = '';
then have your multiple checks, each similar to this
if (empty($_POST['triplocation'])) {
$errmsg .= '<br>Please enter your trip location';
}
you'll notice I used .= there, we want to append to the variable, not replace what is in it. This would mean if it fails every check then there would be a string in that var for every error.
then at the end of your checks you test to see if $errmsg is empty. If it is then there were no errors and you can continue with your db stuff. If there is anything in it then there were one or more errors and you need to re include your form and echo the error var.
<?
$host = " ";
$user = " ";
$pass = " ";
$dbname = " ";
$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");
mysql_select_db($dbname);
$errmsg = '';
if (empty($_POST['triplocation'])) {
$errmsg .= '<br>Please enter your trip location';
}
$sql_query = mysql_query("INSERT INTO tripreport(triplocation, rivercondition, riverclass, triporganizer, GaugeFT, GaugeCFS, TSpickMonth,
TSpickDay, TSpickYear, GaugeID, TFpickMonth, TFpickDay, TFpickYear, author, email, k1, k2, c1, c2, air, raft, report) VALUES ('$triplocation',
'$rivercondition', '$riverclass', '$triporganizer', '$GaugeFT', '$GaugeCFS', '$TSpickMonth', '$TSpickDay', '$TSpickYear', '$GaugeID',
'$TFpickMonth', '$TFpickDay', '$TFpickYear', '$author', '$email', '$k1', '$k2', '$c1', '$c2', '$air', '$raft', '$report')") or die (mysql_error());
header("Location: /tct/trip_reports.html");
?>
<?
// initialize var for storing possible error messages
$errmsg = '';
// check triplocation
if (empty($_POST['triplocation'])) {
$errmsg .= '<br>Please enter your trip location';
}
// check author
if (empty($_POST['author'])) {
$errmsg .= '<br>Please enter the author';
}
// check report
if (empty($_POST['report'])) {
$errmsg .= '<br>Please enter your report';
}
// check to see if anything was placed in our error var, meaning there was an error
if (!empty($errmsg)) {
include [php.net] 'form.html';
} else {
$host = " ";
$user = " ";
$pass = " ";
$dbname = " ";
$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");
mysql_select_db($dbname);
$sql_query = mysql_query("INSERT INTO tripreport(triplocation, rivercondition, riverclass, triporganizer, GaugeFT, GaugeCFS, TSpickMonth,
TSpickDay, TSpickYear, GaugeID, TFpickMonth, TFpickDay, TFpickYear, author, email, k1, k2, c1, c2, air, raft, report) VALUES ('$triplocation',
'$rivercondition', '$riverclass', '$triporganizer', '$GaugeFT', '$GaugeCFS', '$TSpickMonth', '$TSpickDay', '$TSpickYear', '$GaugeID',
'$TFpickMonth', '$TFpickDay', '$TFpickYear', '$author', '$email', '$k1', '$k2', '$c1', '$c2', '$air', '$raft', '$report')") or die (mysql_error());
header("Location: /tct/trip_reports.html");
}
?>
so that will check the 3 fields then re include the form if there is an error. if there isn't an error it will drop into the else portion and connect to the db and do the insert. I could also have just used die(); [php.net] right after the include but just did it this way instead. Both methods are used to stop script execution after the error is found. You don't want it to go on and insert the blank stuff anyway.
The include will work so long as form.html is in the same directory as this script. If it is not you will have to give the appropriate path.
aside from that, now you have to echo the $errmsg over top of your form so the user knows what is wrong.
I usually do this
if (isset [php.net]($errmsg) &&!empty($errmsg)) {
echo '<p>',$errmsg;
}
now, read all the links for the functions so you know what they do. Also something new we hadn't looked at before &&. It means and so both of those criteria in our if statement need to be met for that code inside to be executed. This bit of code will sit on your form but will only be executed when there is an error.
Now you just have to figure out how to repopulate yoour form elements from the $_POST array using echo's in the value="" portion of your form elements.
this thread might be of use if you are doing the date dropdowns
[webmasterworld.com...] msg 5
don't just slam it into your form. Put it on it's own page first and see how it works. ;)
echo '<pre>**' . $_POST['report'] . '**
**' . $_POST['author'] . '**
**' . $_POST['triplocation'] . '**
</pre>';
You'll get a "headers already sent" error, but we're just trying to figure out what's in your $_POST array that might be letting it through. Could be spaces (that's why the ** and <pre>), could be some data you're not expecting.
If all you get is lines of **** with no spaces, then you're doing something unexpected with jatar_k's code.
in the field described.
Any other suggestions?
Here is what I have:
<input type="text" size="29" maxlength="50" name="triplocation" value="<? if (isset($_POST['triplocation'])) { echo $_POST['triplocation'];?>" />
<check-expression>? <if-true-return-this> : <if-false-return-this>
<input type="text" size="29" maxlength="50" name="triplocation" value="
<? echo ( isset($_POST['triplocation']))? $_POST['triplocation'] : '' );?>
" />
<?php
echo "<input type='text' size='29' maxlength='50' name='triplocation' value='". ( isset($_POST['triplocation']))? $_POST['triplocation'] : '' ) ."' />";
?>
<?
// initialize var for storing possible error messages
$errmsg = '';
echo '<font color=#FF0000>SORRY: There is a problem with information you have submitted:</font>';
echo '<ul>';
// check triplocation
if (empty($_POST['triplocation'])) {
$errmsg .= '<font color=#FF0000><li>Your report has no [trip location]</li></font><br>';
}
// check author
if (empty($_POST['author'])) {
$errmsg .= '<font color=#FF0000><li>Please identify the [trip report author]</li></font><br>';
}
// check report
if (empty($_POST['report'])) {
$errmsg .= '<font color=#FF0000><li>Please fill in your [trip report]</li></font><br>';
}
echo '</ul>';
echo '<font color=#FF0000>Please complete your report and "Submit" again...<br></font>';
// check to see if anything was placed in our error var, meaning there was an error
if (!empty($errmsg)) {
include 'form.php';
} else {
the two above lines are echo'ed then you don't echo the $errmsg var until you include form.php
so append those two string to your $errmsg var instead of echo'ing them, like so
$errmsg .= '</ul>';
$errmsg .= '<font color=#FF0000>Please complete your report and "Submit" again...<br></font>';
then when you echo $errmsg it will all be right