Click to See Complete Forum and Search --> : PHP in HTML or entire PHP file?


haynbrian
11-23-2003, 02:20 AM
Hello all.

I have a question. I'm creating a website that has mailing list and inquiry forms on it (each form on a separate page). My goal is to get the information to submit and after the action is taken the same page is showing, only cleared textboxes where the information was, and the information has been sent.

The only way I could think to do this was to make the entire page a PHP file called thisPHPfile.com. When the submit button is pushed, it sends the information to the appropriate e-mail address in the same thisPHPfile.php document. That is to say, the attribute in the form is [action="thisPHPfile.php].

My problem with this is everytime I visit the page, the information is sent to my e-mail address. It's not only sent when I press the submit button, but everytime the page refreshes.

Below is what the code would look like:

<?php


$message="Name: txt_name/n/n".
"E-mail: txt_email".$text;


mail("abc@email.com","$message");
echo"
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
</head>

<body>
body content, pictures etc.
<form name='form1' method='post' action='thisPHPfile.php'>
Name:
<input name='txt_name' type='text' id='txt_name'><br><br>
E-mail:
<input name='txt_email' type='text' id='txt_email'><br>
<br>
<input type='submit' value='submit' name='submit'>
</form>
<br>
<br>
more content and pictures below.
</body>
</html>"

?>

I had thought of embedding the php into the html code, but I'm not sure how to do that, or if that would even solve my problem. I'm not too good at PHP right now, I'm trying to get better, so I apologize in advance for any errors my code has.

Does anyone know what I might be able to do?

Thanks,

Brian

johnalphaone
11-23-2003, 03:03 AM
The code that you want to run only when the submit button has been pressed should be like this:


if (isset($HTTP_POST_VARS['submit'])) {
//your code here
}

pyro
11-23-2003, 10:03 AM
Or, if your server supports PHP >= 4.1.0, you can (should) use the $_POST (http://us4.php.net/manual/en/reserved.variables.php#reserved.variables.post) superglobal instead of the depreciated $HTTP_POST_VARS.

haynbrian
11-24-2003, 01:29 PM
Thanks for your replies.

With the example you have given me:

if (isset($HTTP_POST_VARS['submit'])) {
// your code here
}


...what code should I be putting into the //your code here part? I tried creating it like this:

<?php
$message="Name: txt_name/n/n".
"E-mail: ".$text;

if (isset($HTTP_POST_VARS['submit'])) {
mail("brian@briwired.com","$message", "From:$txt_name");
echo"
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
</head>

<body>
body content, pictures etc.
<form name='form1' method='post' action='thisPHPfile.php'>
Name:
<input name='txt_name' type='text' id='txt_name'><br><br>
E-mail:
<input name='txt_email' type='text' id='txt_email'><br>
<br>
<input type='submit' value='submit' name='submit'>
</form>
<br>
<br>
more content and pictures below.
</body>
</html>";


}

?>


but I get nothing on the screen. Then I tried it like this:

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
</head>

<body>
body content, pictures etc.
<form name='form1' method='post' action='thisPHPfile.php'>
Name:
<input name='txt_name' type='text' id='txt_name'><br><br>
E-mail:
<input name='txt_email' type='text' id='txt_email'><br>
<br>
<input type='submit' value='submit' name='submit'>
</form>
<br>
<br>
more content and pictures below.
</body>
</html>

<?php
$message="Name: txt_name/n/n".
"E-mail: ".$text;

if (isset($HTTP_POST_VARS['submit'])) {
mail("brian@briwired.com","$message", "From:$txt_name");
echo"
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
</head>

<body>
body content, pictures etc.
<form name='form1' method='post' action='thisPHPfile.php'>
Name:
<input name='txt_name' type='text' id='txt_name'><br><br>
E-mail:
<input name='txt_email' type='text' id='txt_email'><br>
<br>
<input type='submit' value='submit' name='submit'>
</form>
<br>
<br>
more content and pictures below.
</body>
</html>";


}

?>

but after hitting the submit button I get a copy of what I want on the screen directly below the original.

I do want to make sure you know I'm trying this and not just wanting an answer, but where exactly would I use the "if" statement?

Thanks again for your help.

Brian

DaiWelsh
11-25-2003, 05:38 AM
Depends what you want it to do when they hit submit? If you want it to send the email then go back to the same page then the if should only be around the send mail part...

<?php
$message="Name: txt_name/n/n".
"E-mail: ".$text;

if (isset($HTTP_POST_VARS['submit'])) {
mail("brian@briwired.com","$message", "From:$txt_name");
}
?>
<html>
....
rest of page here
....

so it always shows the page, but only sends the mail if they have just clicked submit.

Remember this script will be called twice. The first time is when they click on the link to it and get shown the blank form, the second time (and any more times) is after they click the submit button.

HTH,

Dai

haynbrian
11-27-2003, 02:21 PM
Thanks for your help everyone, I got it to work.

Brian