bettydailey Posted December 20, 2007 Share Posted December 20, 2007 I am having trouble with just the basics of PHP. I want to assign a value from my form to a variable name to be used later in my code. Below is my php code <?php echo "testing placing data into a variable"; $field1-name=$_GET['Value1']; ?> Below is the HTML of the form. Note the method is "GET". However, I receive the same error message when I change the method to "POST" as well as changing my PHP code to $_POST. <html> <body> <form action="formGet.php" method="get"> Value1: <input type="text" name="field1-name"><br> <input type="Submit"> </form> </body> </html> I receive the following error on Parse error: parse error in /usr/local/www/vhosts/microedgeinc.net/htdocs/javascript/AJAX/formGet.php on line 4 I know that a parse error generally deals with "{" and ";" but there is only two lines of code in my PHP. Can someone please help? Thank you. Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted December 20, 2007 Share Posted December 20, 2007 you can't use - in var names the must be alphanumeric or underscore and must not start with a number... if this is your html... <html> <body> <form action="formGet.php" method="get"> Value1: <input type="text" name="field1_name"> <input type="Submit"> </form> </body> </html> then your code needs no be... <?php echo "testing placing data into a variable"; $field1_name=$_GET['field1_name']; ?> Quote Link to comment Share on other sites More sharing options...
revraz Posted December 20, 2007 Share Posted December 20, 2007 You are getting the wrong variable. See if this helps http://www.w3schools.com/php/php_forms.asp Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted December 20, 2007 Share Posted December 20, 2007 Variable names cannot contain a dash, "$field-name" is invalid. You could use "$field_name". That still would work with your test code, since the name of the field is "field-name". To retrieve that you need to do: <?php $field_value = $_GET['field-name1']; ?> Ken Quote Link to comment Share on other sites More sharing options...
bettydailey Posted December 21, 2007 Author Share Posted December 21, 2007 Thank you for the link to W3C. I copied the code from the site with two modifications name="yourName" and method="get" so I can see the URL going across the wire. <html> <body><form action="welcome.php" method="get"> Name: <input type="text" name="yourName" /> Age: <input type="text" name="age" /> <input type="submit" /> </form></body> </html> I copied the PHP code changing the field name and POST to GET. <html> <body>Welcome <?php echo $_GET["yourName"]; ?>.<br /> You are <?php echo $_GET["age"]; ?> years old.</body> </html> When I submit the form the returning URL looks like http://www.microedgeinc.net/javascript/AJAX/welcome.php?yourName=dad&age=43 The results from welcome.php. Welcome . You are years old. The values do not display. Please try it yourself. http://www.microedgeinc.net/javascript/AJAX/welcome.html I just do not know what is wrong. Please help. Quote Link to comment Share on other sites More sharing options...
ChompGator Posted December 21, 2007 Share Posted December 21, 2007 Another tip, I wouldn't add an field-name with the dash like that, while it make work, do what these other very intelligent programmers above me have posted, use an underscore or something - databases, and coding does not tend to like dashes too much. Thats my 2 cents - Regards, Quote Link to comment Share on other sites More sharing options...
revraz Posted December 21, 2007 Share Posted December 21, 2007 Make a new file called phpinfo.php. Put these three lines in it and run it. <?php phpinfo(); ?> Quote Link to comment Share on other sites More sharing options...
bettydailey Posted December 21, 2007 Author Share Posted December 21, 2007 Created the php code and executed on my server http://www.microedgeinc.net/javascript/AJAX/phpinfo.php Do not know how to read the data. Can you please look at the results? Thank you for your help. Quote Link to comment Share on other sites More sharing options...
revraz Posted December 21, 2007 Share Posted December 21, 2007 It's an old version of PHP, but I would think it would work. I just copied and pasted your code exactly and it works for me. On your .html form, try changing method="get" to method="GET" Dont think it will matter but worth a try. Quote Link to comment Share on other sites More sharing options...
revraz Posted December 21, 2007 Share Posted December 21, 2007 Something else is odd with your result. It shows it on two lines yet you never send a new line command. What did you use to make the .html and .php file? Quote Link to comment Share on other sites More sharing options...
bettydailey Posted December 21, 2007 Author Share Posted December 21, 2007 1) I am using the textpad editor to create my html code as well as the php code. It is fancy version of notepad. 2) The break statement makes it go to a new line. 3) I did change lowercase get to uppercase GET on the html page. 4) Taking your suggestion of phpinfo I created a new program welcome2.html. It invokes welcome2.php. The php code is below. <html> <body> Welcome <?php echo $_GET["yourName"]; ?>. <br /> <!-- <br /> makes go to new line --> You are <?php echo $_GET["age"]; ?> years old. <p> phpInfo <?php phpinfo(); ?> </p> </body> </html> 5. When I execute the URL looks like http://www.microedgeinc.net/javascript/AJAX/welcome2.php?yourName=dad&age=43 I review the phpinfo and I definitely see the property value pairs. Is there a switch or a flag that needs to be set somewhere to read data from POST or GET? Any ideas? Quote Link to comment Share on other sites More sharing options...
revraz Posted December 21, 2007 Share Posted December 21, 2007 2. You don't have a break statement in your code. Just because you put it on a new line in the code doesn't mean it should display on a new line. Try using Notepad, sounds like you are getting Rich Text in with your code. Quote Link to comment Share on other sites More sharing options...
revraz Posted December 21, 2007 Share Posted December 21, 2007 http://bdcpaging.com/test/test.html Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted December 21, 2007 Share Posted December 21, 2007 Your host is using a VERY old and insecure version of PHP. This version did not have the $_GET and $_POST superglobal arrays. You need to use the array HTTP_GET_VARS instead of $_GET. Ask your host to upgrade to the latest version of PHP, which is PHP v5.2.5 or find an new host. Ken Quote Link to comment Share on other sites More sharing options...
bettydailey Posted December 21, 2007 Author Share Posted December 21, 2007 Problem solved. Thank you have a safe and wonderful holiday season. The welcome2.php code is below <html> <body> Created code in Notepad <br /> Welcome <?php echo $HTTP_GET_VARS["yourName"]; ?>. <br /> You are <?php echo $HTTP_GET_VARS["age"]; ?> years old. <p> phpInfo <?php phpinfo(); ?> </p> </body> </html> Quote Link to comment Share on other sites More sharing options...
revraz Posted December 21, 2007 Share Posted December 21, 2007 Not really solved, as stated, it's not very secure and is old outdated procedures. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.