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]

outdoorxtreme1

3:40 pm on Oct 27, 2005 (gmt 0)

10+ Year Member



I have the first 2 rows in MySQl with data entered. I went in and manually deleted a bunch of other entries that I was using for testing. Even though I deleted everything after row 2 the next entry I make starts on a number after everything I deleted. I have rows 1,2,64,65 Is this normal after deleting rows. The number just keeps going no matter what was deleted in between? Also on my report_list.php The trips are out of order. Here is the order they are currently in 1,2,64,65.

jatar_k

3:51 pm on Oct 27, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> he number just keeps going no matter what was deleted in between

yes it is, mysql keeps track of the last num and the next is always greater than the last one it used, I just leave it as is and let it take care of things

there is a way to reset it but then you run the risk of errors because of attempted dupe entries

>> The trips are out of order.

order doesn't matter when you select them you can put them in any order you want based on a certain criteria using ORDER BY

example
SELECT * FROM tripreport ORDER BY TSpickYear DESC

this will order tham by the year in descending order. You can even use more than one column in the ORDER BY clause and also use ASC for ascending order

rows in the db don't need an order, as we can sort them however we want when we select

sorting rows [dev.mysql.com]
using ORDER BY in a SELECT [dev.mysql.com]

outdoorxtreme1

4:15 pm on Oct 27, 2005 (gmt 0)

10+ Year Member



Ok, 2 minor things I can't seem to get.
The new trips still dont show up untill a refresh and if there are different start and end dates the end date drops down to the next line. I would like to keep it on the same line.

Here is my code to check out:

<html>
<head>
<title>Trip Report List</title>
<meta http-equiv="pragma" content="no-cache">
</head>
<body>
<div>
<?
$host = "";
$user = "";
$pass = "";
$dbname = "";

$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");
mysql_select_db($dbname);

$sql = 'SELECT * FROM tripreport ORDER BY TSpickYear, TSpickMonth, TSpickDay ASC';
$result = mysql_query($sql) or die ('<p>select died: ' . mysql_error());

echo '<center><strong>Trip Reports</strong></center><br>';
echo '<br>';
while ($row = mysql_fetch_array($result)) {
$startdate = $row['TSpickMonth'] . ' ' . $row['TSpickDay'] . ', ' . $row['TSpickYear'];
$enddate = $row['TFpickMonth'] . ' ' . $row['TFpickDay'] . ', ' . $row['TFpickYear'];
echo '<strong><a href="report.php?trip=',$row['trip_id'],'">',$row['triplocation'],'</a></strong> - ', $startdate,'<br>';
if ($enddate!= $startdate) echo ' to ',$enddate,'<br>' ;
}
echo '<br>';
echo '<br>';
echo '<a href="javascript:history.back(1)">
Back</a>';

?>
</div>
</body>
</html>

jatar_k

4:26 pm on Oct 27, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



remove the break after $startdate. Since you want a break at the end of that line all the time put it after your if instead

echo '<strong><a href="report.php?trip=',$row['trip_id'],'">',$row['triplocation'],'</a></strong> - ', $startdate;
if ($enddate!= $startdate) echo ' to ',$enddate;
echo '<br>';

for the refreshing thing I am really not sure, something must be funny with the logic

first thing, I would try it in another browser than IE (firefox or mozilla) to see if that is the problem. If the behaviour is the same then the script is doing something funny.

your html looks like it is all present and accounted for now though ;)

outdoorxtreme1

4:28 pm on Oct 27, 2005 (gmt 0)

10+ Year Member



I figured out the date issue. Had to take out the <br> at the end of each date line and and an extra echo '<br>'; after

Still can't get it to refresh automatically though.

outdoorxtreme1

4:34 pm on Oct 27, 2005 (gmt 0)

10+ Year Member



Is there a way to add button to the full report to be able to delete that report using a password?

jatar_k

4:43 pm on Oct 27, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



maybe not button but you could add a link to a script that does deleting

as far as login goes, now you are breaching a whole new topic

try this library thread
[webmasterworld.com...] msg3 and be sure to follow the link to peer code review as well

