newrow

Dynamic table row inserter

This page will shows how to add new html row with included textboxes and other components when the button is clicked. this uses Jquery,php and mysql..Try sample here :


Year : Month : Week :    
Actual : Max : Target :


here I have encoded javascript code :




here I have encoded HTML code :




Finally I have encoded PHP submit code :



Enjoy it... thanks

16 comments:

  1. Thanks for good job , Please I need a button for deleting the current row , I try but without result, I user the jquery remove function but it doesn't work

    ReplyDelete
  2. Thanks for that , i need to send these data in database but , it getting some problem

    ReplyDelete
    Replies
    1. What is the problem you got here? Its working fine on me.

      Delete
  3. anyone know how i can limit this to only add 10 rows max?

    ReplyDelete
    Replies
    1. Eric, In javascript file there is a variable called "currentItem". You can have a condition on it as less than or equal 10.
      if (currentItem<=10)
      {
      currentItem++;
      $('#items').val(currentItem);
      var strToAdd = 'new row details';
      $('#data').append(strToAdd);
      }

      Delete
    2. thank you for that, works great. Second issue i'm having is with the php code.

      I get undefined variable count and before the for statement i added $count=1; however it will then only process one set of variables, if i set $count to 10 it will run the for statement 10 times, but i get an undefined variable for each item less than 10 that are submitted from the form. How can i get the for loop to only run for the amount of lines submitted from the form?

      appreciate the assistance

      Delete
    3. scratch that, figured out how to just pass the script variable currentItem to php. Thanks again.

      Delete
    4. how do you solve the issue ??

      Delete
  4. Hi!

    Great example!

    Any ideas how to make dynamic calculations? For example, I'm using this for employee list, with add new I add name input field, family status select box (single/family), department select box (department1, department2) etc. And then at the forms bottom I need to calculate how many singles are in department1, how many family in department2 etc. It means, I need to check connection of both select fields per row and then make some calculations further with these values!

    So, the question, how I can check if ($(#selectX) == ZZ) && $(#selectY) == WW)), where X and Y is "items" number!

    If I'm putting this into $('#addnew').click(function(){, then operations works fine with current row, but it's not working with previous rows if user wants to change info for row "item - 1"!

    I hope You understand the question! ;-)

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. Sorry. Did not post correctly.

    Any ideas on how to get this to post to .txt/.csv instead of mysql? Essentially I just need to understand how to get this into a .csv fileper a clients request. I can currently do this with a single form but the tutorial you posted here is immensely helpful as an individual submitting a form will have multiple entries. I also have no knowledge of mysql. Thanks for the help.


    Current version of php to compete Form to CSV.

    for( $i = 0; $i <= $count; $i++ )
    {
    date_default_timezone_set('America/New_York');
    $today = date("m.d.y");
    $area0 = $_POST['area'.$i];

    //the data
    $data = "$today, $area0, $contractor, $hours, $project, $town, $street\n";

    //open the file and choose the mode
    $fh = fopen("users2.csv", "a");
    fwrite($fh, $data);

    //close the file
    fclose($fh);
    }


    //close the file
    fclose($fh);

    ReplyDelete
  7. so i tried cutting this into jsfiddle and it does not work...any ideas?

    ReplyDelete
  8. how can i get this to work with PDO

    ReplyDelete
  9. If you would like this to work in PDO I have spent a good few hours working out how and this should work - thanks to the creators for this nice code! Change your php to this:

    setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    $count = 1;
    if(isset($_POST['submit']))
    {
    for( $i = 1; $i <= $count; $i++ )
    {
    $stmt = $db->prepare("INSERT INTO `table_test`(`a`,`b`,`c`,`d`,`e`,`f`) VALUES(:year$i,:month$i,:week$i,:actual$i,:max$i,:target$i)");
    $year = $_POST['year'.$i];
    $month = $_POST['month'.$i];
    $week = $_POST['week'.$i];
    $actual = $_POST['actual'.$i];
    $max = $_POST['max'.$i];
    $target = $_POST['target'.$i];

    $stmt->execute(array(
    ':year'.$i => $year,
    ':month'.$i => $month,
    ':week'.$i => $week,
    ':actual'.$i => $actual,
    ':max'.$i => $max,
    ':target'.$i => $target
    )
    );

    $count = $_POST['items'];

    }}

    $db = null;

    ?>

    ReplyDelete
  10. The first line of php code got cut off, however it is just the db login:
    $db = new PDO('mysql:dbname=ENTERDBNAME;host=localhost', 'ENTERUSER', 'ENTERPASS');

    ReplyDelete
  11. we have an icon(glyphicon) related to the bootstrap. so we need to get that icon dynamically when we click on add new item button simultaneously with other input fields.

    ReplyDelete