Forum Moderators: coopster

Message Too Old, No Replies

Trip Report PHP

         

outdoorxtreme1

5:02 pm on Oct 18, 2005 (gmt 0)

10+ Year Member



I am a newbie to PHP. How would I go about creating a trip report submission.

Any help would be greatly appreciated. I want to create trip reports for my canoe club webpage.

Thanks in advance.

Dustin

[edited by: jatar_k at 5:28 pm (utc) on Oct. 18, 2005]
[edit reason] no urls thanks [/edit]

jatar_k

5:52 pm on Oct 28, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you can have php do all of your error checking at the top of your submit.php script before it does the insert into the db

outdoorxtreme1

5:54 pm on Oct 28, 2005 (gmt 0)

10+ Year Member



Ya. I would like to do that. What type of code do I need for error checking?

jatar_k

6:25 pm on Oct 28, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



the first step would be to check the required fields to make sure they are not empty

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.

outdoorxtreme1

6:40 pm on Oct 28, 2005 (gmt 0)

10+ Year Member



I tried the code and it still posted with the triplocation empty.

<?
$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");

?>

jatar_k

6:51 pm on Oct 28, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



alright, I'll give a little more on this one

what fields would you like to be required?

and what is the filename of the page with the form on it?

outdoorxtreme1

6:56 pm on Oct 28, 2005 (gmt 0)

10+ Year Member



triplocation, author, report

form.html

jatar_k

7:49 pm on Oct 28, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



take a look at this

<?
// 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. ;)

outdoorxtreme1

8:13 pm on Oct 28, 2005 (gmt 0)

10+ Year Member



It still didn't seem to work. went right on thru and posted.

ergophobe

9:05 pm on Oct 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Is it including the form?

outdoorxtreme1

10:01 pm on Oct 28, 2005 (gmt 0)

10+ Year Member



no it goes right to the header location. Any ideas?

ergophobe

10:33 pm on Oct 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes, your $_POST variables are not empty. Now, why could that be? Not sure. What happens if you put this above the database stuff (in the else block)

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.

outdoorxtreme1

5:25 pm on Oct 30, 2005 (gmt 0)

10+ Year Member



I finally got that part of it working. Is there a way to keep what had been entered in certain fields instead of clearing all of the fields and having to start over?

ergophobe

9:57 pm on Oct 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Let's say validation fails and you get thrown back to the form. You still have all the values in your $_POST array so

<input name="report" value="">

becomes

<input name="report" value="<? if (isset($_POST['report'])) { echo $_POST['report'];?>">

outdoorxtreme1

1:18 am on Oct 31, 2005 (gmt 0)

10+ Year Member



I tried this and it prints <? if (isset($_POST['triplocation'])) { echo $_POST['triplocation'];?>

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'];?>" />

ergophobe

2:50 am on Oct 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Your page is not parsing PHP. If it is named somepage.html, you need to either rename it to somepage.php or set up your server to parse .html files as .php

The easiest, of course, is to rename it.

outdoorxtreme1

3:05 am on Oct 31, 2005 (gmt 0)

10+ Year Member



I renamed it now it gives me an error when I try to use the code you told me in the field described.

Here is the error

Parse error: parse error, unexpected $ in /home/neohoutd/public_html/tct/form.php on line 278

AlexK

6:01 am on Oct 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Slightly different code to do the same thing; uses the ternary operator:
<check-expression>? <if-true-return-this> : <if-false-return-this>

If you wish to mix HTML and PHP-code in the same script (note that the PHP is on a separate line, which should also fix the original problem):
<input type="text" size="29" maxlength="50" name="triplocation" value="
<? echo ( isset($_POST['triplocation']))? $_POST['triplocation'] : '' );?>
" />

I prefer to keep a script PHP-script only if possible:

<?php
echo "<input type='text' size='29' maxlength='50' name='triplocation' value='". ( isset($_POST['triplocation']))? $_POST['triplocation'] : '' ) ."' />";
?>

The echo statement is using a mixture of double-quotes and single-quotes, which are difficult to distinguish in the font used in this forum (at least on my browser).

outdoorxtreme1

3:28 pm on Oct 31, 2005 (gmt 0)

10+ Year Member



This still isn't working. Is it because it is inside a table?

outdoorxtreme1

3:48 pm on Oct 31, 2005 (gmt 0)

10+ Year Member



Seems like to code given to do this is not quite right. Anyone have any suggestions?

outdoorxtreme1

3:53 pm on Oct 31, 2005 (gmt 0)

10+ Year Member



I think i got it.

Here is the code that seems to work.

<input type="text" size="29" maxlength="50" name="triplocation" value="
<? echo $_POST['triplocation'];?>
"

Could someone tell me if this seems ok?
Thanks.

jatar_k

4:19 pm on Oct 31, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



that is fine

outdoorxtreme1

4:51 pm on Oct 31, 2005 (gmt 0)

10+ Year Member



How do I get my form to say "SORRY: There is a problem with information you've submitted" if any or all of the required fields are empty?

jatar_k

4:56 pm on Oct 31, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



did you get it to echo the $errmsg var yet?

outdoorxtreme1

5:01 pm on Oct 31, 2005 (gmt 0)

10+ Year Member



yes. I have all of my other questions answered.

outdoorxtreme1

5:10 pm on Oct 31, 2005 (gmt 0)

10+ Year Member



I actually got it to work myself. I think I am starting to learn something! I am tring to echo "Please complete your report and "Submit" again..." after the error messages but it is putting it above the messages for the fields.

outdoorxtreme1

5:11 pm on Oct 31, 2005 (gmt 0)

10+ Year Member



See anything wrong here?

<?
// 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 {

jatar_k

5:44 pm on Oct 31, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



echo '</ul>';
echo '<font color=#FF0000>Please complete your report and "Submit" again...<br></font>';

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

outdoorxtreme1

6:11 pm on Oct 31, 2005 (gmt 0)

10+ Year Member



Is there a way to have the date reflect the current date on my form page?

jatar_k

6:24 pm on Oct 31, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



yes

there are a range of date functions [php.net]

you might want to look at
[php.net...]

outdoorxtreme1

6:33 pm on Oct 31, 2005 (gmt 0)

10+ Year Member



Will the code work with the dropdown fields for the date?
This 158 message thread spans 6 pages: 158