Connecting Tech Pros Worldwide Forums | Help | Site Map

Need help with $_POST

Newbie
 
Join Date: Nov 2006
Posts: 2
#1: Nov 30 '06
I've just started messing around with PHP and believe it or not its extremely frustrating haha. Anyway, I wanted to make a simple input form that would display the info after submitting it...

The script is a simple html script, which calls a php script passing
it the variables which it got by the forms:

**************HTML doc. named "insert.htm"************************

<html>
<head>
<title>HTML Form</title>
</head>
<body>
<form ACTION="script1.php" METHOD=POST>
First Name<input TYPE=TEXT NAME="FirstName" SIZE=20><br>
Last Name <input TYPE=TEXT NAME="LastName" SIZE=20><br>
E-mail Address <input TYPE=TEXT NAME="Email" SIZE=60><br>
Comments <textarea NAME="Comments" ROWS=5 COLS=40></textarea><br>
<input TYPE=SUBMIT NAME="SUBMIT" VALUE="Submit!">
</form>

</body>
</html>



***************************PHP script named "script1.php"**********************

<HTML>
<HEAD>
<TITLE>Form Results</title>
<BODY>

<?
$FirstName = $_POST["FirstName"];
$LastName = $_POST["LastName"];
$Email = $_POST["Email"];
$Comments = $_POST["Comments"];
?>


<?php
/* This page receives and handles the data generated by "insert.html".
*/
print "Your first name is $_POST["FirstName"].<BR>\n";
[color=blue]
print "Your last name is $_POST["LastName"].<BR>\n";
[color=blue]
print "Your E-mail is $_POST["Email"].<BR>\n";
[color=blue]
print "This is what you had to say:<BR>\n$_POST["Comments"]<BR>\n";
[color=blue]
?>



</BODY>
</HTML>




I keep getting this error :


Parse error: syntax error, unexpected '"', expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/grademyc/public_html/script1.php on line 17


with line 17 being
print "Your first name is $_POST["FirstName"].<BR>\n";
[color=blue]



i'm new to all this so i'm sure i've made mistakes somewhere...
please help...thanks!
bishwadeep's Avatar
Newbie
 
Join Date: Nov 2006
Location: Nepal
Posts: 12
#2: Nov 30 '06

re: Need help with $_POST


Try this code in your line 17
<br>I think it is because you did not concat:
print "Your first name is". $_POST["FirstName"]."<BR>\n";

If any other error occurred please write me
Newbie
 
Join Date: Nov 2006
Posts: 2
#3: Nov 30 '06

re: Need help with $_POST


sorry about that, I figured it out last night...REALLY LATE NIGHT!...unfortunately the script isn't putting anything into the MySql database though....it just keeps putting blank entries in the table...any thoughts?

my code to connect to the database is as follows:


****************************************PHP******* *************************************

<?
$DBhost = "localhost";
$DBuser = "grademyc_me";
$DBpass = "********";
$DBName = "grademyc_users";
$table = "details";
mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database");

@mysql_select_db("$DBName") or die("Unable to select database $DBName");

$sqlquery = "INSERT INTO $table VALUES('$Name')";

$results = mysql_query($sqlquery);

mysql_close();

?>


************************************************** ***********************************

every time i try to update the database through my form is just creates a blank entry which is really annoying...i can obviously update the database via phpMyadmin, but that doesn't help me.....Thanks!
Danny
Newbie
 
Join Date: Nov 2006
Posts: 17
#4: Nov 30 '06

re: Need help with $_POST


Quote:

Originally Posted by lovinlazio9

sorry about that, I figured it out last night...REALLY LATE NIGHT!...unfortunately the script isn't putting anything into the MySql database though....it just keeps putting blank entries in the table...any thoughts?

my code to connect to the database is as follows:


****************************************PHP******* *************************************

<?
$DBhost = "localhost";
$DBuser = "grademyc_me";
$DBpass = "********";
$DBName = "grademyc_users";
$table = "details";
mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database");

@mysql_select_db("$DBName") or die("Unable to select database $DBName");

$sqlquery = "INSERT INTO $table VALUES('$Name')";

$results = mysql_query($sqlquery);

mysql_close();

?>


************************************************** ***********************************

every time i try to update the database through my form is just creates a blank entry which is really annoying...i can obviously update the database via phpMyadmin, but that doesn't help me.....Thanks!
Danny

Hi ..
Try using the Insert statement with the table column name like ..

[PHP]$sqlquery = "INSERT INTO $table ('column_name_1','column_name_2', ..etc)VALUES('$value_1, $value_2', ..etc)";[/PHP]

Hope this will work ...good luck !
Newbie
 
Join Date: Nov 2006
Posts: 1
#5: Nov 30 '06

re: Need help with $_POST


Hello I think tha you'll better use an echo and to use simple cotes for your variable from POST :

Expand|Select|Wrap|Line Numbers
  1. print "Your first name is $_POST["FirstName"].<BR>\n";
  2. [color=blue]  
  3.  
becomes
Expand|Select|Wrap|Line Numbers
  1. echo "Your first name is $_POST['FirstName'].<BR>\n";
  2.  
or

Expand|Select|Wrap|Line Numbers
  1. echo 'Your first name is '.$_POST['FirstName'].'<BR>\n';
bye

Chesko
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#6: Nov 30 '06

re: Need help with $_POST


lovinlazio9: please read the Posting Guidelines at the top of this forum before you post anything else in this forum!

Especially the part about enclosing posted code within php, code or html tags!

Ronald :cool:
moishy's Avatar
Member
 
Join Date: Oct 2006
Posts: 104
#7: Dec 1 '06

re: Need help with $_POST


What was the point of doing this:

$FirstName = $_POST['FirstName'];

If you're anyways gonna call the $_POST

print $_POST['FirstName'];

And not

print $FirstName;
TheMadMidget's Avatar
Member
 
Join Date: Oct 2006
Posts: 98
#8: Dec 1 '06

re: Need help with $_POST


To continue on from moishy's thought, (onto my own thought)
Sometimes when I used $_POST['name'] and then again later, it would show up blank. I was like it was erasing it. This was not consistent though, which really threw me for a spin, but it has only happened to me personally with IE 7.newest out.
Newbie
 
Join Date: Dec 2006
Posts: 4
#9: Dec 2 '06

re: Need help with $_POST


I think I would agree about the variable use rather than using POST. I always save the POSTed data to a variable as I know this will stick and it is easier to type as well.

I also think the echo is better than the print - but I don't have a clue why - I think it is just a personal preference.

Steve
ronverdonk's Avatar
Moderator
 
Join Date: Jul 2006
Location: The Netherlands
Posts: 4,139
#10: Dec 2 '06

re: Need help with $_POST


Quote:

Originally Posted by moishy

What was the point of doing this:

$FirstName = $_POST['FirstName'];

If you're anyways gonna call the $_POST

print $_POST['FirstName'];

And not

print $FirstName;

I hope you don't really use the $_POST in that way, moishy! You must have an endless trust in your fellow human beings. Ever heard of cross-site scripting?
You should always sanitize your outside data, especially the $_GET and $_POST arrays. They are the main source for XSS attacks.

Ronald :cool:
Reply