Jump to content

[SOLVED] Newbie to PHP basic question


bettydailey

Recommended Posts

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.

Link to comment
Share on other sites

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'];
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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,

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.