To retrieve value from a text box in a form (view)

2019-03-03 12:51发布

问题:

view page code

<td align="left" valign="middle" bgcolor="#FFFFFF" class="rows">
    <input type="text" name="Quantity" id="Quantity" value="<?= $row->Quantity ?>" />

Controller code

$category    = $this->input->post('Category');
$num         = $this->input->post('numOflimit');
$productName = $this->input->post('product_name');
$barcode     = $this->input->post('barcode');
$quantity    = $this->input->post('Quantity');

for ($x = 1; $x <= $num; $x++) {
    $userArray = $_POST["select$x"];
}

$userArray   = split(',', $userArray);
$productName = $userArray[0];
$barcode     = $userArray[1];
$quantity    = $userArray[2];
$flag        = $this->cartmodel->getProductNames($category);

print_r($flag);

The problem in the code is that it is able only to fetch the last text box value and the output of

$userArray it is taking as Array

and

$flag = CI_DB_mysql_result Object (
    [conn_id]       => Resource id #27 
    [result_id]     => Resource id #36 
    [result_array]  => Array ( ) 
    [result_object] => Array ( ) 
    [current_row]   => 0 
    [num_rows]      => 0
)

What wrong with the code???

回答1:

You are missing [], which means you are not creating an array, just writing over the same value. Try something like:

for ($x = 1; $x <= $num; $x++) {
    $userArray[] = $this->input->post("select" . $x . "");
}

To be sure you only get actual values you might even want to do:

for ($x = 1; $x <= $num; $x++) {
    if ($this->input->post("select" . $x . "")) {
        $userArray[] = $this->input->post("select" . $x . "");
    }
}


回答2:

Can you show your view file code, which select tag exists(because in above view section, you only include textbox, not the select box). I guess you missed to add "multiple" in your select tag.