outdoorxtreme1

5:16 pm on Oct 27, 2005 (gmt 0)

10+ Year Member



I'll have to get into that later. Is there a way to make a button on my trip submit that would clear all the fields if errors were made so that the info could be entered correctly before submitting?

jatar_k

5:22 pm on Oct 27, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



your script that is the action for your submit form can do some error checking and reinclude the form and repopulate the form fields if errors were found

the error logic would work something like this

1. check each field
this could be empty checks as we have done before, or type checks

2. for each validation problem add an error message to a string so the user can fix that field

3. reinclude the original form

4. echo the error message at the top of your form

5. echo previously entered values in the value="" portion of your form

make sense?

outdoorxtreme1

5:39 pm on Oct 27, 2005 (gmt 0)

10+ Year Member



I have this script so far. How do I get it to interact with the form?

<!--
var date = new Date();

function validate(screen) {
if (screen=="Submit") {
return true;//overrides the validation...
var errmsg = "";
with (document.forms[0]) {
if (!triplocation.value) errmsg += "\nYour trip report has no <title>.";
if (!author.value) errmsg += "\nPlease identify the report <author>.";
if (!report.value) errmsg += "\nPlease fill in the report <body> section.";
}
if (errmsg) {
alert("Sorry...\nYou have not filled out the form completely.\n" + errmsg + "\n\nPlease complete the form and 'SUBMIT' again...");
return false;
}
}
return true;
}

//-->

jatar_k

5:45 pm on Oct 27, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



that looks like javascript

I think you change your submit button to just a button, then use an onclick event to call that function

outdoorxtreme1

5:46 pm on Oct 27, 2005 (gmt 0)

10+ Year Member



Ya, I pasted the wrong script

outdoorxtreme1

5:50 pm on Oct 27, 2005 (gmt 0)

10+ Year Member



I am really confused about the submit button and how it works with the javascript and php submit code. I'm trying to do too many things at once I guess. I'm starting to bluescreen. :)

outdoorxtreme1

6:18 pm on Oct 27, 2005 (gmt 0)

10+ Year Member



Here is what I have on my submit form page so far.
I took out the sections where all the field entries are to cut the code short. Do you see any problems?

<html>
<head>
<title>Submit a Trip Report</title>
<script type="text/javascript">
<!--
var date = new Date();

function validate(screen) {
if (screen=="Submit") {
return true;//overrides the validation...
var errmsg = "";
with (document.forms[0]) {
if (!triplocation.value) errmsg += "\nYour trip report has no <title>.";
if (!author.value) errmsg += "\nPlease identify the report <author>.";
if (!repot.value) errmsg += "\nPlease fill in the report <body> section.";
}
if (errmsg) {
alert("Sorry...\nYou have not filled out the form completely.\n" + errmsg + "\n\nPlease complete the form and 'SUBMIT' again...");
return false;
}
}
return true;
}
//-->
</script>
</head>
<body>
<!--UNIQUE PAGE CONTENT-->
<form method="post" action="/tct/submit.php">

<TABLE WIDTH="273">
<TR>
<td valign="top">
<p>
<input id="Submit" type="submit" style="font-size:smaller;width:7em;" name="screen" value="Submit" onclick="self.clicked=this.value" />&nbsp;
</td>
</tr>
</table>
</form>
<!--END OF UNIQUE CONTENT-->
</body>
</html>

outdoorxtreme1

6:21 pm on Oct 27, 2005 (gmt 0)

10+ Year Member



Also, can I put the submit.php code on this page or does it have to be by itself?

netchicken1

6:45 pm on Oct 27, 2005 (gmt 0)

10+ Year Member



My goodness what a lot of work for nothing!

Just download and install a free messageboard and then modify the catagories / forums to do what you want.

Probably 3 hours from go to whoa work. Why reinvent the wheel?

outdoorxtreme1

6:48 pm on Oct 27, 2005 (gmt 0)

10+ Year Member



I'm not trying to make a message board. I am creating a form where I can enter information about kayaking trips my group does and have it display the information back depending on what trip you would like to see a report on.

