469,362 Members | 2,353 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,362 developers. It's quick & easy.

how to generate new row on each row button click

i want to generate a new row having text field,1 add button and 1 remove button..each time when we press add button of current row same way a new row should be generated with same functinality of its add button..
similarly same should happen with remove button that is when we click on remove button the same row should be deleted of which remove button is pressed..
Sep 2 '08 #1
9 16579
Dormilich
8,652 Expert Mod 8TB
Usually I would do that in JavaScript. Nevertheless it's possible in PHP too. I'm looking forward to your code, so we can discuss problems (if there are any).

kind regards
Sep 2 '08 #2
Usually I would do that in JavaScript. Nevertheless it's possible in PHP too. I'm looking forward to your code, so we can discuss problems (if there are any).

kind regards


dear sir i am novice in php .
I need this done in php exclusively.
As u were asking i am sending my code to u below:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <title>
  4. </title>
  5. </head>
  6. <body>
  7. <form name="form1" method="POST" action="">
  8. <table>
  9.  <tr>
  10. <td><input type="text" name="txt" align="center"> </td>
  11. <td><input type="submit" name="btnadd" id="btnadd" value="ADD"> </td>
  12. <td><input type="submit" name="btnremove" id="btnremove" value="REMOVE"> </td>
  13. <td><input type="hidden" name="hidden"  value="abc<?PHP ($j)?>"> </td>
  14. </tr></table>
  15.  
  16. <?PHP
  17.  if($_POST["btnadd"] == "ADD")
  18. {
  19. $j=0;
  20. for($i=0;$i<$j+1;$i++)
  21. {
  22.  ?>
  23. <table>
  24. <tr>
  25. <td><input type="text" name="txt" align="center"> </td>
  26. <td><input type="submit" name="btnadd" id="btnadd" value="ADD"> </td>
  27. <td><input type="submit" name="btnremove" id="btnremove" value="REMOVE"> </td>
  28. <td><input type="hidden" name="hidden"  value="$j ;"> </td>
  29. </tr>
  30. </table>
  31. <?PHP 
  32. }
  33. }
  34. ?>
  35.  
  36. </form>
  37.  
  38. </body>
  39. </html>
  40.  
Sep 2 '08 #3
Dormilich
8,652 Expert Mod 8TB
It looks (please correct me if I'm wrong) as if you want an additional input field added on pressing the 'ADD' button. and you don't want to retain the input (a page reload usually clears all fields).

a simplified version of your code could look like this
Expand|Select|Wrap|Line Numbers
  1. <html>
  2.   <head>
  3.     <title></title>
  4.     <style type="text/css">
  5. input {
  6.   display: block;   /* makes one <input> per line */
  7. }
  8.     </style>
  9.   </head>
  10. <?php
  11. if($_POST["btnadd"] == "ADD") {
  12. // add 1 to the row counter
  13.     if (isset($_POST['count'])) $count = $_POST['count'] + 1;
  14. // set value for first load
  15.     else $count = 1;
  16. }
  17. if($_POST["btnremove"] == "REMOVE") {
  18. // decrement the row counter
  19.     $count = $_POST['count'] - 1;
  20. // set minimum row number
  21.     if ($count < 1) $count = 1;
  22. }
  23. ?>
  24.   <body>
  25. <!-- call same file on submit -->
  26.     <form name="form1" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  27.  <?php 
  28. // print $count rows
  29. for ($i=1; $i<=$count; $i++) {
  30. echo '      <input type="text" name="txt" align="center">';
  31. }
  32. ?>
  33.  <!-- you only need one set of buttons and only one counter for this script
  34.       because every button would do the same -->
  35.       <input type="submit" name="btnadd" id="btnadd" value="ADD">
  36.       <input type="submit" name="btnremove" id="btnremove" value="REMOVE">
  37.       <input type="hidden" name="count" value="<?php echo $count; ?>">
  38.     </form>
  39.   </body>
  40. </html>
regards

PS: please use [code] tags when posting code
Sep 2 '08 #4
thnk u sir for ur help..ur coding is helped me to solve my problex..
thx
Sep 4 '08 #5
Dormilich
8,652 Expert Mod 8TB
I'm glad that I could help you.

kind regards
Sep 4 '08 #6
[
Expand|Select|Wrap|Line Numbers
  1. <html>
  2.   <head>
  3.     <title></title>
  4.     <style type="text/css">
  5. input {
  6.   display: block;   /* makes one <input> per line */
  7. }
  8.     </style>
  9.   </head>
  10. <?php
  11. if($_POST["btnadd"] == "ADD") {
  12. // add 1 to the row counter
  13.     if (isset($_POST['count'])) $count = $_POST['count'] + 1;
  14. // set value for first load
  15.     else $count = 1;
  16. }
  17. if($_POST["btnremove"] == "REMOVE") {
  18. // decrement the row counter
  19.     $count = $_POST['count'] - 1;
  20. // set minimum row number
  21.     if ($count < 1) $count = 1;
  22. }
  23. ?>
  24.   <body>
  25. <!-- call same file on submit -->
  26.     <form name="form1" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  27.  <?php 
  28. // print $count rows
  29. for ($i=1; $i<=$count; $i++) {
  30. echo '      <input type="text" name="txt" align="center">';
  31. }
  32. ?>
  33.  <!-- you only need one set of buttons and only one counter for this script
  34.       because every button would do the same -->
  35.       <input type="submit" name="btnadd" id="btnadd" value="ADD">
  36.       <input type="submit" name="btnremove" id="btnremove" value="REMOVE">
  37.       <input type="hidden" name="count" value="<?php echo $count; ?>">
  38.     </form>
  39.   </body>
  40. </html>
sir the coding u send me is exactly matches to suituation i want but but i want that particular row deleted up which row's remove button is clickd.and i want this all thing exclusively in php..plz hepl me sir ..

Thnk you.......
Sep 6 '08 #7
Dormilich
8,652 Expert Mod 8TB
does the input fields contain any data? if not it doesn't matter. In the other case you have to retain the data anyhow (maybe something like <input name="line[]">). then you have to apply somehow a line number to the button to tell which values to drop. I have to think about this problem (this wouldn't be a problem in JS). still you need only one add-button.
Sep 6 '08 #8
sir i only need this in php..to drop particular row whose remove button is being clicked
Sep 8 '08 #9
Dormilich
8,652 Expert Mod 8TB
general idea: name every submit button uniquely and check, which row this button belongs to.
[PHP]// get the 'number' of the pressed button
foreach (array_keys($_POST) as $key) {
if (preg_match('@^submit([0-9]+)$@', $key, $match) == 1) {
$no = $match[1];
$count--;
}
}

// remove value
unset($_POST['text'][$no]);
// re-index array
$retain = array_values($_POST['text']);

for ($i=0; $i<$count; $i++) {
echo ' <input type="text" name="text[]" value="' . $retain[$i] . '" />
<input type="submit" name="submit' . $i . '" value="remove" />
<br />';
}
[/PHP]
Sep 8 '08 #10

Post your reply

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

Similar topics

reply views Thread by Avon | last post: by
15 posts views Thread by Orchid | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.