jimleeder123 Posted April 20, 2015 Share Posted April 20, 2015 I'm making my own CMS for my company so going through making the pages for the back end. What I want to do that I'm stuck on is -------------- I want to allow the user to specify how many input text fields they want shown on the page. I'm thinking that they could write a number then click submit, send the data to the same page (PHP self or action blank) and then the required amount of input fields would be echoed out. Echoing out is easy, I want to know how to find out how many fields are required, and then process it. Thanks in advance. Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted April 20, 2015 Share Posted April 20, 2015 using your idea of passing in the number to the page, you would set up a conditional to check if the value had been posed from the input form, then loop through an input box chearion echo untill you meet the number entered: if(!isset($_POST['noOfInputs']{ //<your normal page code here> ... } else{ $lim = $_POST['noOfInputs']; for ($i=0;$i<$lim;$i++){ echo '<input type="text", name="custUrserInput'.$i.'" />' } } You will obviously need to do your own sense-checking on the input value from the user, but that's the basic idea. Quote Link to comment Share on other sites More sharing options...
jimleeder123 Posted April 20, 2015 Author Share Posted April 20, 2015 I've got the input boxes coming up. I am trying to add an auto increment on the "name" so I can then post it onto the next page and add each field to the database. Heres my code - $optid=1; echo str_repeat('Option <br /> <input type="text" name="option'.$optid.'" /> <br /> <br />', $number); $optid++; It's echoing the name as "option1" every time, but I want it to be option2, option3 and so on. Quote Link to comment Share on other sites More sharing options...
IThinkMyBrainHurts Posted April 20, 2015 Share Posted April 20, 2015 Is that in a loop, else $optid is redundant... AFAIK str_repeat() just repeats a string, nothing special. Quote Link to comment Share on other sites More sharing options...
jimleeder123 Posted April 20, 2015 Author Share Posted April 20, 2015 I tried it with the for loop as Muddy wrote and it works, thanks! Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted April 20, 2015 Share Posted April 20, 2015 this part of the code I posted before : for ($i=0;$i<$lim;$i++){ echo '<input type="text", name="custUrserInput'.$i.'" />' } does what you you are trying. you need to use a custom loop so that you can control the increment within the loop. just change the input type to options for a select: for ($i=0;$i<$lim;$i++){ echo '<option value="custUrserInput'.$i.'">'.$i.'</option>'); } Quote Link to comment Share on other sites More sharing options...
jimleeder123 Posted April 20, 2015 Author Share Posted April 20, 2015 Just one more thing needed for this - on the page where I am inserting the post data into the mysql database (data from the form posted to this page), I want to add auto increment to $_POST['option'] so its option0, option1, option 2 etc. How can I add the auto increment to this? Quote Link to comment Share on other sites More sharing options...
maxxd Posted April 20, 2015 Share Posted April 20, 2015 Just one more thing needed for this - on the page where I am inserting the post data into the mysql database (data from the form posted to this page), I want to add auto increment to $_POST['option'] so its option0, option1, option 2 etc. How can I add the auto increment to this? Make the text input name an array. That way you can simply loop through the values in your php script. Like so: //in the form itself for ($i=0;$i<$lim;$i++){ print("<input type='text' name='custUrserInput[]' id='{$i}' />"); } //processing script foreach($_POST['custUrserInput'] as $userValue){ print("<p>\$_POST['custUrserInput'][] is {$userInput}</p>"); } This way, in your processing script, you don't need to know how many fields the user has added. Just loop through the number that are there and process them individually. Quote Link to comment Share on other sites More sharing options...
IThinkMyBrainHurts Posted April 20, 2015 Share Posted April 20, 2015 'option'.$n Quote Link to comment Share on other sites More sharing options...
jimleeder123 Posted April 21, 2015 Author Share Posted April 21, 2015 When I send the data in the mysql query, I will be adding the post data to the database ($_POST['option0'], option1, option2 etc). How can I find the data here or am I better to just have all their names as "option" and add them in like that with mysql? Quote Link to comment Share on other sites More sharing options...
jimleeder123 Posted April 21, 2015 Author Share Posted April 21, 2015 (edited) Ok, I've got this code include "includes/connect.php"; $groupid = $_POST['hidden']; //id for the attribute group $number = $_POST['hiddennumber']; //number of fields entered $i = 0; echo "number "; echo $number; echo "<br /> <br />"; for ($i=0;$i<$number;$i++){ //to find the auto increment on option post names //$option = $_POST['option$1']; // for testing - shows options entered echo "LLL: "; echo $_POST["option$i"]; echo "<br /> <br />"; $query = "INSERT INTO attributeoptions (attopt_group_id, attopt_name) VALUES ('$groupid', '$option".$i."')"; $result = mysql_query($query) or die (mysql_error()); } But it is inserting the post values as the auto increment numbers ( $i ). How can I get it to put the actual values in the Post? (When I echo them out as shown above it shows the actual text I enter into the text boxes) I have tried doing $option$id and a whole lot of other methods to put the Post data in a variable. Edited April 21, 2015 by jimleeder123 Quote Link to comment Share on other sites More sharing options...
jimleeder123 Posted April 21, 2015 Author Share Posted April 21, 2015 This has been fixed via http://stackoverflow.com/questions/9552023/post-variable-name-is-variable-how-to-retrieve 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.