ergophobe

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

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



Since netchicken has brought it up again, did you look at Drupal with the flexinode module installed that I mentioned earlier? The off the shelf distro with flexinode added will literally do exactly what you want and it's about 20 minutes from download to having your flexinode categories set up.

Since drupal is written in PHP, modifying it (or any other large package like it) is also a good way to learn a lot of PHP. The templating system uses very simple PHP, and then some components get fairly complex. You can get deeper and deeper in as your confidence grows.

jatar_k

10:48 pm on Oct 27, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> can I put the submit.php code on this page or does it have to be by itself?

you can do either, my preference is to have it as a seperate script and include the original form on error or forward using the header function on success.

>> My goodness what a lot of work for nothing!

I seldom find learning an excercise in futility.

The process that outdoorxtreme1 is learning is fairly basic and lays out a lot for continued development in php and mysql.

ergophobe

11:24 pm on Oct 27, 2005 (gmt 0)

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




you can do either, my preference is to have it as a seperate script and include the original form on error or forward using the header function on success.

Ditto. Among other things, it makes double submissions less likely (to prevent entirely takes additional measures). Generally, the flow for me would be

1. If there's data, send it on to be validated and processed.
2. If the validation/processing succeeded, redirect to a success page or some such.
3. If validation fails, stay where I am and show the form again and repost the data submitted by the user so she doesn't have to type it all back in.

There are exceptions. The site that's open in the other tab of my browser has no purpose but to enter/edit data. In that case, it just goes back to the same page.

I seldom find learning an excercise in futility.

That's because like most of us who frequent this place, we tend to think "I could do this by hand in an hour and I'll never do it again, but I wonder if I could write a script that would do this automatically." Bleery-eyed and finally looking up at midnight, you have this nice little smile and think "Wow, how cool is that? Glad I didn't just do that by hand!" Many folks, though, would rather be up and running ASAP.

That's why I initially asked if outdoorextreme's goal is first and foremost to learn PHP or first and foremost to get his trip report thing running. I did not mean it as a rhetorical question, but just trying to figure out what his priorities are.

outdoorxtreme1

11:40 pm on Oct 27, 2005 (gmt 0)

10+ Year Member



I want to get it going but I don't really have a time frame. It is nice to be able to see how things work and why.

jatar_k, do you see anything that is wrong with the above code or anything missing?

Thanks.

ergophobe

1:26 am on Oct 28, 2005 (gmt 0)

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




I want to get it going but I don't really have a time frame.

Ha! I've got a lot of those projects!

I'm not jatar_k, but the code in your last seems fine as far as PHP and HTML. I wouldn't purport to say anything one way or the other on the Javascript without testing since I don't "sight read" javascript ;-)

Is something not working?

[edit]
if (!repot.value) errmsg += "\nPlease fill in the report <body> section.";

I'm guessing that's a typo?
[/edit]

outdoorxtreme1

11:40 am on Oct 28, 2005 (gmt 0)

10+ Year Member



It seem like the javascript part of it is not working. If I leave the field defined empty it should let me know that the fields are empty. It doesn't tell me though and it posts the report.

danielNorge

12:46 pm on Oct 28, 2005 (gmt 0)

10+ Year Member



Hi. I'd just like to say, as basic as outdoorxtreme1's questions might be, I, for one, have learned and applied some of jatar_k's solutions. Thanks!

