Passing Values

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

crzyman
Forum Newbie
Posts: 18
Joined: Fri Sep 16, 2005 10:44 pm

Passing Values

Post by crzyman »

I am trying to pass a value, from one page using a form, like this:

Code: Select all

<? $title = "name"; ?><FORM METHOD="POST" ACTION="/my_page/names/get_name.php"><input type="hidden" name="<?php $title ?>">
And get the value on get_name.php, with POST, like this:

Code: Select all

$name = $_POST['title'];
Can someone please tell me what I am doing wrong? Thanks.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Are you setting a value to the hidden input?

Code: Select all

<input type="hidden" name="title" value="blah blah" />
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you're passing no value, but "dynamically" changing the name of the hidden field.
crzyman
Forum Newbie
Posts: 18
Joined: Fri Sep 16, 2005 10:44 pm

Post by crzyman »

I tried this, but it still won't pass the value. I want the value of $title to be passed to get_name.php. I am still new to php, so please forgive my lack of knowledge.

Code: Select all

<? $title = "name"; ?><FORM METHOD="POST" ACTION="/my_page/names/get_name.php"><input type="hidden" name="<?php $title ?>"  value="<?php $title1 ?>" >
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

<FORM METHOD="POST" ACTION="/my_page/names/get_name.php"><input type="hidden" name="title"  value="<?php echo $title ?>" />
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Post by blacksnday »

when grabbing values from sql I use something like

*value from sql*

Code: Select all

$id=mysql_result($result,$i,"sid");
Then for form to get the value...
<input type="hidden" name="ud_id" value="<? echo "$id"; ?>">

then for second page to use the value...

Code: Select all

$query="UPDATE table SET sid='$ud_id'";
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

be aware that will only work (by default) if register_globals is set to on, which they should not be in this day and age (wow, I sound old)
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Post by blacksnday »

feyd wrote:be aware that will only work (by default) if register_globals is set to on, which they should not be in this day and age (wow, I sound old)
are you talking about my example?
I just need to know so i could correct some code in case :)


btw... you are only as old as you let yourself think you are :roll:
hence my problem i guess.. but thats not for a php forum! more like a
'HELP IM NOT A NORMAL PERSON FORUM'
but we could all use that anyways!
crzyman
Forum Newbie
Posts: 18
Joined: Fri Sep 16, 2005 10:44 pm

Post by crzyman »

I tried the code you gave me. It did not work. I tried to put a ";" at the end of $title after the echo. It still will not work. I do not have register_globals turned on. Is this the problem?

Code: Select all

<FORM METHOD="POST" ACTION="/my_page/names/get_name.php"><input type="hidden" name="title"  value="<?php echo $title ?>" />
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

blacksnday wrote:are you talking about my example?
yes. :)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

crzyman wrote:I do not have register_globals turned on. Is this the problem?
no, but did you forget to set $title?
crzyman
Forum Newbie
Posts: 18
Joined: Fri Sep 16, 2005 10:44 pm

Post by crzyman »

Nope,

Code: Select all

$title = "name";
Any idea's?
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Post by blacksnday »

feyd wrote:
blacksnday wrote:are you talking about my example?
yes. :)
ok, with my example, how do i correct it to use if Register Globals turned off?

I want to make sure my code is portable, since I eventually plan to release it as open source
once it has enough features and is code worthy.
I would imagine it would be by at least removing the $ud_ part of it?


Also, should I really bug my host to turn off register Globals?
(i am the only active account of server! arent I lucky? :) lol)
So if needed I could make sure it was there.

btw.. How can i donate to these forums?
the help I have been given so far to develop my code as
extensive as it so far...is priceless!
i dont plan to profit from my code once I make it public release,
but still feel this forum ownwer should be given some donation
for all the help it has given a newbie like me :)
Even if we diagree with my IP ONLY ADMIN function, no one has bother to
check out my extremly fast and safe shout box that will also be released on it's own.

Thanks for all the help AND advice :)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

You should always define your variables explicitly, regardless of register_globals setting :)

Example of why:

Code: Select all

<?php

$result = mysql_query("SELECT * FROM users WHERE user='$user' AND password='$password'");

if (mysql_num_rows($result) > 0) {

  $auth = 'yes';

}

if ($auth == 'yes') {

//do stuff..

}

?>
Now, ignoring the poor coding, with the above, we are assuming register_globals is ON and the file name is page.php.

What was to happen if someone enters the url with:

"/page.php?auth=yes"

so to avoid it, define each and every variable, even if you are defining it as null, to avoid any complicaitons brought on by register_globals.

I prefer to set them to the "majority" option, for example I use variables for storing a users access level(s) so when I define the variable, it is set to the same level that a new user would get.
Last edited by Jenk on Sat Oct 08, 2005 11:57 pm, edited 1 time in total.
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Post by blacksnday »

crzyman wrote:Nope,

Code: Select all

$title = "name";
Any idea's?
Doesnt $title need to get defined somewhere?

like...........

$title = $POST[title]; or
$title = $GET[title];
or
$title = $POST['title'];
$title = $GET['title];


?
Post Reply