p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: difficulty in passing a variable to php from textbox


Message #1 by cyberjoe@t... on Sat, 11 May 2002 20:48:54
Hi everyone,

I need help with that code snippet from Chapter 3 in which the value 
of "Author" is passed from a textbox in "text.html" to "text.php".All I 
get is "Your favourite author is:------------------".The box says one of 
the possible problems may be an incorrect spelling of the variable, for 
instance, typing "author" rather than "Author", but Ive checked that and 
its correct.Does anyone have a clue what might be going on?

Thanks a lot,

Joe

Message #2 by Peter Simard <pasimard@v...> on Sat, 11 May 2002 16:58:21 -0400
Please post your code, both the .html ( sending form ) and .php
receiving code.


Message #3 by cyberjoe@t... on Sat, 11 May 2002 22:18:51
> Please post your code, both the .html ( sending form ) and .php
receiving code.


Hi and thanks for replying,

The HTML code is:

<HTML>
<HEAD></HEAD>
<BODY>
<FORM METHOD=GET ACTION="text.php">
Who is your favourite author?
<INPUT NAME="Author" TYPE="TEXT">
<BR>
<BR>
<INPUT TYPE=SUBMIT>
</FORM>
</BODY>
</HTML>


and the php cope is:

<HTML>
<HEAD></HEAD>
<BODY>
Your favorite author is:
<?php 
echo $Author;
?>
</BODY>
</HTML>

I do not see any discrepancies in either code. Could you please help me?

Thanks a lot,

Joe
Message #4 by "Jonothon Ortiz \(Xnext, Inc\)" <jon@X...> on Sat, 11 May 2002 19:03:03 -0400
Did you check to see if your server allows the

<?php
//code
?>

bracket set?

Some servers only permit

<?
//code
?>


-----Original Message-----
From: cyberjoe@t... [mailto:cyberjoe@t...]
Sent: Saturday, May 11, 2002 22:19
To: beginning php
Subject: [beginning_php] Re: difficulty in passing a variable to php
from textbox


> Please post your code, both the .html ( sending form ) and .php
receiving code.


Hi and thanks for replying,

The HTML code is:

<HTML>
<HEAD></HEAD>
<BODY>
<FORM METHOD=GET ACTION="text.php">
Who is your favourite author?
<INPUT NAME="Author" TYPE="TEXT">
<BR>
<BR>
<INPUT TYPE=SUBMIT>
</FORM>
</BODY>
</HTML>


and the php cope is:

<HTML>
<HEAD></HEAD>
<BODY>
Your favorite author is:
<?php
echo $Author;
?>
</BODY>
</HTML>

I do not see any discrepancies in either code. Could you please help me?

Thanks a lot,

Joe

Message #5 by Peter Simard <pasimard@v...> on Sat, 11 May 2002 19:51:01 -0400
OK.

Your code worked for me no problem.  For your case change your code to
look like this:

<?php
echo $_GET['Author']; //I'll explain next!!
?>

Some changes in php have been instituted since that book was written,
one of them being that register_globals is automatically set as off
when php is configured.


POST and GET variables are no longer available globally by default
because it's a security issue, so they established some new global
variables, 2 of them are $_POST[] and $_GET[], (which are actually
arrays ).

Whenever you submit a form, depending on the METHOD you define in the
form, either: METHOD="POST" or  METHOD="GET", the variables generated
by the action of sending the form are put into the global array which
corresponds to the METHOD="___" of the sending form: $_GET[] or  $_POST[].

So in this case, we are using GET as the method, thus the variable
$Author is now accessible using this syntax:

$_GET['Author'];


For your purposes as you go through the book, use this syntax to
access your variables and you should be fine.

It may seem confusing, but after a few attempts you'll just do it
naturally.

Also, open a new .php file and type in
<?php
phpinfo();
?>

and save the file as phpinfo.php.

Next, run that script and you'll see a all the data pertaining to your
PHP installation .

As ever,

Pete


Message #6 by cyberjoe@t... on Sun, 12 May 2002 05:05:20
> OK.

Your code worked for me no problem.  For your case change your code to
look like this:

<?php
echo $_GET['Author']; //I'll explain next!!
?>

Some changes in php have been instituted since that book was written,
one of them being that register_globals is automatically set as off
when php is configured.


POST and GET variables are no longer available globally by default
because it's a security issue, so they established some new global
variables, 2 of them are $_POST[] and $_GET[], (which are actually
arrays ).

Whenever you submit a form, depending on the METHOD you define in the
form, either: METHOD="POST" or  METHOD="GET", the variables generated
by the action of sending the form are put into the global array which
corresponds to the METHOD="___" of the sending form: $_GET[] or  $_POST[].

So in this case, we are using GET as the method, thus the variable
$Author is now accessible using this syntax:

$_GET['Author'];


For your purposes as you go through the book, use this syntax to
access your variables and you should be fine.

It may seem confusing, but after a few attempts you'll just do it
naturally.

