How to generate a row of form elements based on a $variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
edawson003
Forum Contributor
Posts: 133
Joined: Thu Aug 20, 2009 6:34 am
Location: Los Angeles, CA - USA

How to generate a row of form elements based on a $variable

Post by edawson003 »

I am trying to set up an application that will allow a user to generate a row of form elements based on a $variable that would increase in numerical value based on a form action such as submit. Also, vice a versa, I would like to remove form element rows as well. I made an attempt but so far I am only able to add row and that is it. For my approach to work I need to set a php element/$variable that I can keep adding a value to incrementally. I feel like I am almost there and just missing a key piece. Can someone gimme a clue? Maybe my whole approach is flawed, so please dont be bashful.
Set_Add.gif
Set_Add.gif (8.52 KiB) Viewed 454 times

Code: Select all

 
<?
                 $set = 0;
                 $endset = 3;                
                 if(isset($_POST['add_a_set_x'])){
                 $endset1[] = 1;
                 $endset = $endset + 1;
                 }               
                 
                 while($set != $endset){
                 echo "              
                 <table width=370 border=0 cellspacing=0 cellpadding=0>
                    <tr>                       
                      <td width=37 bgcolor=EAE9E9 align=center><span class=psm_b>Set ";
                      echo $set + 1;
                      echo "</span></td>
                      <td width=5>&nbsp;</td>
                      <td width=46>
                        <input type=text name=num_sets maxlength=3 size=3 value=";
                        
                        echo isset($_POST['num_sets']) ? $_POST['num_sets'] : '';
                        
                        echo ">
                      </td>
                      <td width=5>&nbsp;</td>
                      <td width=46>
                        <input type=text name=w8_lbs maxlength=3 size=3 value=";
                        
                        echo isset($_POST['w8_lbs']) ? $_POST['w8_lbs'] : '';
                        
                        echo ">
                      </td>
                      <td width=5>&nbsp;</td>
                      <td width=46>
                        <input type=text style=background-color: #FFF475; name=w8_units maxlength=3 size=3 value=";
                        
                        echo isset($_POST['w8_units']) ? $_POST['w8_units'] : '';
                        
                        echo ">
                      </td>
                      <td width=5>&nbsp;</td>
                      <td width=30 align=center>
                        <input type=checkbox name=w8_spot value=1 ";
                        if($_POST['w8_spot'] == '1' || $w8_spot == '1') { print 'checked'; }                        
                        echo ">";
                        echo "
                      </td>
                      <td width=12>&nbsp;</td>
                      <td width=84>
                        <input type=text name=spot_set maxlength=3 size=7 value=";
                        
                    echo isset($_POST['spot_set']) ? $_POST['spot_set'] : ''; 
                    
                     echo ">
                      </td>
                      <td width=5>&nbsp;</td>
                      <td width=39>";
                      if($set+1 == $endset){echo "<span class=psm_b>Remove</span>";}                      
                      echo "</td>
                    </tr>
                    <tr> 
                      <td colspan=13 height=5><img src=../images/spacer10.gif width=10 height=5></td>
                    </tr>
                  </table>
                 ";
                 $set = $set + 1;
                 }  
                 ?>
 
User avatar
BlaineSch
Forum Commoner
Posts: 28
Joined: Sun Jun 07, 2009 4:28 pm
Location: Trapped in my own little world.

Re: How to generate a row of form elements based on a $variable

Post by BlaineSch »

Javascript would be a lot easier...

Code: Select all

<html>
<head>
<title>BlaineSch.com</title>
<script>
var i = 0;
function addOne() {
    i++;
    var parent = document.getElementById('BlaineSch');
    var child = document.createElement('div');
    child.setAttribute('id','bsChild'+i);
    var childfield1 = document.createElement('input');
    childfield1.setAttribute('type','text');
    childfield1.setAttribute('name','name[]');
    var childfield2 = document.createElement('a');
    childfield2.setAttribute('href','javascript&#058;removeChild(\'bsChild'+i+'\');');
    childfield2.innerHTML = 'Remove';
    child.appendChild(childfield1);
    child.appendChild(childfield2);
    parent.appendChild(child);
}
function removeChild(id) {
    var parent = document.getElementById('BlaineSch');
    var child = document.getElementById(id);
    parent.removeChild(child);
}
</script>
</head>
<body>
    <div id="BlaineSch"></div>
    <a href="javascript&#058;addOne();">Add 1</a>
</body>
</html>
If you wanna do it with PHP just make links that change the address (get values)
User avatar
edawson003
Forum Contributor
Posts: 133
Joined: Thu Aug 20, 2009 6:34 am
Location: Los Angeles, CA - USA

