Multiple checkboxes with one name (no jquery!)

2019-06-14 13:50发布

问题:

I have a following list of checkboxes:

<input type="checkbox" name="day_of_week" value="1">Monday
<input type="checkbox" name="day_of_week" value="2">Tuesday
<input type="checkbox" name="day_of_week" value="3">Wednessday
<input type="checkbox" name="day_of_week" value="4">Thursday
<input type="checkbox" name="day_of_week" value="5">Friday
<input type="checkbox" name="day_of_week" value="6">Saturday
<input type="checkbox" name="day_of_week" value="7">Sunday

After user submits the full form, I receive it in another file:

$week_days = mysqli_real_escape_string($this->db->conn_id, $_POST['day_of_week'])

But then $week_days only contains the value of the last checked checkbox, not all of them. How can I receive all the values?

回答1:

The name should be an array.

<input type="checkbox" name="day_of_week[]" value="1">Monday
<input type="checkbox" name="day_of_week[]" value="2">Tuesday
<input type="checkbox" name="day_of_week[]" value="3">Wednessday
<input type="checkbox" name="day_of_week[]" value="4">Thursday
<input type="checkbox" name="day_of_week[]" value="5">Friday
<input type="checkbox" name="day_of_week[]" value="6">Saturday
<input type="checkbox" name="day_of_week[]" value="7">Sunday

Hope this helps.


For your second error, mysqli_real_escape_string accepts second parameter as string and you are passing array. Please check this

string mysqli_real_escape_string ( mysqli $link , string $escapestr )

Please use for loop to solve that error.