Jump to content

How to echo number of text input fields as defined by the user


jimleeder123

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>');
 }
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by jimleeder123
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.