Jump to content

Using PHP to add rows to a form


Chakron

Recommended Posts

Hey everyone,

 

I just started using php a couple days ago and I've run into an issue. This is also my first time coding anything, so even basic things are a bit difficult for me.

 

What I have posted below is an html form with 5 fields. After typing values (into as many or as little fields as you want) and hitting "submit", the form will echo how many values you've entered (integers, strings, whatever, it'll add one to the counter as long as the value isn't negative). The values will also remain in the form after being submitted (this is intentional, and actually what I'm after). This is working just fine. What I'm trying to add is an option to switch between a maximum of either 5 fields (rows), or 10 fields. The code below manages to do this, and actually return the correct result after hitting "submit". Unfortunately, it won't "hold onto" any of the values I enter past the original 5 fields. So for example if I switch to 10 fields, and enter "1, 2, 3, 4, 5, 6, and 7" into the fields, it will display "7 items", however, only the "1, 2, 3, 4, and 5" will remain in their respective fields, while the "6, and 7" will disappear.

 

So, two questions. First, how do I fix that? And second, what's actually the best way for me to do what I'm trying to do? Because I'm guessing I've come up with a pretty bad newbie hacked-together way of doing it :)

 

(Note: This is all part of a larger function that has proper data sanitation and etc, I just cut it down for this example.)

 

Thanks!

 

Edit: I'd also like to add I ideally want this entirely in php (no javascript) for compatibility reasons.

 

<?php // rows.php

$a[1] = $a[2] = $a[3] = $a[4] = $a[5] = $a[6] = $a[7] = $a[8] = $a[9] = $a[10] = $rows = $r10 = '';
$r5 = 'checked';

  if ($r5 == 'checked')
  {
  $start_r_cnt = 1;
  $end_r_cnt = 5;
  }

  if (isset($_POST['rows']))
  {
    $check_radio = $_POST['rows'];
    if ($check_radio == 'r10')
{
  $r10 = 'checked';
  $start_r_cnt = 1;
  $end_r_cnt = 10;	  
    for ($i=6;$i<=$end_r_cnt;$i++)
    {
      $value = "$a[$i]";
      $rows = $rows.'<tr><td><input type="text" name="a'.$i.'" size="10" value="'.$value.'"/></td></tr>';
             $value = $a[$i];
    }
}
  }
  
  $count_a = 0;
  $value_error = '';

  for($i=1;$i<=$end_r_cnt;$i++)
  {
    if(isset($_POST['a'.$i]))
    {
      $a[$i] = $_POST['a'.$i]; 
  if($a[$i] == '') {
        $a[$i] == '';
      }
  elseif($a[$i] >= 0) {
        $count_a++;
      }
  elseif ($a[$i] < 0) {
	$value_error = 'One of the entered values is negative. Please fix this before continuing.';
  }
    }
  }

echo <<<_END

<html>
<form method="post" action="rows.php">
<table cellpadding="5">
<b>Number of Rows:</b><br>
<input type="submit" name="rows" value="Change" style="width: 70px" /> [5]<input type='radio' name='rows' value='r5' $r5/>[10]<input type='radio' name='rows' value='r10' $r10/>
<tr><td><center>Data A</center></td></tr>
<tr><td><input type="text" name="a1" size="10" value='$a[1]'/></td></tr>
<tr><td><input type="text" name="a2" size="10" value='$a[2]'/></td></tr>
<tr><td><input type="text" name="a3" size="10" value='$a[3]'/></td></tr>
<tr><td><input type="text" name="a4" size="10" value='$a[4]'/></td></tr>
<tr><td><input type="text" name="a5" size="10" value='$a[5]'/></td></tr>
$rows
<tr><td><center><input type="submit" value="Submit" style="width: 80px" /></center></td></tr>
</table>
<b>$count_a</b> items are present.
<b>$value_error</b>
</html>
_END;
?>

Link to comment
Share on other sites

Right, I mentioned that (and I agree). But if it's possible to do in php, it would be a lot easier for me (the final product has to be able to run on some school computers that block javascript in really annoying ways, sadly)

Link to comment
Share on other sites

isn't it possible to first let them decide 5 or 10 and than get 5 or 10 rows from a database with LIMIT

 

than output the form directly with a foreach loop like:

// some data I if you get this from a database i would use LIMIT to fetch either 5 or 10
$your_array = array(
    'value1','value2','value3','value4','value5','value6','value7','value8','value9','value10',
);

//foreach loop
$rows = '';
foreach($your_array as $value){
    $rows .= '<tr><td>'.$value.'</td><td>mooOOO</td></tr>';
}

// echo form
echo $form = <<<FORM
<table border="1">
<thead>
    <tr>
        <th>some</th>
        <th>stuf</th>
    </tr>
</thead>
<tbody>
    $rows
</tbody>
</table>
FORM;

 

Not sure if that is what you are looking for but it might help

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.