开发者

Inputing as array in textbox group

开发者 https://www.devze.com 2023-02-28 16:32 出处:网络
I have created a textbox group using JavaScript i.e. on clicking Add button new set of textboxes will be added to the page.

I have created a textbox group using JavaScript i.e. on clicking Add button new set of textboxes will be added to the page.

var newTextBoxDiv = $(document.createElement('div'))
                 .attr("id", 'TextBoxDiv' + counter);

newTextBoxDiv.after().html('<label>Track #'+ counter + ' : </label>' + 
               '<input type="text" name="textbox1[]' + counter + '" value="" >' + 
               '<input type="text" name="textbox2[]' + counter + '" value="" >' );

newTextBoxDiv.appendTo("#TextBoxesGroup");

DIV class for my PHP page is..

    <div id='TextBoxesGroup'> <div id="TextBoxDiv1"> <label>Track #1 : </lab开发者_如何学JAVAel>
    <input name="textbox1[]" type='textbox' ><input name="textbox2[]" type='textbox'>
    </div>
    </div>

Now I want to create an array so that, easily input values of this set of text boxes to database. Instead of using multidimensional array, I want to use single array so values of set of textbox1# stores as key for array and values of set of textbox2# stores as corresponding values for array.


When you submit a form through POST, it doesn't make a multi-dimensional array of your input fields. Rather, it uses the name attribute of the field as the key in the $_POST array.

So when you're making the textboxes, make textbox2 an array name, so that you can have more than one field, like so:

var newTextBoxDiv = $(document.createElement('div'))
                 .attr("id", 'TextBoxDiv' + counter);

newTextBoxDiv.after().html('<label>Track #'+ counter + ' : </label>' + 
               '<input type="text" name="textbox1[]' + counter + '" id="textbox1' + counter + '" value="" >' + 
               '<input type="text" name="textbox2[]' + counter + '" id="textbox2' + counter + '" value="" >' );

newTextBoxDiv.appendTo("#TextBoxesGroup");

Then you can get the keys using $_POST['textbox1'] and the values will be stored in $_POST['textbox2'].

Then to use it, let's say you wanted to get the first textbox1/textbox2 pair, you would access that value using $_POST['textbox1'][0] and $_POST['textbox2'][].

To loop through the two arrays and extract all of the values (assuming that the number of textbox1 and textbox2 fields are the same), you can do something like this:

$inputs;
$length = count($_POST['textbox1']);
for($i=0; $i< $length; $i++){
$inputs[$_POST['textbox1'][$i]] = $_POST['textbox2'][$i];
}


I don't think you can do that directly. You have to deal with the way HTML pass forms to PHP. You shouldn't use the same names for all the textboxes. The easiest way, I think, would be to use 'textbox1[]' and 'textbox2[]' as names, so you get an array from all the textboxes. Then, with a loop, you can build a new array the way you want using textbox1 and textbox2 arrays.


I made some sample code to show you how the different attributes of checkboxes can be used to map a key to a value in an checkbox array:

So you are able to control what value is mapped to which key... If you want that something is insertet as a value to a key in a checkbox array just do the following:

<input type="checkbox" id="somewhat1" name="checkboxes[key1]" value="value1"/> where "checkboxes" will be an array containing key1 with value1

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title></title>
</head>
<body>

    <?php
    echo print_r($_POST) . "<br/>";
    $checkboxvalues = $_POST['checkboxes'];
    echo "key1 = " . $checkboxvalues["key1"] . "<br/>";
    ?>

    <form action="index.php" method="post">
        asd1<input type="checkbox" id="somewhat1" name="checkboxes[key1]" value="value1"/><br/>
        asd2<input type="checkbox" id="somewhat2" name="checkboxes[key2]" value="value2"/><br/>
        asd3<input type="checkbox" id="somewhat3" name="checkboxes[key3]" value="value3"/><br/>
        <input type="submit" value="ok">
    </form>

</body>


$(document).ready(function(){

var counter = 5;

$("#addButton").click(function () {

if(counter>50){
        alert("Only 50 textboxes allow");
        return false;
}   

var newTextBoxDiv = $(document.createElement('div'))
     .attr("id", 'TextBoxDiv' + counter);

newTextBoxDiv.after().html('<div class="form-group" id="TextBoxesGroup" ><div class="col-md-5" id="TextBoxDiv'+ counter + '"><input type="text" name="Features['+ counter + ']" id="textbox' + counter + '" placeholder="Features type" class="form-control " value="" ></div> ' +
      '<div class="col-md-6" id="TextBoxDiv'+ counter + '"><input type="text" name="Features_v[' + counter + 
      ']" id="textbox' + counter + '" <input type="textbox" placeholder="Features description" class="form-control " value="" ></div>'+ '<div class="col-md-1" id="TextBoxDiv'+ counter + '"><i class="fa fa-trash btn" id="removeButton" ></i></div></div>');

newTextBoxDiv.appendTo("#TextBoxesGroup");


counter++;
 });

});

0

精彩评论

暂无评论...
验证码 换一张
取 消