value = " . $var." method allocating variable is not working for me

Hi all

I just started my adventure with php and I’m using Jeremy Allen’s book "Mastering PHP 4.1.

On that book he use method to allocating variables from form, which is not working for me.

for example
[php]<?php print(’<input type=“text” name=“person” value="’ . $person .
‘"size=“15” />’);
[/php]

doesn’t return variable $person in php file directed in

<form action="you_are.php">

I know i can use method

[php]$person = $_GET[‘person’][/php]

which is working perfect for me, but I just wonder why above method is not working for me?

Is that because I’m using php version 5.3.6?
or I have to configure something in server settings?

sorry for next post bellow my first, but I found another clue

I have got 2 files book.php and end.php.

It looks like any variables created in file book.php aren’t available in end.php. I mean even if I create in book.php variable $a when I try to print it in file end.php variable $a is empty (doesnt exist?)

It should work as is. Show more of your code. Did you set the person variable somewhere?
If it is not set, you would be setting the value of the text field to nothing…

Not sure without seeing more of your code.

Normally, you have two files. The first is a HTML FORM that allows users to input values into fields.
This FORM is then “posted” to your PHP code, which in turn, displays results from the computations
using various fields read from the form using the $_POST[‘variablenames’] data from the FORM fields.
You can display data in text fields using variables as long as they are created somewhere. (Either from
posted variables or created in your PHP code.)
Hope all this helps explain it for you… Good luck…

Welcome to wonderful world of troubleshooting PHP :slight_smile: I think you skipped ahead of the code in the book. For that to work, $person has to be already set, either from hardcoding it (ie, $person = 'Billy Bob Joe"; or from user input via a form (ie, $person = $_POST[‘person’];), basically what Ernie said. The name from inbetween the [] comes from the name (name = “”) attribute in the text field (or whatever input it happens to be).

Thx guys for your help

Maybe I shall start from copy/paste code from book.

This is file in which I set few variables (for examle $favorite_language)

[code]

Who Are You Advanced! Please fill in the following fields:
<?php // Assign a value to favorite_language variable $favorite_language = "PHP"; ?> First Name:
Last Name:
Favorite Programming Language:
[/code]

and this is file when all datas from forms are send as variables

[code]

You Are Advanced! ... Hello <?php print($first . " " . $last); ?> I am glad to know your favorite programming language is <?php print($favorite_language); ?>. [/code]

the problem is when I execute both programs in web browser it looks like all variables created in 1st file ($favorite_language, $first, $last) are not available in 2nd file.

In this book author use this method of allocating(?) variables from .

If I use bellow method

<input type="text" name="first" size="15"><br />

and then

[php]$first= $_GET[‘first’][/php]

all is ok.

Of course I can use 2nd method, I just wonder why 1st is not working?

Because the php part is on a seperate page. Once you hit submit, its opening up you_are_advanced.php to process the information from the form. If it was on the same page as the form, it would work.

The second page should be full of $_GET’s, 1 for each form field.

Sponsor our Newsletter | Privacy Policy | Terms of Service