IT.COM

PHP Form Script

Spaceship Spaceship
Watch
Impact
0
Ok this may sound like a silly question but I have written up a PHP form script which when filled out:

Sends the information in the form fields to the webmaster.

Sends an email to the person who filled out the form telling them it has been successfully filled out.

Ok well that part of it is fine. The problem is when someone visits the page it sends a blank form to the webmaster as if they already hit submit before they filled anything out. If anyone has a solution to this please post here. Here is my script:

PHP:
<form action="mail.php" method="get">
Your Name: <input type="text" name="name"><br>
E-mail: <input type="text" name = "email"><br><br>
Comments<br>
<textarea name="comments"></textarea><br><br>
<input type="submit" value="Submit">
</form>
<?
// PHP Script and form written by Alpha.
$name=$_GET['name'];
$email=$_GET['email'];
$comments=$_GET['comments'];
$to="[email protected]";
$message="$name just filled in your comments form. They said:n$commentsnnTheir e-mail address was: $email";
if(mail($to,"Comments From Your Site",$message,"From: $emailn")) {
echo "Thanks for your comments.";
} else {
echo "There was a problem sending the mail. Please check that you filled in the form correctly.";
}
mail($email,"Email Sent","Your email to the webmaster has been successfully sent. Thank you for the mail!","From: [email][email protected][/email]n");
?>



BTW: If that looks really silly or anything it's because I learned PHP today LOL. It's pretty simple though. Next I'm going to PHP with mySQL.
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
Do you have the form code and the php script code all in the same file or are they in two seperate files?

If they are in the same file try spliting them up into two files so that the php script is not executed every time the form page is load. I think thats what your problem is.

Over and out:)
 
0
•••
Your problem is in line 20. This line is triggered every time the file is called upon. So when it loads its triggered. This is when this blank file is sent, every time its loaded. Try adding an if statement similar to the one you already have. Make it check for a message, name, reply email address, etc.. etc.. and then within that if statement put the mail() command that you have from line 20. The if statement you already have may infact do aswell. This should fix the problem, let me know.
 
0
•••
Originally posted by web guru
Do you have the form code and the php script code all in the same file or are they in two seperate files?

If they are in the same file try spliting them up into two files so that the php script is not executed every time the form page is load. I think thats what your problem is.

Over and out:)

That fixed it thanks... Now the script is not even touched until the form is filled out. I am going to do what Devil said and make an IF statement so the person has to fill out the fields before they can press submit. Thank you both :)

OK one last question. I made an if statement that looks like this:

if ($name == "" || $email == "" || $comments == "") {
echo "Please fill in all fields!";


How do I get it so that if they don't fill the fields it doesn't send the email? Thanks.
 
0
•••
PHP:
if(($firstname == "")||($lastname == "")||($username == "")||($password == "")){


thats what im uisng
 
0
•••
PHP:
if (($name) &&  $email && $comments)) {
your mail code to send email
) 
else {
echo "Please fill in all the fields";
}
That's basically saying if each field has something in it then process the code else don't. Which is basically the same thing as if the fields equal nothing don't process the code else do, like you have it now.

You just need to rearrange your if else blocks to something like the above to prevent it from sending email when all the fields aren't filled in. Keep the mailto in an if statement and that should work. That should also solve your problem with having the code in the same file.

There's so many different ways you could go about it. Here's a couple more examples:
PHP:
if ($email == "") {
echo "your form code for the user here";
} 
else {
your mail code to send email here
}
That alone should work to prevent anything from happening unless the email address is filled in, although it doesn't check for the other fields, but you can easily add them in there.

Without using php to print out your form code:
PHP:
<?
if ($email == "") {
?>
// OUt of php
Your html for the form here.
// Back to php
<?} 
else {
your mail code to send email here
}
?>
 
0
•••
Ok thanks deadserious :) I'll try some of them and see which one works best then I'll edit the message and let you know. Thanks again!



OK, how's this look? I there's something I am not noticing apparently because it still emails me blank pages lol:

PHP:
<?
if ($email == "") {
?>
//Out of PHP
<form action="mail.php" method="get">
Your Name: <input type="text" name="name"><br>
E-mail: <input type="text" name = "email"><br><br>
Comments<br>
<textarea name="comments"></textarea><br><br>
<input type="submit" value="Submit">
</form>
// Back to PHP
<?} 
else {
$name=$_GET['name'];
$email=$_GET['email'];
$comments=$_GET['comments'];
$to="[email protected]";
$message="$name just filled in your comments form. They said:n$commentsnnTheir e-mail address was: $email";
mail($email,"Email Sent","Your email to the webmaster has been successfully sent. Thank you for the mail!","From: [email][email protected][/email]n");
}
?>
 
0
•••
Try it like this:
PHP:
<?
$email=$_GET['email'];
if ($email == "") {
?>
<form action="mail.php" method="get">
Your Name: <input type="text" name="name"><br>
E-mail: <input type="text" name = "email"><br><br>
Comments<br>
<textarea name="comments"></textarea><br><br>
<input type="submit" value="Submit">
</form>
<?} 
else {
mail($email,"Email Sent","Your email to the webmaster has been successfully sent. Thank you for the mail!","From: [email][email protected][/email]n");
}
?>

Also what are these lines for?

$name=$_GET['name'];
$comments=$_GET['comments'];
$to="[email protected]";
$message="$name just filled in your comments form. They said:n$commentsnnTheir e-mail address was: $email";

They don't appear to be getting used in the script anywhere.

EDIT: I see you were using them variables in the first little snippet of code you posted. :o:D
 
0
•••
Thanks everyone (especially deadserious) for your help. That was basically a little test script I was writing. Now that my PHP book came in I can learn how to make more sophisticated, interesting stuff. :) PHP is my favorite programming language now :D
 
0
•••
wow! i learnt somin new cheers dead
 
0
•••
Ok the PHP book says to write this HTML form.
<HTML>
<HEAD>
<TITLE>Project 4-1</TITLE>
</HEAD>
<BODY>
<FORM METHOD="POST" ACTION="p-4-1.php">
<H2>Contact List</H2>
<BR>Nickname:
<BR><INPUT TYPE="TEXT" NAME="Nickname">
<BR>
<BR>Full Name:
<BR><INPUT TYPE="TEXT" NAME="Fullname">
<BR>
<BR>Memo:
<BR><TEXTAREA NAME="Memo" ROWS="4" COLS="40" WRAP="PHYSICAL">
</TEXTAREA>
<BR>
<BR>
<INPUT TYPE="SUBMIT">
</FORM>
</BODY>
</HTML>

The PHP script that the book says to write is:
PHP:
<?php
  echo "<BR>Nickname=$Nickname";
  echo "<BR>Fullname=$Fullname";
  echo "<BR>Memo=$Memo";
?>

The Book says that will display whatever you enter into the form fields on your browser when you click Submit.

I found that to be incorrect so I changed it to:
PHP:
<?php
$Nickname=$_POST['Nickname'];
$Fullname=$_POST['Fullname'];
$Memo=$_POST['Memo'];
echo "<BR>Nickname=$Nickname";
echo "<BR>Fullname=$Fullname";
echo "<BR>Memo=$Memo";
?>

And it worked fine. I just wanted some clarification on if I am doing it right because I may have to rearrange this guy's code throughout the book if this continues :(
 
0
•••
The problem you are having is in PHP 4.2 and on, on installation, PHP by default sets register_globals to off. register_globals was the php.ini setting that allowed the above use of $variable. Until 4.2, register_globals was set to "on". However, in order to stem the flow of potentially insecure code, the PHP Group have turned register_globals off by default.

Where as with previous versions of PHP you could simply do,

PHP:

<?php
echo $variable;
?>

now you need to do,

PHP:

<?php
echo $_POST['variable'];
?>




Or, if the form was using the GET method, you would use the _GET array:

PHP:

<?php
echo $_GET['variable'];
?>



If for some reason you didn't know whether you were using a POST or GET method, you could simply use the _REQUEST array like so:

PHP:

<?php
echo $_REQUEST['variable'];
?>


So your doing it right and dont worry the way your doing it will work for all versions of php.


For more information, see this page in the PHP Online Manual.

http://www.php.net/manual/en/language.variables.predefined.php
 
0
•••
0
•••
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back