Don't mean to hijack outdoor's thread, but maybe I could ask a quick question. I'm doing something similar to outdoor's results page, but I start with a case statement, then break down my queries depending on what the form results were. When I stuff everything into a var (for example...

<?php
$graphic = $row['id'],' ',$row['some_stuff'],' ',$row['other_stuff'],' ',$row['even_more_stuff'],'<br>';
?>

<inHtmlCodeSomewhere>
<?php echo ($graphic);?>

...it bombs. Yet when I take the exact same query, and replace the var $graphic with echo, it works (albeit at the top of my page, where I don't want it).

Should I be using a different function than echo, or maybe a better way of doing this?

Any tips in the right direction greatly appreciated!

Daniel

jatar_k

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

WebmasterWorld Administrator 10+ Year Member



>> It seem like the javascript part of it is not working

you need to call the js function somehow either by using the onsubmit event or changing your submit button to a regular button and using the onclick event

Welcome to WebmasterWorld danielNorge

if you are putting all those vars into another var you can't use the comma you must use the concatenation operator like so

$graphic = $row['id'] . ' ' . $row['some_stuff'] . ' ' . $row['other_stuff'] . ' ' . $row['even_more_stuff'] . '<br>';

the comma can only be used with echo

outdoorxtreme1

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

10+ Year Member



What do I need to delete to checnge it to a regular button?

jatar_k

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

WebmasterWorld Administrator 10+ Year Member



you might even be able to use the onclick in the submit button, my uses of js are few and far between anymore

<input id="Submit" type="submit" style="font-size:smaller;width:7em;" name="screen" value="Submit" onclick="self.clicked=this.value" />

though it looks like it should work, not really sure. You may want to post that to the javascript forum [webmasterworld.com] and see if someone can help with that. My js is way too rusty to be right.

maybe just post what you have in message 74.

ergophobe

4:36 pm on Oct 28, 2005 (gmt 0)

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



Like I said, my JS skill is pretty poor, though I have actually been doing just that for the last few days. Of course, if you saw the results... ;-) Jatar_K doesn't sound much better. If this doesn't help, my apologies and please head over to the Javascript forum where someone should have a clue what they're talking about!

Anyway, I have something similar with a function called submitForm in a separate file with all my other JS. In my case, it's not for validation, but it checks whether data has been changed and, if so, submits the form when the user clicks on the "next" and "previous". I'm not sure if "return false;" stops a form submission in the same way that it stops a link, but I assume it does.

So, a few things
- I assume you fixed the typo I mentioned?

- unless you are registering the event somewhere (in an onload script), you are not firing the validate function and, even if it is registered, I guess it would get overridden by the action you define in the onclick.

I would have something like this


<input id="Submit" type="submit" name="screen" value="Submit" onclick="validate()" />

Then for the function itself, I've had better luck with assigning an ID to every form element (usually just use the same as the name) and using getElementById like this:



var errmsg = "";
var elTriploc = getElementById('triplocation');
var elAuthor = getElementById('author');
var elReport = getElementById('report');

if (!elTriploc.text)
{
errmsg += "\n *Trip Location.";
}
if (!elAuthor.text)
{
errmsg += "\n *Report author.";
}
if (!elReport.text)
{
errmsg += "\n *Trip report.";
}

if (errmsg)
{
alert("Sorry...\nYou have not filled out the form completely.
The following required fields are missing:\n"
+ errmsg + "\n\nPlease complete the form and 'SUBMIT' again...");

return false;
}

return true;


This should stop the submit (return false) on error and allow the submit to go through otherwise.

For users with JS turned off, you'll want to do PHP validation server side and bump them back to the form if there's a problem (we sort of hit on that further up RE where to submit).

If you want to just not permit users with JS turned off to user your form, then you would make the submit button and make it a non-functional button that merely fires the JS function and then you would use the submit method in JS as in

var myForm = getElementById('myForm');
myForm.submit;

before returning true.

A few notes

- I don't know about the this.value. Shouldn't that always be "Submit" in which case your conditional at the top is always true and it always submits? This version doesn't use it.

- I use the .text because I have had bad luck with the .value property always returning NULL (undefined) in some browser (can't remember if it was IE or Firefox).

- I use the getElementById('myId') form because it has been more cross-browser friendly for me than using the collection of elements from the form.

I hope that gets it sorted, but if not, head over to the JS forum and come back here if you have more questions on the PHP side.

[edited by: ergophobe at 8:57 pm (utc) on Oct. 28, 2005]

outdoorxtreme1

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

10+ Year Member



I'm going to have to see if I can get it solved in the javascript forum. It still posted even with the fields empty

outdoorxtreme1

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

10+ Year Member



Is there a better way to do this same idea instead of using javascript?
This 158 message thread spans 6 pages: 158