gevo12321 Posted July 10, 2007 Share Posted July 10, 2007 would the following be a valid statement for php? $name[$id]=$_POST[button[$id]]; im trying to set a variable within a variable would it be possible im trying to say that for example $name1=$_POST[button1}; would that work? Quote Link to comment Share on other sites More sharing options...
suma237 Posted July 10, 2007 Share Posted July 10, 2007 r u getting any error? Quote Link to comment Share on other sites More sharing options...
gevo12321 Posted July 10, 2007 Author Share Posted July 10, 2007 i havent actually tried it yet because the code i have to change is huge but i was just wondering if it would work and that noting is wrong with its format Quote Link to comment Share on other sites More sharing options...
suma237 Posted July 10, 2007 Share Posted July 10, 2007 i also tried the code that you posted.No error is showing in the browser. i am not sure about the validity. Quote Link to comment Share on other sites More sharing options...
chronister Posted July 10, 2007 Share Posted July 10, 2007 Yes, I don't see anything wrong with this code. It should work for you. However, your setting your name variable and button variable to be an array. If you want it to be $name1=$_POST[button1] you should do it like this. <?php $id=1; $name.$id=$_POST[button.$id]; ?> You have to concatenate the 2 variables you want to join. Quote Link to comment Share on other sites More sharing options...
gevo12321 Posted July 10, 2007 Author Share Posted July 10, 2007 the code u gave me doesnt work it gives a blank page when i try to echo $name1 and it gives no errosr but actually i want $id to be an array so i tryed my original code it says: unexpected '[', expecting ']' so i changed the code to this: $name[$id]=$_POST[button][$id]; but this doesnt work either it gives me the following error: Warning: Illegal offset type so i guess it's not valid? do you know how it might work? Quote Link to comment Share on other sites More sharing options...
gevo12321 Posted July 10, 2007 Author Share Posted July 10, 2007 bump Quote Link to comment Share on other sites More sharing options...
per1os Posted July 10, 2007 Share Posted July 10, 2007 Post more code. Quote Link to comment Share on other sites More sharing options...
gevo12321 Posted July 10, 2007 Author Share Posted July 10, 2007 this code is the form: <html> <body> <form action="2.php" method="post"> <input type="text" name="button1"> <input type="text" name="button1"> <input type="submit" value="enter"> </form> </body> </html> this code is the 2.php: <?php $id=array(1,2) $name.$id=$_POST[button.$id]; echo $name1; echo $name2; ?> Quote Link to comment Share on other sites More sharing options...
per1os Posted July 10, 2007 Share Posted July 10, 2007 You have two options <html> <body> <form action="2.php" method="post"> <input type="text" name="button1"> <input type="text" name="button2"> <!-- notice I changed this to 2, crazy how it works! --> <input type="submit" value="enter"> </form> </body> </html> <?php $id=array(1,2); foreach ($id as $val) { $name[$id] = $_POST['button'.$id]; } echo $name[1]; echo $name[2]; ?> Or <html> <body> <form action="2.php" method="post"> <input type="text" name="button[]"> <input type="text" name="button[]"> <input type="submit" value="enter"> </form> </body> </html> <?php foreach ($_POST['button'] as $key => $val) { $name[$key] = $val; } echo $name[0]; echo $name[1]; ?> Either should work. Quote Link to comment Share on other sites More sharing options...
gevo12321 Posted July 10, 2007 Author Share Posted July 10, 2007 for the first one it says: Warning: Illegal offset type the second one says Warning: Invalid argument supplied for foreach() Quote Link to comment Share on other sites More sharing options...
per1os Posted July 10, 2007 Share Posted July 10, 2007 <html> <body> <form action="2.php" method="post"> <input type="text" name="button1"> <input type="text" name="button2"> <!-- notice I changed this to 2, crazy how it works! --> <input type="submit" value="enter" name="submit"> </form> </body> </html> <?php if (isset($_POST['submit'])) { $id=array(1,2); foreach ($id as $val) { $name[$id] = $_POST['button'.$id]; } echo $name[1]; echo $name[2]; } ?> <?php if (isset($_POST['submit'])) { foreach ($_POST['button'] as $key => $val) { $name[$key] = $val; } echo $name[0]; echo $name[1]; } ?> <html> <body> <form action="2.php" method="post"> <input type="text" name="button[]"> <input type="text" name="button[]"> <input type="submit" name="submit" value="enter"> </form> </body> </html> Is the form and the php code on the same page? If so try the above as what was happening is it was trying to access the data with no data, which generally does not play well. Quote Link to comment Share on other sites More sharing options...
gevo12321 Posted July 10, 2007 Author Share Posted July 10, 2007 for the first one the page is completely blank the second one works but i don't quite understand it could you explain in a sentence what foreach statement is saying? Quote Link to comment Share on other sites More sharing options...
per1os Posted July 10, 2007 Share Posted July 10, 2007 For every item in the array assign it the key to $key and the value to $val and loop. Basically it loops through the array so you can access the elements. www.php.net/foreach Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.