...

View Full Version : PHP tutorial problem



Jimbolgs
11-21-2011, 07:59 PM
Can anybody help with this please it's a form validation exercise from a book I'm following.

If you remove lines 4 and 25 the script works, except it returns the error message I've built in on line 11 when the form has never been filled in.

Lines 4 and 25 are what the book tells me to add to stop this error message from happening when the page first loads, but then the script doesn't work.

Thanks in advance for any help.



<p class="body">

<?php
if (isset($_POST['submit'])) { //This is the start of the problem line
$from = 'me@gmail.com'; //What mail recipient will see as mail sender
$subject = $_POST['subject']; //Field from form on send_email.html
$message = $_POST['message']; //Field from form on send_email.html
$output = false; //Flag for whether to re-output form set to false

if (empty($subject) && empty($message)) {
echo '<b>Error:</b> Please enter a subject and message';
$output = true;
}

if (empty($subject) && (!empty($message))) {
echo '<b>Error:</b> Please enter a subject';
$output = true;
}

if ((!empty($subject)) && empty($message)) {
echo '<b>Error:</b> Please enter a message';
$output = true;
}

}//This closes the problem line

else {
$output = true;
}


if ((!empty($subject)) && (!empty($message))) {

$dbc = mysqli_connect('localhost', 'xxx', 'xxx', 'xxx') //Open database connection
or die('Error connecting to MySQL database'); //Or die trying

$query = "SELECT * FROM elvis_list"; //Query - select everything from the elvis_list table in the database above
$result = mysqli_query($dbc, $query) //'Result' is a new variable which is the result of the dbc and query above
or die('Error querying MySQL database'); //Or die trying

echo '<b>Email sent to: </b><br /><br />'; //

while ($row = mysqli_fetch_array($result)) { //Begin send email loop / where '$row' is a new variable / containing a row of info, or 'array' from '$result' variable
$to = $row['email']; //'$to' is a new variable using the 'email' column from '$row' above
$first_name = $row['first_name']; //'$first_name' is a new variable using the 'first_name' column from '$row' above
$last_name = $row['last_name']; //'$last_name' is a new variable using the 'last_name' column from '$row' above

$msg = "Dear $first_name $last_name, \n $message"; //'$msg' is a new variable containing the string "Dear 'see variables above'"

mail($to, $subject, $msg, 'from: ' . $from); //'mail' is a PHP command and the rest of this line is the variables with which to construct the message

echo $to . '<br />'; //Echo string containing 'to' varable back to page

}

mysqli_close($dbc); //Close database connection

}

if ($output) { //Output form if true

?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<p class="body"><label for="subject">Subject:</label>
<input type="text" id="subject" name="subject" value="<?php echo $subject ?>" /><br /></p>
<br />
<p class="body"><label for="message">Message:</label>
<textarea name="message" cols="50" rows="5" id="message"><?php echo $message ?></textarea>
<br /></p>
<br />
<p class="submit"><input type="submit" id="submit" value="Send message" /></p>
</form>

<?php

} //Jump back into PHP to close if statement

?>

</p>

Adee
11-21-2011, 08:12 PM
change


<p class="submit"><input type="submit" id="submit" value="Send message" /></p>

to

<p class="submit"><input type="submit" name="submit" id="submit" value="Send message" /></p>

Jimbolgs
11-21-2011, 08:20 PM
That's fixed it — thanks. I take my hat off to you guys that can respond to these questions so quickly. Tell me, do you test the code or are you so experienced you can just read it and spot the errors?

Thanks again.

tangoforce
11-21-2011, 08:37 PM
Your error was just a common one. It's a mixture of both. Sometimes we test peoples code, other times the error stands out by itself. It's just one of those things you get better at with experience.

Talking of experience, avoid even using if ($_POST['submit']) completely (Adee should of mentioned this). See my signature for the reason why and the link below the php code box for an indepth explanation.

Adee
11-21-2011, 08:40 PM
Your error was just a common one. It's a mixture of both. Sometimes we test peoples code, other times the error stands out by itself. It's just one of those things you get better at with experience.

Talking of experience, avoid even using if ($_POST['submit']) completely (Adee should of mentioned this). See my signature for the reason why and the link below the php code box for an indepth explanation.

i didn't mention it cause i knew for sure you would have LOL :D

tangoforce
11-21-2011, 08:44 PM
:thumbsup:

(YEah right :P)

Jimbolgs
11-21-2011, 08:56 PM
Thanks, I'll make a note of that for any 'real-life' future forms, but for now I'm very new to all this so I'll just carry on with my book.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum