0

Hi i am submitting a form fields with same name

Form

 <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>

Controller

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;

}

I need to get these inputs as single array and to be inserted as individual rows in the database

How should i get those values into an array by using foreach loop or any other condition to get the data as an array

Am using codeigniter-3, am using post method and am calling the controller and parsing the data and inserting into database

I have same three scenarios of this kind, after form submission the data is not pushed into newArray

Sairam Duggi
  • 165
  • 3
  • 14

3 Answers3

0

With your current HTML structure you can access and group the data like this:

// 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
}
MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
0

You can use php filter_input_array to process html array into php. Extract all the field individually first next try to rebuild an array with this value to process further in php. Sample code given bellow.

$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++;
}

all the above variable will be array

  • I have implemented the above code but the values are not returning and am getting the data from the form in this way $form_data['chemistname'], Should i Change INPUT_POST to $form_data@Prithwis – Sairam Duggi Apr 20 '18 at 12:50
  • Input_post is your form method. Check if it is get or post in your form. Where did you stuck? – Prithwis Chatterjee Apr 20 '18 at 14:53
  • am submitting the form except this data all the data is been displayed am using post method @Prithwis – Sairam Duggi Apr 21 '18 at 04:22
  • try print_r($chemistname) and other three other array $chemist_mobile , $typeofchemist and $realtiont . If you are able to get data from this variable then go to next step with foreach loop – Prithwis Chatterjee Apr 21 '18 at 04:27
  • am not getting the data can i share the code to you @Prithwis – Sairam Duggi Apr 21 '18 at 05:32
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/169496/discussion-between-sairam-duggi-and-prithwis-chatterjee). – Sairam Duggi Apr 21 '18 at 10:02
0

Try this once form get submitted, hope so you get your answer.

<!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);
}
?>
Bits Please
  • 877
  • 6
  • 23