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

how to generate new row on each row button click

P: 5
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
Share this Question
Share on Google+
9 Replies


Dormilich
Expert Mod 5K+
P: 8,639
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

P: 5
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
Expert Mod 5K+
P: 8,639
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

P: 5
thnk u sir for ur help..ur coding is helped me to solve my problex..
thx
Sep 4 '08 #5

Dormilich
Expert Mod 5K+
P: 8,639
I'm glad that I could help you.

kind regards
Sep 4 '08 #6

P: 5
[
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
Expert Mod 5K+
P: 8,639
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

P: 5
sir i only need this in php..to drop particular row whose remove button is being clicked
Sep 8 '08 #9

Dormilich
Expert Mod 5K+
P: 8,639
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.