Sangeet Shukla
Sangeet Shukla

Reputation: 37

How to get the value of these created textboxes?

Whenever I am clicking on "ADD TEXTBOX" button it will create new +1("MULTIPLE") HTML textbox with jQuery's append method but i am not get the logic to get their value in php.

$(w).append('<div><input type="text" name=/><a href="#" id="removes">Remove</a><br/></div>');

Suppose if I create 5 new textboxes than how to assign them name that it will not create problem when I will get the value with PHP & store it in database.

Upvotes: 1

Views: 69

Answers (4)

FatalError
FatalError

Reputation: 550

Try this way:

$(w).append('<div><input type="text" name="textboxname[]"/><a href="#" id="removes">Remove</a><br/></div>');
                                              ^^^ add name like this

And you have get the value in PHP like this:

Suppose you are using POST method:

$fisrt = $_POST['textboxname'][0];
$second = $_POST['textboxname'][1];

Or you can use foreach loop like this:

foreach($_POST['textboxname'] as $values){
//you logic
}

Upvotes: 4

sanir ranta
sanir ranta

Reputation: 291

Append input field as array

$(w).append('<div><input type="text" name="txt1[]"/><a href="#" id="removes">Remove</a><br/></div>');

txt1[] will make your text field as array then in php get txt1[] using foreach

if(isset($_POST['txt1'])){
   $txt_array = $_POST['txt1'];
   foreach(txt_array as $key => $value){
     echo $value . "<br>";
   }
}

Upvotes: 0

dsharew
dsharew

Reputation: 10665

How about like this?

for(var i=0; i<5; ++i){
  $(w).append('<div><input type="text" name=input" +
         i + "/><a href="#"     id="removes">Remove</a><br/></div>');
}

Then you can use similar looping on your PHP side.

for( i=0; i<5; ++i){//not sure the exact syntax of php
 echo $_POST['input'.i];
}

Upvotes: 1

javacreed
javacreed

Reputation: 968

In the javascript when you are using this append method, use a variable and keep incrementing its value every time you append, use that variable value for the naming purpose.

It will be like name+1=name1 for first input name+2=name2 for second input..

Upvotes: 0

Related Questions