1

I went through almost all of the previous topics in the site and saw may options on how to add a value from a cell to a mysql using cell name & array variable. But I couldn't find anything similar to the innerhtml method that i use to create table rows dynamically.Please help.

This is my html

<form action="test.php" method="post" name="f"> 
    <table class="inventory">
        <thead>
            <tr>
                <th><span contenteditable>SL. NO.</span></th>
                <th><span contenteditable>DESCRIPTION / PURPOSE</span></th>
                <th><span contenteditable>AMOUNT</span></th>

            </tr>
        </thead>
        <tbody>

        </tbody>
    </table>
<INPUT type="submit" value="Insert" name="submit" />
</form>
<a class="add">+</a>

When I click the + a new row is generated using javascript. And when I click '-' the row gets deleted.

This is my javascript for click event

function onClick(e) {
    var element = e.target.querySelector('[contenteditable]'), row, rowCount, targetTbl;

    element && e.target != document.documentElement && e.target != document.body && element.focus();

    if (e.target.matchesSelector('.add')) {

        document.querySelector('table.inventory tbody').appendChild(generateTableRow());
    }
    else if (e.target.className == 'cut') {
        row = e.target.ancestorQuerySelector('tr');

        row.parentNode.removeChild(row);
    }

    updateInvoice();
}

this is my add row function

function generateTableRow() {

var emptyColumn = document.createElement('tr');

emptyColumn.innerHTML = '<td><a class="cut">-</a><span id="slnocell[]" contenteditable></span></td>' +
    '<td><span id="desccell[]" contenteditable></span></td>' +
    '<td><span data-prefix>Dhs </span><span id="amtcell[]" contenteditable>0.00</span></td>';

return emptyColumn;
}

And this is my php

<?php

require('application_top.php');

GLOBAL $mysql_db;

$mysql_db = new Database();

$mysql_db->mysqldb_connect(DBHOST,DBPORT,DBNAME,DBUSER,DBPASS);

if(isset($_POST['submit']))
{
 foreach ($_POST['slnocell'] as $key => $value) 
    {
        $item = $_POST["slnocell"][$key];
        $desc = $_POST["desccell"][$key];
        $amt = $_POST["amtcell"][$key];


        $mysql_db->mysqldb_query("INSERT INTO testtbl VALUES  ('','$item', '$desc', '$amt')");

        $mysql_db->mysqldb_execute();
    }
}
?>

When I click form submit I get two errors

"undefined index slnocell"

"invalid argument supplied foreach()"

Please help, how to save data to mysql.The sql connection is ok because it returns the "connected" string which I use to check connection.

My SQL structure is as follows

CREATE TABLE `testbbl` (
`id` int(11) NOT NULL auto_increment,
`item` varchar(200) NOT NULL,
`desc` varchar(200) NOT NULL,
`amt` varchar(200) NOT NULL,
PRIMARY KEY  (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf-8 AUTO_INCREMENT=1 ;

1 Answers1

1

You have to create the database table first once before inserting data inside.