Good day, can you please help me how to add textbox dynamically and insert its value to database. I tried Javascript in adding textbox, but how can I insert its value?? Thank You!

I had a code in creating textbox, the only problem was, how can I insert it on mysql.

function adda() {

    var tbl = document.getElementById("tblHist");
        var row = document.createElement("TR"); /*row*/

        var td1 = document.createElement("TD");
        var strHtml1 = "<input type=\"text\" width=\"100\">";
        td1.innerHTML = strHtml1.replace(/!count!/g,count);

    row.appendChild(td1);
    tbl.appendChild(row);


} 

Thank You!

Member Avatar for diafol

For this you'll need a server-side language, like PHP. Where are you with regard to running PHP or MySQL - have you got these installed?

Yeah, my problem is how can i insert the added textbox value in my database(mysql).

Member Avatar for diafol

OK I take it that you're not experienced with mysql or php?
First off you'll need to give the added textbox a name attribute, otherwise the value will not be picked up via GET or POST - whichever you're using. Try running this and you'll understand why:

<?php
    print_r($_POST);
?>
<!doctype html>
<html>
<head></head>
<body>
<form method="post">
    <input type="text" width="100" name="named" />
    <input type="text" width="100"  />
    <input type="submit" value="SUB" />
</form>
</body>
</html>

So, back to your js - you can include the attribute in the vat itself if you want:

 var strHtml1 = "<input type=\"text\" name=\"myfield[]\" width=\"100\">";

Some js afficionados may have a better way to apply a name attribute, I don't know, but anyway, the main point is that you probably need to set up an array value for the name: "myfield[]" - note the square brackets.

This means you can add as many textboxes as you like without having to keep track of which one is which or supply a unique name every time.

You may end up with something like this if you add 3 textboxes to the original one in the form already (I've excluded the html table items):

<form method="post">
    <input type="text" name="myfield[]" width="100" />
    <input type="text" name="myfield[]" width="100" />
    <input type="text" name="myfield[]" width="100" />
    <input type="text" name="myfield[]" width="100" />
    <button>Add a textbox</button>
    <input type="submit" name="submit" value="Send" />
</form>

You pick up the values of the textboxes in $_POST['myfield'] which will be an array.

That should be a start...

Thank You, I'll try what had you given.

Hi sir, I had tried what had you given, but I'm still confused on what will I do, to save the added textbox in the database. Sorry. I do not know what do you mean by this $_POST['myfield']? Thank You

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.