0

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!

3
Contributors
7
Replies
48
Views
3 Years
Discussion Span
Last Post by bLuEmEzzy
0

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!

0

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?

0

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

2

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...

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.