Jump to content


Photo

call on variable variable via POST


  • Please log in to reply
9 replies to this topic

#1 OranniS

OranniS
  • New Members
  • Pip
  • Newbie
  • 6 posts
  • LocationGent, Belgium

Posted 24 September 2006 - 05:03 AM

hello
what i want to exactly is this:
in my code, i have used POST to get a variable to the next page. but the variable itself is also a variable.

for instance:

<? $pagenumber= "2" ?>
<input name="page<?echo="$number"; ?>"

so my question is, how do i call on this variable in the next page?
these are the ones that i have tried yet.

$pagenumber=$_POST['page' . '$number']
$pagenumber=$_POST['page$pagenumber']

none of these work actually, and i even dont know wether its possible, but it would be unlogical if it weren't, wouldn't it.

If you have a way of making this work, via POST or other, let me know! the main idea is to get the value of a variable to the next page, and the value of this variable is" $variable" (yeah i know its darn confusing ;D)

thanks
OranniS

_____________________________________
cross the river styx - OranniS awaits!

#2 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 24 September 2006 - 05:18 AM

why dont you post page and number separate ???
I'm not sure i fully understand what you try to accomplish either

echo $_POST['page'];
echo $_POST['number'];

#3 OranniS

OranniS
  • New Members
  • Pip
  • Newbie
  • 6 posts
  • LocationGent, Belgium

Posted 24 September 2006 - 05:40 AM

well what i want to do :
call on a variable wich was used in the previous page.I did this with POST.
so my ode would look like this:

<input name="awnser1">

-->then the code would be:

$awnser=$_POST['awnser1']

but the clue is that the thing i use POST on is also a variable:
so i get for instance:


<? $page_n=1; ?>
<input name="anwser<?echo"$page_n"?>">

so the server would compile the above as <input name="awnser1">
what i want to use in the next page now is the value "awnser$page_n", but i can't find a way to do this...
_____________________________________
cross the river styx - OranniS awaits!

#4 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 24 September 2006 - 05:46 AM

I think i get your point, try this to extract all posted variables as formname variablenames

<?php

foreach( $_POST as $key => $value )
{
 ${$key} = htmlspecialchars($value);
}

?>


#5 OranniS

OranniS
  • New Members
  • Pip
  • Newbie
  • 6 posts
  • LocationGent, Belgium

Posted 24 September 2006 - 05:53 AM

i... dont think i quite understand everthing in that structure... what does the=> stand for?could you explain it a bit?

<?php

foreach( $_POST as $key => $value )
{
${$key} = htmlspecialchars($value);
}

?>
_____________________________________
cross the river styx - OranniS awaits!

#6 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 24 September 2006 - 06:03 AM

your $_POST is an array of all of the posted variables. the foreach loop does this:

for each array element in the $_POST array, assign the element name to $key and it's corresponding value to $value.  Then (for each one of these $key/$value pairs), make new variable that is named the same as the array element ($key).  so for example, let's say you have a posted variable called number1 that holds the value '1' so that code will in essence do this:

// $_POST['number1'] exists, cuz it was posted
foreach ($_POST as $key => $value) {
  $number1 = 1;
}

an alternative method is to use the extract function:

extract ($_POST);

it pretty much does the same thing as the foreach loop, but there are extra arguments you can use, as far as overwriting pre-existing variable names, adding on prefixes, etc.. (refer to the linkie above)


Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#7 OranniS

OranniS
  • New Members
  • Pip
  • Newbie
  • 6 posts
  • LocationGent, Belgium

Posted 24 September 2006 - 06:15 AM

ok so when i do this script, i should be able to get all my POST varnames, without using $_POST['']?

i'll try this!!

_____________________________________
cross the river styx - OranniS awaits!

#8 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 24 September 2006 - 06:23 AM

yes, yes you can.  but, if  your script is generating variable names in your form, and then you are using the foreach or extract methods to change them from $_POST['blah'] to $blah.. i have to ask you why you wish to do this?  you don't know your variable names, so you won't be able to access them later.  The only reason why generating variable names is really useful is for keeping them in an array for easy looping in the first place.  In which case, there's really no point in doing this. 

well anyways, good lucks.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#9 OranniS

OranniS
  • New Members
  • Pip
  • Newbie
  • 6 posts
  • LocationGent, Belgium

Posted 24 September 2006 - 06:28 AM

actually, that was the whole point, being able to acces them as they were afterwards...

anyway i got an error:

Warning: htmlspecialchars() expects parameter 1 to be string, array given in c:\phpdev\www\public\survey\vraag1.php on line 33

was i supposed to customize that piece of code?

edit:
aaah i get what you mean, the php script first generates the formname using the variable, then in the second page it generates a variable, using the formname that was compiled on the prevoious page,... well you'r right, thats not what i want, what i want is to have the complete syntax as it was before the compiling...
_____________________________________
cross the river styx - OranniS awaits!

#10 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 24 September 2006 - 06:54 AM

use the manual and look up htmlspecialchars()





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users