[advanced search]
 

Go Back   NamePros.com > Discussion > Web Design & Development > Programming

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


Closed Thread
 
LinkBack Thread Tools
Old 07-18-2003, 01:17 AM   #1 (permalink)
NamePros Regular
 
Join Date: Jun 2003
Location: California
Posts: 249
401.00 NP$ (Donate)

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.

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";
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, 06:29 AM   #2 (permalink)
NamePros Member
 
Join Date: Jul 2003
Posts: 120
185.00 NP$ (Donate)

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, 09:03 AM   #3 (permalink)
NamePros Regular
 
Join Date: Sep 2002
Location: Canada
Posts: 498
834.00 NP$ (Donate)

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, 11:23 AM   #4 (permalink)
NamePros Regular
 
Join Date: Jun 2003
Location: California
Posts: 249
401.00 NP$ (Donate)

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

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, 01:39 PM   #5 (permalink)
Senior Member
 
Join Date: May 2003
Posts: 2,211
6,170.25 NP$ (Donate)

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 == "")){

thats what im uisng
adam_uk is offline  
Old 07-18-2003, 02:13 PM   #6 (permalink)
Senior Member
 
Join Date: Aug 2002
Posts: 1,300
2.85 NP$ (Donate)

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.

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.
// Back to php
<?}
else {
your mail code to send email here
}
?>
deadserious is offline  
Old 07-18-2003, 02:46 PM   #7 (permalink)
NamePros Regular
 
Join Date: Jun 2003
Location: California
Posts: 249
401.00 NP$ (Donate)

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:
<?
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, 09:19 PM   #8 (permalink)
Senior Member
 
Join Date: Aug 2002
Posts: 1,300
2.85 NP$ (Donate)

deadserious has a spectacular aura aboutdeadserious has a spectacular aura about


Try it like this:
PHP Code:
<?
$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, 02:29 AM   #9 (permalink)
NamePros Regular
 
Join Date: Jun 2003
Location: California
Posts: 249
401.00 NP$ (Donate)

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, 06:22 AM   #10 (permalink)
Senior Member
 
Join Date: May 2003
Posts: 2,211
6,170.25 NP$ (Donate)

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, 03:36 PM   #11 (permalink)
NamePros Regular
 
Join Date: Jun 2003
Location: California
Posts: 249
401.00 NP$ (Donate)

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";
  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 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, 03:48 PM   #12 (permalink)
NamePros Member
 
Join Date: Jul 2003
Posts: 120
185.00 NP$ (Donate)

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:

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, 03:54 PM   #13 (permalink)
NamePros Regular
 
Join Date: Jun 2003
Location: California
Posts: 249
401.00 NP$ (Donate)

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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Site Sponsors
Advertise your business at NamePros

All times are GMT -7. The time now is 11:45 PM.


Powered by: vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0
Template-Modifications by TMS
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85