-1

I am using following code to generate multiple textbox on label click, it works correctly and multiple textbox created with different name. I want to take the value of textbox at php side and run insert query for each textbox. But i don't know how to use it in php. Here is my code plz help.

<html>
<head>
<title>

</title>
<script>
   var index = 1;
    function insertRow(){
        var table=document.getElementById("rep1");
        var row=table.insertRow(table.rows.length);
        var cell1=row.insertCell(0);
        var t1=document.createElement("input");
            t1.id = "txtName"+index;
            t1.name = "txtName"+index;
            cell1.appendChild(t1);
   index++;
}
</script>
</head>
<body>
<label id="btnAdd" style=" width: 10px;" class="button-add" onclick="insertRow();">add</label>
<table  id="rep1">

</table>
</body>
</html>

SQL Schema :

T_no(AI)     Text_value

PHP CODE:

 <?php
 if(isset($_POST["submit"]))
 {
     $t1=$_POST['name'];// i am confused here how to take value and run query for all
 ?>        
M. Eriksson
  • 13,450
  • 4
  • 29
  • 40
Shalini
  • 131
  • 1
  • 10

3 Answers3

2

Client side Code

You must use Array for multiple name field

<script>
   var index = 1;
    function insertRow(){
        var table=document.getElementById("rep1");
        var row=table.insertRow(table.rows.length);
        var cell1=row.insertCell(0);
        var t1=document.createElement("input");
            t1.id = "txtName"+index;
            t1.name = "txtName[]"; //Use array of names not id
            cell1.appendChild(t1);
   index++;
}
</script>

Please check How to get form input array into PHP array

And You can fetch those array server side using $_POST['txtName']; or any other request method

Server side coding for inserting array into database

<?php
if(isset($_POST["submit"]))
{ 
    $t2=$_POST['txtName']; 
    $query_parts = array();
    $qu = "INSERT INTO try VALUES";
    foreach($t2 as $val)
    {

         $query_parts[] = "('', '" . $val . "')";
    }
    $qu .= implode(',', $query_parts);
    $res=mysqli_query($con,$qu);
    if(!$res) { echo("Error description: " . mysqli_error($con)); } 

}
?>

The above code will not prevent data from SQl injection.You can use prepared statement for prevent SQL injection in PHP. Link

Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
Vishnu Bhadoriya
  • 1,655
  • 1
  • 20
  • 28
  • Yes what you suggest is working, but i am not able to send value via query. Will you please help – Shalini Feb 05 '18 at 05:43
  • please show how you are sending those values @Shalini – Vishnu Bhadoriya Feb 05 '18 at 05:44
  • if(isset($_POST["submit"]) { $t2=$_POST['txtName']; $qu="INSERT INTO `try` VALUES ('','$t2')"; $res=mysqli_query($con,$qu); if($res) { echo ''; } else { echo "Error:".mysqli_error($con); exit(); echo ''; } } – Shalini Feb 05 '18 at 05:46
  • ok you are inserting whole array please use foreach or other loop for inserting multiple data.An array can not be stored in database – Vishnu Bhadoriya Feb 05 '18 at 05:48
  • ya i use this code it work. but what if there is 2 different textbox then how to execute for each loop for them. if used: foreach ($t2 as $value ) { $qu="INSERT INTO `try` VALUES ('','$value')"; $res=mysqli_query($con,$qu);} – Shalini Feb 05 '18 at 05:57
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/164524/discussion-between-vishnu-bhadoriya-and-shalini). – Vishnu Bhadoriya Feb 05 '18 at 06:02
  • This code is **wide open to [SQL Injections](http://php.net/manual/en/security.database.sql-injection.php)**. You should really use **[Prepared Statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php)** instead of concatenating your queries. Specially since you're not escaping the user inputs at all! You should also refrain from using bold, unless you what to emphasize something specific. – M. Eriksson Feb 05 '18 at 06:07
  • @vishnu will you please be on chat so that i can take help from you on my query – Shalini Feb 05 '18 at 06:40
  • anyone suggest how to run foreach loop multiple time – Shalini Feb 06 '18 at 06:29
  • @VishnuBhadoriya do i add columns by using your previous code – Shalini Feb 15 '18 at 07:04
  • Please do futher messages or comment here https://chat.stackoverflow.com/rooms/165198/discussion465 – Vishnu Bhadoriya Feb 15 '18 at 07:14
1

Use this code. It will helps you.

<?php 
print_r($_POST['txtName']); // Here you can get you all values.
?>
<html>
<head>
<title>

</title>
<script>
   var index = 1;
    function insertRow(){
        var table=document.getElementById("rep1");
        var row=table.insertRow(table.rows.length);
        var cell1=row.insertCell(0);
        var t1=document.createElement("input");
            t1.id = "txtName"+index;
            t1.name = "txtName[]"; // Use array in textbox name..
            cell1.appendChild(t1);
   index++;
}
</script>
</head>
<body>
    <form method="POST" action="" >
        <label id="btnAdd" style=" width: 10px;" class="button-add" onclick="insertRow();">add</label>
        <table  id="rep1">

        </table>
        <input type="submit">
    </form>
</body>
</html>
Gaurav Kandpal
  • 1,250
  • 2
  • 15
  • 33
1

For this you have to store total index value also(that should specify how many time you created input field).After that your php code should be like below..

PHP CODE:

 <?php
     if(isset($_POST["submit"]))
     {
     $t1=$_POST['txtName'.$index]; 
     ?>  

Where index variable will your current index value from your loop for length of total input fields

Ms.KV
  • 214
  • 2
  • 10
  • Use of undefined constant index – Shalini Feb 05 '18 at 05:48
  • Create hidden field in your php code.Create one variable to store total index value in your javascript insertRow() after that put that variable value in your hidden input field value.Now you are able to fetch this hidden field value in your PHP code and by this you could apply loop in before **$t1=$_POST['txtName'][index];** – Ms.KV Feb 05 '18 at 05:56
  • This answer is not going to work. If you look at the OP's code (`t1.name = "txtName"+index;`), the fields will be called `txtName1, txtName2, txtName3, etc` and won't be an array. – M. Eriksson Feb 05 '18 at 05:57
  • I edited my answer and comments like this - Create hidden field in your php code.Create one variable to store total index value in your javascript insertRow() after that put that variable value in your hidden input field value.Now you are able to fetch this hidden field value in your PHP code and by this you could apply loop in before **$t1=$_POST['txtName'.$index];** – Ms.KV Feb 05 '18 at 06:03