plugins - wordpress select multiple options and illegal string offset 'timeslot'

admin2025-06-02  1

Closed. This question needs details or clarity. It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 6 years ago.

Improve this question
<form method="POST">
<select name="timeslot[]" multiple="multiple" size = 4 required>
    <option value="1">1</option>
    <option value="1">1</option>
    <option value="1">1</option>
</select>
<input type="submit" name="submit"/>
</form>

<?php
if(isset($_POST['submit'])){
    global $wpdb;
    $booking_timeslots = $wpdb->prefix . 'booking_timeslots';   
    $timeslots = $_POST['timeslot'];
       foreach ($timeslots as $time) {
          echo $dd = $time['timeslot'];
       }
}
?>

This is my full code with query.

<?php
if(isset($_POST['submit'])){
    global $wpdb;
    $booking_dates = $wpdb->prefix . 'booking_dates';   //'booking_dates' is a table name which is in the database
    $booking_timeslots = $wpdb->prefix . 'booking_timeslots';   //'booking_timeslots' is a table name which is in the database

    $year = $_POST['year'];
    $month = $_POST['month'];
    $days = $_POST['days'];
    $timeslots = $_POST['timeslot'];

        $data['year'] = $year;      // $data['year'] year is a name of column in database
        $data['month'] = $month;
    foreach ($days as $day) {           // $data['month'] month is a name of column in database
        $data['day'] = $day;            // $data['day'] day is a name of column in database

        $wpdb->insert($booking_dates,$data);
        $last_record_id = $wpdb->insert_id;

        foreach ($timeslots as $timeslot) {
            $data_timeslot['bid'] = $last_record_id;
            $data_timeslot['time'] = $timeslot;
            $wpdb->insert($booking_timeslots,$data_timeslot);
        }
    }
}
?>

While above code executes wordpress gives following error.

Illegal string offset 'timeslot' 

Can someone help to resolve this.

Closed. This question needs details or clarity. It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 6 years ago.

Improve this question
<form method="POST">
<select name="timeslot[]" multiple="multiple" size = 4 required>
    <option value="1">1</option>
    <option value="1">1</option>
    <option value="1">1</option>
</select>
<input type="submit" name="submit"/>
</form>

<?php
if(isset($_POST['submit'])){
    global $wpdb;
    $booking_timeslots = $wpdb->prefix . 'booking_timeslots';   
    $timeslots = $_POST['timeslot'];
       foreach ($timeslots as $time) {
          echo $dd = $time['timeslot'];
       }
}
?>

This is my full code with query.

<?php
if(isset($_POST['submit'])){
    global $wpdb;
    $booking_dates = $wpdb->prefix . 'booking_dates';   //'booking_dates' is a table name which is in the database
    $booking_timeslots = $wpdb->prefix . 'booking_timeslots';   //'booking_timeslots' is a table name which is in the database

    $year = $_POST['year'];
    $month = $_POST['month'];
    $days = $_POST['days'];
    $timeslots = $_POST['timeslot'];

        $data['year'] = $year;      // $data['year'] year is a name of column in database
        $data['month'] = $month;
    foreach ($days as $day) {           // $data['month'] month is a name of column in database
        $data['day'] = $day;            // $data['day'] day is a name of column in database

        $wpdb->insert($booking_dates,$data);
        $last_record_id = $wpdb->insert_id;

        foreach ($timeslots as $timeslot) {
            $data_timeslot['bid'] = $last_record_id;
            $data_timeslot['time'] = $timeslot;
            $wpdb->insert($booking_timeslots,$data_timeslot);
        }
    }
}
?>

While above code executes wordpress gives following error.

Illegal string offset 'timeslot' 

Can someone help to resolve this.

Share Improve this question edited Mar 5, 2019 at 8:35 Afzal Khan asked Mar 4, 2019 at 11:39 Afzal KhanAfzal Khan 54 bronze badges 3
  • please Elaborate your question. – Gufran Hasan Commented Mar 4, 2019 at 11:40
  • Please change name="timeslot[]" to name="timeslot". – Gufran Hasan Commented Mar 4, 2019 at 11:43
  • Please check by change your select box options values. – Pratik Patel Commented Mar 4, 2019 at 11:49
Add a comment  | 

1 Answer 1

Reset to default 1

You are getting illegal string offset 'timeslot' because $time is not an array, it is an item in array $timeslots. You have already retrieved the values submitted using $timeslots = $_POST['timeslot'];

Your code also contains other typing mistakes. Here is corrected code:

<form method="POST">
   <select name="timeslot[]" multiple="multiple" size = 4 required>
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
   </select>
   <input type="submit" name="submit"/>
</form>

<?php
if(isset($_POST['submit'])){
    global $wpdb;
    $booking_timeslots = $wpdb->prefix . 'booking_timeslots';   
    $timeslots = $_POST['timeslot'];
    // you can use print_r() here 
    print_r( $timeslots );  // to see what is submitted from form

    foreach ($timeslots as $time) {
          echo $time;
    }
}
?>

I hope this helps.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748841804a314173.html

最新回复(0)