Also, open a new .php file and type in
<?php
phpinfo();
?>

and save the file as phpinfo.php.

Next, run that script and you'll see a all the data pertaining to your
PHP installation .

As ever,

Pete

Dear Peter,

Thanks for being so thoughtful again. Youve been of great help.Ive had a 
look at php.ini and youre absolutely right. In the new versions of php4, 
as of php4.1 plus register_globals is automatically switched off and, they 
say, will soon be deprecated due to security issues, which I do not really 
grasp to be honest, and they recommend that $_REQUEST["filename"] or the 
specific ones, namely $_GET["filename"], or $_POST["filename"], be used 
instead. Thanks again and I hope we can keep in touch. I live in So 
Paulo, Brazil.Im an English teacher and Web Designer/Master as well. Im 
looking forward to hearing from you again.

Best regards,

Joe

Message #7 by Sdrstrm@c... on Sun, 12 May 2002 08:40:38 EDT
--part1_12c.114c123b.2a0fbcc6_boundary
Content-Type: text/plain; charset="US-ASCII"
Content-Transfer-Encoding: 7bit

So is it correct to say that everytime you want to do something with a 
variable that has come from a form (html) then you must script 
$_GET['author'] on the page you want to use it on. Lets say I have a 
registration form on a web page that i want the variables to be put into a 
table in MySQL.  Would I have to declare $_POST[variable here] for every 
piece of infomation the user put in the form?  LIke so;
$_POST['firstname'];
$_POST['lastname'];
$_POST['address'];
and so on before I do the SQL insert statement?

Message #8 by Peter Simard <pasimard@v...> on Sun, 12 May 2002 12:11:25 -0400
Well, you can circumvent this on your own machine by setting
register_globals = on in the php.ini file, but the proper way is to
leave it off and go about using the new variable referencing syntax.

Don't forget, however, that you can always reassign the variable, if
that is more to your liking:
$author = $_GET['Author'];


Message #9 by "Nikolai Devereaux" <yomama@u...> on Mon, 13 May 2002 13:56:12 -0700
> Some servers only permit
>
> <?
> //code
> ?>


HUH?  Can you post a link that backs this statement up?  Servers don't care
WHAT's in a file.  If you tell the server to pass the file to the php
interpreter for processing before displaying output to the client, then
you're done.  PHP knows how to handle both <? and <?php.  In fact, many php
developers prefer the use of <?php (e.g. the PEAR people).  The only
configuration setting that I'm aware of which lets you specify which
delimiters are acceptable are the asp-style delimiters (<% and %>).


Take care,

nik

Message #10 by "Jason DeWitt" <roxet@c...> on Mon, 13 May 2002 16:32:53 -0500
you can turn off the use of the plain <? .. ?> tags. You do this by using
the "short_open_tag" directive in the php.ini file. But I've never heard of
not being able to use the <?php ...?> version.


----- Original Message -----
From: "Nikolai Devereaux" <yomama@u...>
To: "beginning php" <beginning_php@p...>
Sent: Monday, May 13, 2002 3:56 PM
Subject: [beginning_php] Re: difficulty in passing a variable to php from
textbox


>
> > Some servers only permit
> >
> > <?
> > //code
> > ?>
>
>
> HUH?  Can you post a link that backs this statement up?  Servers don't
care
> WHAT's in a file.  If you tell the server to pass the file to the php
> interpreter for processing before displaying output to the client, then
> you're done.  PHP knows how to handle both <? and <?php.  In fact, many
php
> developers prefer the use of <?php (e.g. the PEAR people).  The only
> configuration setting that I'm aware of which lets you specify which
> delimiters are acceptable are the asp-style delimiters (<% and %>).
>
>
> Take care,
>
> nik
>
>

Message #11 by "Brian" <vagabond@b...> on Mon, 13 May 2002 14:35:05 -0700 (Pacific Daylight Time)
--------------Boundary-00=_HYJ2O2Q1VA4000000000
Content-Type: Text/Plain;
  charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

>you can turn off the use of the plain <? .. ?> tags. You do this by usin
g=0D
>the "short_open_tag" directive in the php.ini file. But I've never heard
 of=0D
>not being able to use the <?php ...?> version.=0D
=0D
There are 3 open/close tag-sets that PHP recognizes.=0D
=0D
1. <? ... ?>=0D
2. <?php ... ?>=0D
3. <script language=3D"php"> ...</script> =0D
=0D
- Brian

Message #12 by "Brian" <vagabond@b...> on Mon, 13 May 2002 14:38:17 -0700 (Pacific Daylight Time)
--------------Boundary-00=_T3K2ENY1VA4000000000
Content-Type: Text/Plain;
  charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

> But I've never heard of not being able to use the <?php ...?> version.=0D
=0D
Sorry about the last message I misread the last sentence.... I've never
heard of not being able to use the <?php ...?> tag-set either.=0D
=0D
- Brian


  Return to Index