Jump to content

I Need Help with Dynamically Adding New Form Fields! Please?!


jadeoracle

Recommended Posts

Hello!

 

I'm new to PHP coding, and I'm stuck. I'm trying to code an order form that allows the user to add more fields if they need to. I have the order form display 5 item lines. Each line has a line number, item name box, and quantity box. If the user has more than 5 items to order, I have a button that says, "Add More Items". If the user presses that button another item line (or set of 5 more lines, doesn't matter) should appear at the bottom of the list, extending the form.

 

How do I do this?  ???

 

I'm thinking I need to use isset() to check is that "Add More Items" button has been pressed, and then unset() the button, but I'm not sure... Perhaps a checkbox would be better? The only way I understand so far is to retrieve form input is to use $_POST['formDataName'] but I technically don't want to send all the data yet, just add more fields. Like I said, I'm a beginner, so any help is greatly appreciated.

 

The Uline site has an order form that extends, which is what I'm wanting to mimic:

 

http://www.uline.com/EasyOrder.asp

 

Of course, it seems that form is using JavaScript to pull off the adding of the new fields. I'm hoping I can use PHP to do the same ... if it's possible.

 

Below is a stripped down basic version of what I have so far with my form.

 

<form method="post" action="quickorder.php">
     <table border="1">
          <tr>
               <td>NO.</td>
               <td>ITEM NAME</td>
               <td>QUANTITY</td>
          </tr>

          <?php
               $counter = 1;
     
               function orderField () {
                    global $counter;
                    print "<tr>\n";
                    print "<tr>\n";
                    print "<td width=\"5\">$counter</td>\n";
                    print "<td><input type=\"text\" name=\"$item\" size=\"30\" /></td>\n";
                    print "<td><input type=\"text\" name=\"$quantity\" size=\"10\" /></td>\n";
                    print "</tr>\n";
               }

               if ($counter == 1)  {
                    for ($x = 1; $x <= 5; $x++) {
                         $item = "item" . $counter;
                         $quantity= "quantity" . $counter;
                         orderField ();
                         $counter++; 
                    }
               }
          ?>

          <tr>
               <td colspan="3">
                    <input type="button" name="moreItems" value="Add More Items" />
                    <input type="submit" value="Submit Order" />
                    <input type="reset" value="Reset Form" />
               </td>
          </tr>
     </table>
</form>

Link to comment
Share on other sites

you coudl do one of two things.

 

you could use PHP as your asking and have the <add more items>  button resubmit that page.  then what you woudl do is add the values back intot eh field, and echo more fields form your PHP script.

 

Using javascript, you would create new elements in thr form - probably using a name like <*** name = name_1>  having "1" increment.  then you could easily play with those names on the form handling page.

 

Hope that helps to spell it out a little more.

 

gdlk

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.