Total Newbie needs help: PHP Does not echo form variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Raven_Words
Forum Newbie
Posts: 4
Joined: Sat Sep 19, 2009 7:15 am

Total Newbie needs help: PHP Does not echo form variables

Post by Raven_Words »

I just started PhP and am using "Head First: PHP & MySQL" as my guide.

So: I decided to make a simple form which will echo back name, phone, email.

This is the HTML code for the form called Index.html:

Code: Select all

 <html><head>   <title>TEST DATABASE</title>  </head>  <body><h1> TEST DATABASE </h1> <form method="post" action="../php/sql_insert.php" > <label for = "name"> Your Name </label><input type="text" name="name">  <br /><label for = "email"> Your E-mail  : </label><input type="text" name="email"><br /><label for = "phone"> Phone number : </label><input type="text" name="phone"><br /> <br /><input type="submit" value="Send"><input type="reset" value="Clear"> </form>      </body></html> 
This is the php code on another file called sql_insert.php:

Code: Select all

 <html><head><title> Output </title></head><body><h1> Thank You </h1> 

Code: Select all

 
<?php
 
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
echo 'Thanks for submitting your feedback';
echo 'Your name is'. $name;
echo 'Your email address is'. $email;
echo 'Your Phone Number is'. $phone;
?>
 

Code: Select all

 </body></html> 

The result is all I get after I input data on the form is the heading "Thank You" and that's it.

What am I doing wrong?

About to start opening my veins here.
User avatar
edawson003
Forum Contributor
Posts: 133
Joined: Thu Aug 20, 2009 6:34 am
Location: Los Angeles, CA - USA

Re: Total Newbie needs help: PHP Does not echo form variables

Post by edawson003 »

Well, based on the code you provided, it doesn't seem like your posting the data to a mysql table nor are you establishing session variables that can be carried/retained over into another page. Those variables, $name, $email and $phone that are supposed to established through your form via the form fields are not being defined.

so, if you are not setting session variables or actually posting to sql table, you can at least just test your form this way. At this point you would need only one file as you do not have a mechanism for carrying over variables set on page to another or querying posted datapoints.

Code: Select all

 
<html>
<head>
   <title>TEST FORM</title>
  </head>
 
 
<body>
<h1> TEST FORM </h1>
 [color=#0000FF]<?php 
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = "\t<p><b>Thanks for submitting your feedback</b><br>Your name is <u>$name</u><br>Your email address is <u>$email</u><br>Your Phone Number is <u>$phone</u></p>\n";
?>
 <?
if(isset($_POST['submit']) && $_POST['name'] && $_POST['email'] && $_POST['phone']){
echo $message;
}
else {
 ?>
 [/color]
<form method="post" action="[color=#0000FF]<? echo $HTTP_SERVER_VARS['PHP_SELF']; ?>[/color]" >
 
<label for = "name"> Your Name </label>
<input type="text" name="name">  <br />
<label for = "email"> Your E-mail  : </label>
<input type="text" name="email"><br />
<label for = "phone"> Phone number : </label>
<input type="text" name="phone"><br />
 
<br />
<input type="submit" name="submit" value="Send"><input type="reset" value="Clear">
 
</form>  
[color=#0000FF]<?
}
?>  [/color]
 
</body>
</html>
 
To really get started, you will need to:
1) set up a mysql table that your form data points will be posted to
2) create a connection to the table
3) set session variable(s) and/or display content in your mysql table based on your session
Raven_Words
Forum Newbie
Posts: 4
Joined: Sat Sep 19, 2009 7:15 am

Re: Total Newbie needs help: PHP Does not echo form variables

Post by Raven_Words »

Thanks for the response, will try this and see what happens. :D
merylvingien
Forum Newbie
Posts: 12
Joined: Mon Sep 07, 2009 9:05 am

Re: Total Newbie needs help: PHP Does not echo form variables

Post by merylvingien »

I know i am a newbie, but wouldnt it be:

Code: Select all

 
<?php
 
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
 
?>
 
<html>
<head>
<title> Output </title>
</head>
<body>
<h1> Thank You </h1>
<?php
echo "Thanks for submitting your feedback";
echo "Your name is. $name";
echo "Your email address is. $email";
echo "Your Phone Number is. $phone";
?>
 
</body>
</html>
 
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Total Newbie needs help: PHP Does not echo form variables

Post by califdon »