Re: How to generate a row of form elements based on a $variable

Post by edawson003 »

Thanks for the javascript alternative. It seems that as I take on more complex functionality tasks, client side scripting seems to become more an more unavoidable. I guess I need to get more familiar with it.

With regard to your second suggestion (the address_get values), I took a shot at implementing this, with no luck. So far I get it to add only one row from 3 to 4 and that's it. What piece am I missing to get the PHP solution to work? If it ends up not be practical, then I will dust off my javascript book :)

Code: Select all

 
<?
                  if(!isset($_GET['truset'])){
                 $set = 0;
                 $truset = 3;               
                 } 
                 
                 if(isset($_POST['addaset_x'])){
                 if(isset($truset) && !isset($_GET['truset'])){
                 $truset = $truset + 1;
                 header( "Location: http://www.url.com/membersarea/add_ex_p ... et=$truset" ); 
                 }
                 elseif(isset($truset) && isset($_GET['truset'])){
                 $truset = $_GET['truset'] + 1;
                 header( "Location: http://www.url.com/membersarea/add_ex_p ... et=$truset" );
                 }                   
                 }                               
                                        
                 if(isset($_GET['truset'])){
                 $truset = $_GET['truset'];              
                 }                               
                 while($set != $truset){
                 echo "              
                 <table width=370 border=0 cellspacing=0 cellpadding=0>
                    <tr>                       
                      <td width=37 bgcolor=EAE9E9 align=center><span class=psm_b>Set ";
                      echo $set + 1;
                      echo "</span></td>
                      <td width=5>&nbsp;</td>
                      <td width=46>
                        <input type=text name=num_sets maxlength=3 size=3 value=";
                        
                        echo isset($_POST['num_sets']) ? $_POST['num_sets'] : '';
                        
                        echo ">
                      </td>
                      <td width=5>&nbsp;</td>
                      <td width=46>
                        <input type=text name=w8_lbs maxlength=3 size=3 value=";
                        
                        echo isset($_POST['w8_lbs']) ? $_POST['w8_lbs'] : '';
                        
                        echo ">
                      </td>
                      <td width=5>&nbsp;</td>
                      <td width=46>
                        <input type=text style=background-color: #FFF475; name=w8_units maxlength=3 size=3 value=";
                        
                        echo isset($_POST['w8_units']) ? $_POST['w8_units'] : '';
                        
                        echo ">
                      </td>
                      <td width=5>&nbsp;</td>
                      <td width=30 align=center>
                        <input type=checkbox name=w8_spot value=1 ";
                        if($_POST['w8_spot'] == '1' || $w8_spot == '1') { print 'checked'; }                        
                        echo ">";
                        echo "
                      </td>
                      <td width=12>&nbsp;</td>
                      <td width=84>
                        <input type=text name=spot_set maxlength=3 size=7 value=";
                        
                    echo isset($_POST['spot_set']) ? $_POST['spot_set'] : ''; 
                    
                     echo ">
                      </td>
                      <td width=5>&nbsp;</td>
                      <td width=39>";
                      if($set+1 == $endset){echo "<span class=psm_b>Remove</span>";}                      
                      echo "</td>
                    </tr>
                    <tr> 
                      <td colspan=13 height=5><img src=../images/spacer10.gif width=10 height=5></td>
                    </tr>
                  </table>
                 ";
                 $set = $set + 1;
                 }  
                 ?>
 
Last edited by edawson003 on Sat Nov 21, 2009 6:07 pm, edited 1 time in total.
User avatar
BlaineSch
Forum Commoner
Posts: 28
Joined: Sun Jun 07, 2009 4:28 pm
Location: Trapped in my own little world.

Re: How to generate a row of form elements based on a $variable

Post by BlaineSch »

I'm not really sure what your trying to do. You have it now so you can assign "truset" with a default value of 3 to tell how many rows you want. If you want to remove a row just get that value and take off one, to add a new one just increase it. Put those in links, right?
User avatar
edawson003
Forum Contributor
Posts: 133
Joined: Thu Aug 20, 2009 6:34 am
Location: Los Angeles, CA - USA

Re: How to generate a row of form elements based on a $variable

Post by edawson003 »

I'm not quite sure what you are trying to suggest as a solution. In the part of my code where I set truset = 3, I am trying to set up a default of three rows upon landing on the page. I am trying to utilize the url and the $_GET to pull down a value, add to it and push the new value out as a url again, but it only seems to work once. It would be helpful if you provided a code example.
Post Reply