By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,210 Members | 1,851 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,210 IT Pros & Developers. It's quick & easy.

Auto generate text boxes for submission

P: 5
I am trying to setup a webpage in PHP to auto generate a table filled with text boxes for a form submission..

Is this possible to do in PHP??

Basically what I need to do is have a main page that asks 2 questions.. Question 1 is how many rows and question 2 is how many columns and when you hit submit it generates the table filled with text boxes that can be filled in with values and added together and then submitted to a SQL database.

I am rather new to PHP and am unsure if this is possible? I have searched the web and haven't been able to find anything
Jun 14 '10 #1
Share this Question
Share on Google+
7 Replies

Expert 100+
P: 1,584
Yes this is possible.

What code have you attempted?

Do you know how to work with posted variables in PHP?

Do you know how loops work?

Is your MySQL database setup and can you connect to it with PHP code?
Jun 14 '10 #2

P: 5
I am still pretty new to PHP and learning... I have some code somewhat similar to this setup now that pulls tables from the database and drops it all into a dynamically generated table... Am assuming this will be similar..

I haven't attempted any code yet as I am still unsure how to begin creating it... I have been looking at some samples of code from other sites but all of them want to mix javascript in with it and I would rather stick with PHP... The data integrity must remain server-side if possible to help prevent highjacking of the data and manipulation..

I know a little about variables and arrays...

Loops I know... They are relatively easy...

I have a small simple CMS I have setup just to maintain users for this site... Yes it uses MYSQL...
It is likely that I will setup a separate database to hold the data generated within this particular code...

Most of my issues so far with generating code is trying to remember all the rules for formatting... I have the basics down of PHP (so far)...
Jun 14 '10 #3

P: 424

you need to go to this link and scroll down... this will show you how to generate rows... you could use it as a base to work from on you loops.

also the part where it says the code below... the number "4" is the amount of columns it will echo into your page so however many columns is filled in your form will have to be placed there

Expand|Select|Wrap|Line Numbers
  1. if ($i == 4){
  2. echo '</tr>';
  3. $i = 0;
  4. }
Jun 14 '10 #4

P: 5
Hey I appreciate the link...

That one looks like it is pulling info from a database to display it... What I am trying to do is different but it gives me a good idea of what I need to do...

Any and all help is appreciated...
Jun 14 '10 #5

P: 5
The one I found which is very close to what I need is this but it uses javascript and it doesn't generate tables:

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3.      <script>
  4.       function create_input_boxes()
  5.       {
  6.        if(document.getElementById("name0")){return true;}
  7.       var boxes="";
  8.      var num_boxes=document.getElementById("num_boxes").value;
  9.       if(num_boxes)
  10.       {
  11.        for(var i=0;i<num_boxes;i++)
  12.        {
  13.         boxes+="<input id='name"+i+"' name='name"+i+"' value=''><br />";
  14.         }
  15.        document.getElementById("textbox_container").innerHTML=boxes;
  16.        }
  17.        return false;
  18.        }
  19.      </script>
  20.      </head>
  21.     <body>
  22.      <form id="theForm" action="#" method="post" onsubmit="return create_input_boxes();">
  23.      <div id="textbox_container"></div>
  24.      Enter number input boxes: <input name="num_boxes"><br />
  25.      <div id="submit_button"><input type="submit" name="submit" value="Submit"></div>
  26.      </form>
  27.      <?php
  28.      if(isset($_POST["submit"]))
  29.      {
  30.       for($i=0;$i<count($_POST);$i++)
  31.       {
  32.        $name[]=!empty($_POST["name".$i])?$_POST["name".$i]:""; // All dynamically created inputs are being stored again in $name array
  33.       }
  34.       for($i=0;$i<count($name);$i++)
  35.       (
  36.        echo $name[$i]."<br />";
  37.        }
  38.       }
  39.     ?>
  40.       </body>
  41.       </html> 
Jun 14 '10 #6

P: 424
it can be used without pulling data from a DB... you minus the top loop and take off the last bracket, use a for loop instead of if loop it may help... make $i the post data for the rows and then use the post data for the column where the number 4 is.... that's all
Jun 14 '10 #7

P: 5
Ok I will look into that then... Much appreciated
Jun 14 '10 #8

Post your reply

Sign in to post your reply or Sign up for a free account.