在codeigniter中同名文本输入提交?

时间:2018-04-20 11:33:49

标签: php codeigniter-3

您好我正在提交同名的表单字段

表格

 <form role="form"  class="m-t-20"  method="post"
                             action='<?php echo base_url();?>save'>

        <tbody>
            <tr>
                <td>
                    <input type="text" name="chemistname[]" id="chemistname" class="form-control m-b-10">
                </td>
                <td>
                    <input type="text" name="chemist_mobile[]" id="chemist_mobile" class="form-control m-b-10" pattern=".{10,10}" title="Enter 10 digit Mobile Number" onkeypress="return isNumber(event)">
                </td>
                <td>
                    <select class="form-control" name="typeofchemist[]" id="typeofchemist">
                        <option>Select Type of Chemist</option>
                        <option value="Attached">Attached</option>
                        <option value="Floating">Floating</option>
                        <option value="Institution">Institution</option>
                    </select>
                </td>
                <td>
                    <input type="text" name="realtion[]" id="realtion" class="form-control m-b-10">
                </td>
            </tr>
            <tr>
                <td>
                    <input type="text" name="chemistname[]" id="chemistname" class="form-control m-b-10">
                </td>
                <td>
                    <input type="text" name="chemist_mobile[]" id="chemist_mobile" class="form-control m-b-10" pattern=".{10,10}" title="Enter 10 digit Mobile Number" onkeypress="return isNumber(event)">
                </td>
                <td>
                    <select class="form-control" name="typeofchemist[]" id="typeofchemist">
                        <option>Select Type of Chemist</option>
                        <option value="Attached">Attached</option>
                        <option value="Floating">Floating</option>
                        <option value="Institution">Institution</option>
                    </select>
                </td>
                <td>
                    <input type="text" name="realtion[]" id="realtion" class="form-control m-b-10">
                </td>
            </tr>
        </tbody>
    </form>

控制器

public function user_registration()
    {   
         $objDate = new DateTime();
         $employee_id = $this->session->userdata('employee_id');
         $usercode =$this->User_model->User_code();
         $form_data = $this->input->post();
        $args        = array('chemistname' => array('filter' => FILTER_SANITIZE_STRING, 'flags' => FILTER_REQUIRE_ARRAY));
        $chemistname     = filter_input_array(INPUT_POST,$args);
        $args        = array('chemist_mobile' => array('filter' => FILTER_SANITIZE_STRING, 'flags' => FILTER_REQUIRE_ARRAY));
        $chemist_mobile     = filter_input_array(INPUT_POST,$args);
        $args        = array('typeofchemist' => array('filter' => FILTER_SANITIZE_STRING, 'flags' => FILTER_REQUIRE_ARRAY));
        $typeofchemist     = filter_input_array(INPUT_POST,$args);
        $args        = array('realtion' => array('filter' => FILTER_SANITIZE_STRING, 'flags' => FILTER_REQUIRE_ARRAY));
        $realtiont     = filter_input_array(INPUT_POST,$args);
        //You may need to check this part
        $newArray=array();
        $i=0;
        foreach($chemistname as $rowChemistName){
        $newArray[$i]['chemist_name']=$rowChemistName;
        $newArray[$i]['chemist_mobile']=$chemist_mobile[$i];
        $newArray[$i]['typeofchemist']=$typeofchemist[$i];
        $newArray[$i]['realtion']=$realtiont[$i];
        $i++;
        }
        echo'<pre>';print_r($chemistname);
        echo'<pre>';print_r($newArray);exit;

}

我需要将这些输入作为单个数组获取,并作为单独的行插入数据库

如何通过使用foreach循环或任何其他条件将数据作为数组获取到数组中

使用codeigniter-3,使用post方法并调用控制器并解析数据并插入数据库

我有相同的三种这样的场景,在表单提交后数据没有被推送到newArray

3 个答案:

答案 0 :(得分:0)

使用您当前的HTML结构,您可以访问和分组数据:

// Loop the chemistname and use $k to extract data from the other inputs in this same position
foreach( $_POST[ 'chemistname' ] as $k=>$v )
{
    echo $k.': '.$_POST[ 'chemistname' ][ $k ].' '.$_POST[ 'chemist_mobile' ][ $k ].'<br>';

    // You can use the $_POST example from my echo statement to build your DB insert
}

答案 1 :(得分:0)

您可以使用php filter_input_array 将html数组处理成php。 首先单独提取所有字段然后尝试使用此值重建数组以在php中进一步处理。 示例代码如下所示。

$args        = array('chemistname' => array('filter' => FILTER_SANITIZE_STRING, 'flags' => FILTER_REQUIRE_ARRAY));
$chemistname     = filter_input_array(INPUT_POST,$args);

$args        = array('chemist_mobile' => array('filter' => FILTER_SANITIZE_STRING, 'flags' => FILTER_REQUIRE_ARRAY));
    $chemist_mobile     = filter_input_array(INPUT_POST,$args);

$args        = array('typeofchemist' => array('filter' => FILTER_SANITIZE_STRING, 'flags' => FILTER_REQUIRE_ARRAY));
$typeofchemist     = filter_input_array(INPUT_POST,$args);

$args        = array('realtion' => array('filter' => FILTER_SANITIZE_STRING, 'flags' => FILTER_REQUIRE_ARRAY));
$realtiont     = filter_input_array(INPUT_POST,$args);
//You may need to check this part
$newArray=array();
$i=0;
foreach($chemistname as $rowChemistName){
    $newArray[$i]['chemist_name']=$rowChemistName;
    $newArray[$i]['chemist_mobile']=$chemist_mobile[$i];
    $newArray[$i]['typeofchemist']=$typeofchemist[$i];
    $newArray[$i]['realtion']=$realtiont[$i];
    $i++;
}

以上所有变量都是数组

答案 2 :(得分:0)

尝试此表格一旦提交,希望您得到答案。

<!DOCTYPE html>
<html>
<head>
    <title>demp</title>
</head>
<body>
    <form method="POST" action="">
        <input type="text" name="d[]">
        <input type="text" name="d[]">
        <input type="submit" name="submit" value="submit" name="submit">
    </form>
</body>
</html>
<?php
if(!empty($_POST['submit']))
{
    echo '<pre>';
    $d_array = $_POST['d'];
    $d_store_values = array();
    foreach($d_array as $key => $d)
    {
        $d_store_values[$key] = $d;
        //insert query
        $sql = "INSERT INTO tablname (dvalue)
        VALUES ($d)";

        if ($conn->query($sql) === TRUE) 
        {
            echo "New record created successfully";
        }
    }
    print_r($d_store_values);
}
?>