NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page PHP Form Script

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 07-18-2003, 02:17 AM THREAD STARTER               #1 (permalink)
NamePros Regular
Join Date: Jun 2003
Location: California
Posts: 245
Alpha is an unknown quantity at this point
 



PHP Form Script


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.
????: NamePros.com http://www.namepros.com/programming/15010-php-form-script.html

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 Code:
<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="webmaster@whatever.com";
$message="$name just filled in your comments form. They said:n$commentsnnTheir e-mail address was: $email";
????: NamePros.com http://www.namepros.com/showthread.php?t=15010
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]noreply@whatever.com[/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.
__________________
--Alpha
Alpha is offline  
Old 07-18-2003, 07:29 AM   #2 (permalink)
NamePros Member
Join Date: Jul 2003
Posts: 118
web guru is an unknown quantity at this point
 



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:-)
web guru is offline  
Old 07-18-2003, 10:03 AM   #3 (permalink)
NamePros Regular
Join Date: Sep 2002
Location: Canada
Posts: 481
DarkDevil is an unknown quantity at this point
 



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.
__________________
Sometimes I lay awake at night and I ask "Where have I gone wrong?" Then a little voice says "This is going to take more than one night"
DarkDevil is offline  
Old 07-18-2003, 12:23 PM THREAD STARTER               #4 (permalink)
NamePros Regular
Join Date: Jun 2003
Location: California
Posts: 245
Alpha is an unknown quantity at this point
 



Quote:
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
????: NamePros.com http://www.namepros.com/showthread.php?t=15010

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.
__________________
--Alpha
Alpha is offline  
Old 07-18-2003, 02:39 PM   #5 (permalink)
Senior Member
Join Date: May 2003
Posts: 2,187
adam_uk is a jewel in the roughadam_uk is a jewel in the roughadam_uk is a jewel in the rough
 


Breast Cancer
PHP Code:
if(($firstname == "")||($lastname == "")||($username == "")||($password == "")){ 
????: NamePros.com http://www.namepros.com/showthread.php?t=15010

thats what im uisng
adam_uk is offline  
Old 07-18-2003, 03:13 PM   #6 (permalink)
Senior Member
Join Date: Aug 2002
Posts: 1,255
deadserious has a spectacular aura aboutdeadserious has a spectacular aura about
 



PHP Code:
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.
????: NamePros.com http://www.namepros.com/showthread.php?t=15010

There's so many different ways you could go about it. Here's a couple more examples:
PHP Code:
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 Code:
<?
if ($email == "") {
?>
// OUt of php
Your html for the form here.
????: NamePros.com http://www.namepros.com/showthread.php?t=15010
// Back to php
<?
else {
your mail code to send email here
}
?>
deadserious is offline  
Old 07-18-2003, 03:46 PM THREAD STARTER               #7 (permalink)
NamePros Regular
Join Date: Jun 2003
Location: California
Posts: 245
Alpha is an unknown quantity at this point
 



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 Code:
<?
????: NamePros.com http://www.namepros.com/showthread.php?t=15010
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="webmaster@whatever.com";
$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]noreply@whatever.com[/email]n");
}
?>
__________________
--Alpha
Alpha is offline  
Old 07-18-2003, 10:19 PM   #8 (permalink)
Senior Member
Join Date: Aug 2002
Posts: 1,255
deadserious has a spectacular aura aboutdeadserious has a spectacular aura about
 



Try it like this:
PHP Code:
<?
????: NamePros.com http://www.namepros.com/showthread.php?t=15010
$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]noreply@whatever.com[/email]n");
}
?>
Also what are these lines for?

$name=$_GET['name'];
$comments=$_GET['comments'];
$to="webmaster@whatever.com";
$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.
deadserious is offline  
Old 07-20-2003, 03:29 AM THREAD STARTER               #9 (permalink)
NamePros Regular
Join Date: Jun 2003
Location: California
Posts: 245
Alpha is an unknown quantity at this point
 



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
__________________
--Alpha
Alpha is offline  
Old 07-20-2003, 07:22 AM   #10 (permalink)
Senior Member
Join Date: May 2003
Posts: 2,187
adam_uk is a jewel in the roughadam_uk is a jewel in the roughadam_uk is a jewel in the rough
 


Breast Cancer
wow! i learnt somin new cheers dead
adam_uk is offline  
Old 07-20-2003, 04:36 PM THREAD STARTER               #11 (permalink)
NamePros Regular
Join Date: Jun 2003
Location: California
Posts: 245
Alpha is an unknown quantity at this point
 



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 Code:
<?php
  
echo "<BR>Nickname=$Nickname";
  echo 
"<BR>Fullname=$Fullname";
????: NamePros.com http://www.namepros.com/showthread.php?t=15010
  echo 
"<BR>Memo=$Memo";
????: NamePros.com http://www.namepros.com/showthread.php?t=15010
?>
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 Code:
<?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
__________________
--Alpha
Alpha is offline  
Old 07-20-2003, 04:48 PM   #12 (permalink)
NamePros Member
Join Date: Jul 2003
Posts: 118
web guru is an unknown quantity at this point
 



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:

????: NamePros.com http://www.namepros.com/showthread.php?t=15010
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/languag...predefined.php
web guru is offline  
Old 07-20-2003, 04:54 PM THREAD STARTER               #13 (permalink)
NamePros Regular
Join Date: Jun 2003
Location: California
Posts: 245
Alpha is an unknown quantity at this point
 



Ok thanks a bunch
__________________
--Alpha
Alpha is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Liquid Web Smart Servers  
All times are GMT -7. The time now is 11:33 AM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger