PDA

View Full Version : prevent post action file send empty email


javierrebelde
02-03-2010, 09:07 PM
Hi,

I got this problem i create a form file "reservation.php" and a action file "sendreservation.php" ok this is what i need.

It is anyway i can prevent "sendreservation.php" send a empty email if someone access that file directly example:

http://www.mydomain.com/sendreservation.php

edbr
02-03-2010, 11:49 PM
if ($_SERVER["REQUEST_METHOD"] <> "POST")
die("<h1>You can only reach this page by posting from the html form<h1>");
maybe

javierrebelde
02-04-2010, 06:50 AM
Hmm. I need more info because i dont undertand what i have to do with that. Btw the posting page is php not html

edbr
02-04-2010, 07:50 AM
just put it at the begining your php block
it may not be enough it just will show the message if the file is access directly
change the message to what you want

tux
02-04-2010, 07:36 PM
Try this, its a page with a simple form to collect name and email and a page to display the results. When the submit button is clicked it will display the results in sendmail.php. Not added any send mail function its just to validate the form so that all fields are filled in but will also redirect back to the form if you try to view sendmail.php directly.

form.php....

<?php

$error = $_GET['error'];

if($error){
$error = "You must fill in all fields!";
}else $error = "";

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php echo $error; ?>
<form name="form1" method="post" action="sendmail.php">

<input type="hidden" name="submitted" id="submitted" value="1">
<p>
Name
<input type="text" name="name" id="name" value="">
</p>
<p>
Email
<input type="text" name="email" id="email" value="">
</p>
<p>
<input type="submit" name="Submit" value="Submit">
</p>
</form>
</body>
</html>

Notice the hidden field!


sendmail.php....

<?php

$submitted = $_POST['submitted'];
$name = $_POST['name'];
$email = $_POST['email'];

if($submitted && $name && $email){

echo $name."<br>";
echo $email;

}elseif(!$submitted || $submitted){
header("location:form.php?error=1");
exit;
}

?>

Not sure if its what you are after but hope it helps.

Regards Paul

davidj
02-09-2010, 05:49 AM
I would use tux's suggestion however there is no need to implement a hidden field


$submit= $_POST['Submit'];
$name = $_POST['name'];
$email = $_POST['email'];

if($submit && $name && $email){

By using $_POST[Submit] you are catching the submit button value which is also passed when submitted

tux
02-09-2010, 06:03 AM
Thats interesting Dj, I have always used a hidden field since it was used in your vids when you started teaching me.

Trims off a line of code, so even shorter scripts. Cheers mate.

Paul

davidj
02-09-2010, 04:49 PM
Those vids are sooooo old

id do them a bit different next time

tux
02-09-2010, 07:45 PM
They might be old but they are still a great way to learn PHP for people starting out....

.... apart from http://codezenith.co.uk/ of course.

If it wasn't for those OLD tuts I don't think I would have plunged into the sea of PHP.

domedia
02-09-2010, 09:07 PM
An old version of this was my first exposure to it:
http://articles.sitepoint.com/article/php-amp-mysql-1-installation