apot Posted September 28, 2007 Share Posted September 28, 2007 I need help with $_POST I'm trying to pass a variable into a form and cannot get the data from $_FORM using the same variable. Is this because I can't pass a variable into a form name? $i = 1; <?php echo "<form><input type=\"text\" name=\"name$i\">"; echo "<input type=\"submit\" name\"submit\" value=\"submit\"></form>"; ?> have also tried: $i = 1; <?php echo "<form><input type=\"text\" name=\"name$i\">"; echo "<input type=\"submit\" name\"submit\" value=\"submit\"></form>"; ?> now getting the information: <?php $name = $_POST['name1']; echo $name; ?> Is this possible? If not is there another way to do it? thanks Quote Link to comment Share on other sites More sharing options...
rarebit Posted September 28, 2007 Share Posted September 28, 2007 You need to tell the form where to go... <form method="POST" action='myfile.php'> else it all looks ok... Just remember to check what you get! Quote Link to comment Share on other sites More sharing options...
apot Posted September 28, 2007 Author Share Posted September 28, 2007 sorry I did have action="myfile.php in my script. I just quickly wrote that code to describe what i was talking about and forgot to write that in. Anything else you see wrong? Quote Link to comment Share on other sites More sharing options...
dingus Posted September 28, 2007 Share Posted September 28, 2007 yah you are need to define the POST method in your form tags because most browser default to GET i would also suggest escaping you text for the variable echo "<form><input type=\"text\" name=\"name" . $i . "\">"; and possable useing a single quote to save on the \ (cant use to many stocks are limited) echo '<form><input type="text" name="name"' . $i . '">'; Quote Link to comment Share on other sites More sharing options...
sasa Posted September 28, 2007 Share Posted September 28, 2007 you must specified method="POST" in form tag default is GET and $i = 1 must be inside <?php tag Quote Link to comment Share on other sites More sharing options...
apot Posted September 28, 2007 Author Share Posted September 28, 2007 I have made all those corrections and now i am trying to get the post info while using a variable. <?php $i = 1; $name = $_POST['name$i']; ?> have also tried: <?php $i = 1; $name = $_POST['name.$i']; ?> Is this possible? Quote Link to comment Share on other sites More sharing options...
marcus Posted September 28, 2007 Share Posted September 28, 2007 print_r($_POST); Or run a loop to see which value exists. <?php for($i=1;$i<=50;$i++){ if($_POST['name' . $i]){ echo $_POST['name' . $i] . " exists<br />\n"; } } ?> <?php $ran = rand(1,50); ?> <form method="post"> <input type="text" name="name<?=$ran?>" value="<?=$ran?>"> <input type="submit"> </form> Quote Link to comment Share on other sites More sharing options...
sasa Posted September 28, 2007 Share Posted September 28, 2007 try $name = $_POST["name$i"]; or $name = $_POST['name'.$i]; Quote Link to comment Share on other sites More sharing options...
marcus Posted September 28, 2007 Share Posted September 28, 2007 @sasa, either way works. Quote Link to comment Share on other sites More sharing options...
apot Posted September 28, 2007 Author Share Posted September 28, 2007 sweet the $_POST['name'.$i]; worked perfect. it works now! Thanks a lot!! 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.