PHP problem in script, A-Level coursework [urgent!]?

l00n3

New member
i have made a website for my a-level coursework, its ment to be a booking system where the public can submit the dates they want to rent out an apartment. Now the contact us form is written in php, it basically takes what information you enter and puts it into a text file wich is specified in the script, but somewere along the script there is an error because it stopped writing to the txt file but it doesnt give me an error saying it didnt work it redirects me to the page i chose after someone submits a form. the script is as follows...

<?php

if ($_POST['submit'] == "Submit") {
$dir = 'inbox/';
$filename = "booking.txt";

$content .= $_POST['firstname']." ";
$content .= $_POST['lastname']." /n/r E-Mail: ";
$content .= $_POST['email']." /n/r Phone: ";
$content .= $_POST['phone']." /n/r Holliday Dates: ";
$content .= $_POST['fromday']."/";
$content .= $_POST['frommonth']."/";
$content .= $_POST['fromyear']." to ";
$content .= $_POST['today']."/";
$content .= $_POST['tomonth']."/";
$content .= $_POST['toyear']." /n/r Number of People: ";
$content .= $_POST['number']." /n/r Comments: ";
$content .= $_POST['comment']."/n/r <===========================================> nr Name: ";

if (!$error) {
header("Location: pages/thankyou.html"); //change this to redirect to where u want it to
}

}
if (!fwrite($handle, $content)) {
echo 'Cannot write to file: ' . $filename;
exit;
}


?>

i dont know where to begin, i havent got much knowlage of php so i would be more than gratefull if someone can correct it and post it.

p.s. i have checked chmod file permission and set them to 777,

thanks in advanced, i apprechiate any help
johnathan
 
Well, I see a few errors.

/r/n should be \r\n

I think the main problem is undefined variables:



Add this before if(!$error)

$error = false;
foreach($_POST as $key => $value) {
$$key = trim(strip_tags($value));
if(empty($$key)) $error = true;
} else $error = false;

second is this:

after if (!$error) {
header("Location: pages/thankyou.html"); //change this to redirect to where u want it to
}

add this:

$handle = @fopen($filename, 'a');

One more important note, you should always "cleanse" data being recorded to text files/database, which is why I added trim(strip_tags($content))

and when testing scripts add this to the end of the file, before the closing PHP tags:

error_reporting(E_ALL);

then once you have tested the script, remove the error_reporting(E_ALL)

Hope this makes sense!
 
I think part of your problem here is that the $error variable is never being set... so your if(!$error) statement is always true, and thus always redirects you to the thank you page.

Additionally, the statement that writes to the file comes after your redirect, so it never actually executes.

You need something like:


<?php

if ($_POST['submit'] == "Submit") {
$dir = 'inbox/';
$filename = "booking.txt";

$content .= $_POST['firstname']." ";
$content .= $_POST['lastname']." /n/r E-Mail: ";
$content .= $_POST['email']." /n/r Phone: ";
$content .= $_POST['phone']." /n/r Holliday Dates: ";
$content .= $_POST['fromday']."/";
$content .= $_POST['frommonth']."/";
$content .= $_POST['fromyear']." to ";
$content .= $_POST['today']."/";
$content .= $_POST['tomonth']."/";
$content .= $_POST['toyear']." /n/r Number of People: ";
$content .= $_POST['number']." /n/r Comments: ";
$content .= $_POST['comment']."/n/r <=======================================… nr Name: ";

if (!fwrite($handle, $content)) {
echo 'Cannot write to file: ' . $filename;
exit;
}
else {
header("Location: pages/thankyou.html"); //change this to redirect to where u want it to
}


}

?>
 
amongst all of the other comments you know that your file will always be overwritten!
$handle is not defined. I suppose you mean it to be:
$handle = $dir . filename;
in which case you shouls

$handle = "inbox/booking.txt";

check out this example to help you open and write the file the right way
"<?php
$filename = 'test.txt';
$somecontent = "Add this to the file\n";

// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {

// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}

// Write $somecontent to our opened file.
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}

echo "Success, wrote ($somecontent) to file ($filename)";

fclose($handle);

} else {
echo "The file $filename is not writable";
}
?>"
http://us3.php.net/fwrite
 
amongst all of the other comments you know that your file will always be overwritten!
$handle is not defined. I suppose you mean it to be:
$handle = $dir . filename;
in which case you shouls

$handle = "inbox/booking.txt";

check out this example to help you open and write the file the right way
"<?php
$filename = 'test.txt';
$somecontent = "Add this to the file\n";

// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {

// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}

// Write $somecontent to our opened file.
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}

echo "Success, wrote ($somecontent) to file ($filename)";

fclose($handle);

} else {
echo "The file $filename is not writable";
}
?>"
http://us3.php.net/fwrite
 
Back
Top