Although there are several problems with your scripts, that should work, assuming that all the lines in the 3 segments you displayed are actually continuous in one file, sql_insert.php, so since it doesn't, it appears that the php parser in your web server isn't parsing your php. There are 2 reasons that could cause that. If your web server isn't configured properly, or if you have a syntax error in your php that causes the parser to give up trying to interpret your code. To test whether it's the first issue, temporarily remove (or comment out) ALL the lines between <?php and ?>, then insert one line:

Code: Select all

echo "This is being sent by PHP.";
Or you could enter this line:

Code: Select all

echo phpinfo();
which will print out a whole lot of system information for you. If that doesn't show up in your page, your web server is not configured to interpret php, and you need to modify your configuration file. If that shows up, you must have a fatal syntax error, so you must carefully review your php code to find the error. Common errors include a missing semicolon at the end of a php statement, mismatched parentheses or curly brackets, missing $ at the beginning of a variable name. I don't see any such errors in your code, so I suspect the problem may be of the first kind.
Raven_Words
Forum Newbie
Posts: 4
Joined: Sat Sep 19, 2009 7:15 am

Re: Total Newbie needs help: PHP Does not echo form variables

Post by Raven_Words »

califdon wrote:Although there are several problems with your scripts, that should work, assuming that all the lines in the 3 segments you displayed are actually continuous in one file, sql_insert.php, so since it doesn't, it appears that the php parser in your web server isn't parsing your php. There are 2 reasons that could cause that. If your web server isn't configured properly, or if you have a syntax error in your php that causes the parser to give up trying to interpret your code. To test whether it's the first issue, temporarily remove (or comment out) ALL the lines between <?php and ?>, then insert one line:

Code: Select all

echo "This is being sent by PHP.";
Or you could enter this line:

Code: Select all

echo phpinfo();
which will print out a whole lot of system information for you. If that doesn't show up in your page, your web server is not configured to interpret php, and you need to modify your configuration file. If that shows up, you must have a fatal syntax error, so you must carefully review your php code to find the error. Common errors include a missing semicolon at the end of a php statement, mismatched parentheses or curly brackets, missing $ at the beginning of a variable name. I don't see any such errors in your code, so I suspect the problem may be of the first kind.

I think this is the problem. For some reason PHP is not being parsed by either firefox or I.E. I am working off a XAMP install and the standard PhPInfo link on the XAMP frontend is working fine. I did modify the file in order for it to work with Mercury mail off the local host, so I am not sure what the problem is.

Maybe I should reinstall the whole thing and try again?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Total Newbie needs help: PHP Does not echo form variables

Post by califdon »

Raven_Words wrote:
califdon wrote:I think this is the problem. For some reason PHP is not being parsed by either firefox or I.E. I am working off a XAMP install and the standard PhPInfo link on the XAMP frontend is working fine. I did modify the file in order for it to work with Mercury mail off the local host, so I am not sure what the problem is.

Maybe I should reinstall the whole thing and try again?
No, php is never parsed (or even seen) by any browser, because php is server-side code, parsed by the web server (Apache) before any of the html it generates is sent to the browser! I'm not sure I know what you mean by "the standard PhPInfo link on the XAMP frontend is working fine"; phpinfo() is a built-in php function. I gave you the code wrong in my previous post, though, it doesn't need the echo:

Code: Select all

<?php
  phpinfo();
?>
If Apache is showing other pages, I don't see any reason to reinstall it. The configuration file is named httpd.conf and that's the only place you would need to change or add anything to make it parse php. I would advise you to do the first thing I suggested earlier. You need to find out why your script isn't being parsed by Apache. It now sounds more like it's a php syntax error, although not in the part that you posted. Is there ANY php in another part of that script?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Total Newbie needs help: PHP Does not echo form variables

Post by califdon »

Another thing you can do to pinpoint what is happening is to install a plug-in for your FIrefox browser, called TamperData. You can download it from https://addons.mozilla.org/en-US/firefox/addon/966. With it installed, you can open your form page, fill in the form fields, then before you click the Submit button, go to Tools on the Firefox menu and click on TamperData, turn it on, then click your Submit button, and it will show you what your form page is sending to the server. That can immediately clarify whether your POST data is being sent correctly.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Total Newbie needs help: PHP Does not echo form variables

Post by Jonah Bron »

Also, remember that you must access the file through http://localhost/, not the path of the file on your drive (assuming you are using a localhost).
Raven_Words
Forum Newbie
Posts: 4
Joined: Sat Sep 19, 2009 7:15 am

Re: Total Newbie needs help: PHP Does not echo form variables

Post by Raven_Words »

Thanks for the help all, will try it out and see what happens, setting up a test SQL database and going to try to add a few more functions.

Will post here how it works out.
Post Reply