[advanced search]
Results from the most recent live auction are here.
23 members in the live chat room. Join Chat!
Register Rules & FAQ NP$ Store Active Threads Mark Forums Read
Go Back   NamePros.Com > Design and Development > Programming
User Name
Password

Old 07-18-2003, 02:17 AM   · #1
Alpha
NamePros Regular
 
Name: Todd
Location: California
Trader Rating: (0)
Join Date: Jun 2003
Posts: 249
NP$: 401.00 (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.


Please register or log-in into NamePros to hide ads
__________________
--Alpha
Alpha is offline   Reply With Quote
Old 07-18-2003, 07:29 AM   · #2
web guru
NamePros Member
 
Trader Rating: (0)
Join Date: Jul 2003
Posts: 120
NP$: 185.00 (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   Reply With Quote
Old 07-18-2003, 10:03 AM   · #3
DarkDevil
NamePros Regular
 
Name: Nick
Location: Canada
Trader Rating: (0)
Join Date: Sep 2002
Posts: 498
NP$: 834.00 (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   Reply With Quote
Old 07-18-2003, 12:23 PM   · #4
Alpha
NamePros Regular
 
Name: Todd
Location: California
Trader Rating: (0)
Join Date: Jun 2003
Posts: 249
NP$: 401.00 (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   Reply With Quote
Old 07-18-2003, 02:39 PM   · #5
adam_uk
Senior Member
 
Trader Rating: (17)
Join Date: May 2003
Posts: 2,211
NP$: 6170.25 (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   Reply With Quote
Old 07-18-2003, 03:13 PM   · #6
deadserious
Senior Member
 
Trader Rating: (13)
Join Date: Aug 2002
Posts: 1,300
NP$: 2.85 (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   Reply With Quote
Old 07-18-2003, 03:46 PM   · #7
Alpha
NamePros Regular
 
Name: Todd
Location: California
Trader Rating: (0)
Join Date: Jun 2003
Posts: 249
NP$: 401.00 (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   Reply With Quote
Old 07-18-2003, 10:19 PM   · #8
deadserious
Senior Member
 
Trader Rating: (13)
Join Date: Aug 2002
Posts: 1,300
NP$: 2.85 (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   Reply With Quote
Old 07-20-2003, 03:29 AM   · #9
Alpha
NamePros Regular
 
Name: Todd
Location: California
Trader Rating: (0)
Join Date: Jun 2003
Posts: 249
NP$: 401.00 (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   Reply With Quote
Old 07-20-2003, 07:22 AM   · #10
adam_uk
Senior Member
 
Trader Rating: (17)
Join Date: May 2003
Posts: 2,211
NP$: 6170.25 (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   Reply With Quote
Old 07-20-2003, 04:36 PM   · #11
Alpha
NamePros Regular
 
Name: Todd
Location: California
Trader Rating: (0)
Join Date: Jun 2003
Posts: 249
NP$: 401.00 (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   Reply With Quote
Old 07-20-2003, 04:48 PM   · #12
web guru
NamePros Member
 
Trader Rating: (0)
Join Date: Jul 2003
Posts: 120
NP$: 185.00 (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/langua....predefined.php
web guru is offline   Reply With Quote
Old 07-20-2003, 04:54 PM   · #13
Alpha
NamePros Regular
 
Name: Todd
Location: California
Trader Rating: (0)
Join Date: Jun 2003
Posts: 249
NP$: 401.00 (Donate)
Alpha is an unknown quantity at this point
Ok thanks a bunch
__________________
--Alpha
Alpha is offline   Reply With Quote
Closed Thread

NamePros is a revenue sharing forum.

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


Site Sponsors
EscrowDNS Hunting Moon RealTechNetwork
Advertise your business at NamePros
All times are GMT -7. The time now is 05:10 PM.


Powered by: vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 2.4.0