0

I have a form with some fields normal fields as well as the array fields but when I'm submitting the form everything working alright but I'm getting array fields to value only the index is zero I don't know why this is happening this is happening

This is the response I'm getting when printing the request value where I have append two many rows but the response is the only index with zero fields

Array
(
    [txt_location] => Array
        (
            [0] => Uttar Pradesh
        )

    [txt_traveldate] => Array
        (
            [0] => 2019-04-06
        )

    [txt_age] => Array
        (
            [0] => 2
        )

    [txt_from] => Array
        (
            [0] => Delhi
        )

    [txt_to] => Array
        (
            [0] => Allahabad
        )

    [transport] => Array
        (
            [0] => Flight
        )

    [txt_departure] => Array
        (
            [0] => 10:30
        )

    [txt_preference] => Array
        (
            [0] => No preferred
        )

    [txt_remark] => Array
        (
            [0] => Remark qts 2
        )

    [txt_hotel] => Array
        (
            [0] => Kanha Shyam
        )

    [txt_checkin_date] => Array
        (
            [0] => 2017-04-06
        )

    [txt_checkin_time] => Array
        (
            [0] => 10:50
        )

    [txt_checkout_date] => Array
        (
            [0] => 2017-04-08
        )

    [txt_remarks] => Array
        (
            [0] => what to say
        )

    [submit] => 
)

This is the array fields which I'm appending using the help of jquery

<tr>
   <td width="5%">
    <a href="#" class="btn btn-danger btn-xs delete-row"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></a>
   </td>
   <td><input type="text" name="txt_traveldate[]" class="form-control" placeholder="Date"></td>
   <td><input type="number" name="txt_age[]" class="form-control" placeholder="Age"></td>
   <td><input type="text" name="txt_from[]" class="form-control" placeholder="From" id="search-term"></td>
   <td><input type="text" name="txt_to[]" class="form-control" placeholder="To"></td>
   <td>
     <select name="transport[]" class="form-control">
        <option value="Flight">Flight</option>
        <option value="Train">Train</option>
        <option value="Bus">Bus</option>   
        <option value="Cab">Cab</option>   
     </select>
    </td>
    <td><input type="text" name="txt_departure[]" class="form-control" placeholder="Dep. Arr.Time"></td>
    <td><input type="text" name="txt_preference[]" class="form-control" placeholder="Preferances"></td>
    <td><input type="text" name="txt_remark[]" class="form-control" placeholder="Remarks"></td>
</tr>

Why am I getting data with zero indexes only?

Mahonri Moriancumer
  • 5,993
  • 2
  • 18
  • 28
  • I think with the fields adding the `<` entities. Go through this question [link]https://stackoverflow.com/questions/5068951/what-do-lt-and-gt-stand-for check the posted values. – mageDev0688 Apr 06 '19 at 11:27
  • Sir my question is completely different please go through with question first – Madhukant Tiwari Apr 06 '19 at 12:04
  • 1
    Could you please show me your jQuery code? So, this helps us to rectify the issue. – mageDev0688 Apr 06 '19 at 12:31
  • I agree with mageDev, my suspicion is that your jQuery process is only targetting a single `` of data (which is why they are all [0] and only one row. You need to be capturing the full table of form data I presume. – mickmackusa Apr 06 '19 at 15:34
  • Thanks, megeDev0688 I have changed my form from to
    and now It's working fine Thank you so much
    – Madhukant Tiwari Apr 15 '19 at 05:32

1 Answers1

-1

name="txt_from[]" tells the system that it's an array of values. If you're not accepting multiple values, just use name="txt_from". That will turn the submitted value to just one value, not an array of them.

Also, note that only Checkboxes, Selects and Files accept multiple values. Text, Number, Email etc don't accept multiple values. Unless you're doing something with Javascript.

Yousof K.
  • 1,374
  • 13
  • 23
  • I just wanted to insert multiple values at a time – Madhukant Tiwari Apr 06 '19 at 11:54
  • $txt_remark=$_POST['txt_remark'];for($x=0; $x<$count; $x++){ $sql_insert=exeQuery("INSERT INTO travel_booking set request_id='".$request_id."', booking_date='".$txt_traveldate[$x]."', booking_age='".$txt_age[$x]."', booking_from='".$txt_from[$x]."', booking_to='".$txt_to[$x]."', booking_transport='".$transport[$x]."', departure_arrival_time='".$txt_departure[$x]."', preferences='".$txt_preference[$x]."', remarks='".$txt_remark[$x]."'"); } – Madhukant Tiwari Apr 06 '19 at 11:55
  • As long as you have the `[]` in your `name` attributes, they will continue to submit as arrays. If you want just one value, remove it. If you want to deal with multiple values,you will have to iterate through them with `foreach($input_name as $key => $value)` – Yousof K. Apr 06 '19 at 12:16
  • I need values in the array and I don't want one value My query is When I'm submitting the form with array attribute I'm getting the single array value which index is 0 only I also want that values which indexes are 1,2...n – Madhukant Tiwari Apr 06 '19 at 12:24