Send Selected Checkbox

Asked

Viewed 362 times

0

I looked at some questions here on the site, but could not understand well. I have several checkboxes like this:

<td>
    <label class="ls-label-text">
        <input type="checkbox" name="adicional" value="L. Ninho" id="2.00">
        L. Ninho
    </label>
</td>
<td>
    <label class="ls-label-text">
        <input type="checkbox" name="adicional" value="Kit Kat" id="2.50">
        Kit Kat
    </label>
</td>
<td>
    <label class="ls-label-text">
        <input type="checkbox" name="adicional" value="Confete" id="2.00">
        Confete
    </label>
</td>

Inside a form, and I send this data to the database, only when I send it, it only sends one checkbox, it does not send all selected.

My code of action that’s the one:

<?php
include "../../lib/inc_con.php";
session_start();
$mesa = $_POST['mesa'];
$tamanho = $_POST['tamanho'];
$quantidade = $_POST['qtd'];
$adicional = $_POST['adicional'];
$hiddentotal = $_POST['hiddentotal'];
$data = date('Y-m-d H:i:s');
$produto_id1 = $_POST['produto_id1'];
$atendente_id = $_SESSION['id'];

$sql = mysql_query("INSERT INTO pedidos (mesa, tamanho, qtd, adicional, hiddentotal, data, produto_id1, atendente_id) values ('$mesa', '$tamanho', '$quantidade', '$adicional', '$hiddentotal', '$data', '$produto_id1', '$atendente_id')") or die (mysql_error());

?>
  • Hi Alfredo, you commented on Action but forgot to post it. Could you put as you are recovering it, please? By the way, the browser submits all checkboxes as the same name in one, but separated as if they were an array. Post Action and I’ll show you how to get it right.

  • 2

    Look at this reply or that other

  • If you can have more than one adicional please do not record in a column the values separated by comma, nothing implode/explode. Have you thought about how you’re gonna check out this fruit salad?

  • 1

    Read this: http://answall.com/questions/579/por-que-n%C3%A3o-devo-usar-fun%C3%A7%C3%B5es-do-tipo-mysql

  • We are not a forum :) http://answall.com/posts/95582/revisions

1 answer

2


Change the attribute name from your checkbox to:

<input type="checkbox" name="adicional[]" value="Confete" id="2.00">

So PHP will receive an array of objects, when receiving normal:

$_POST['additional']

Give a var_dump to understand how these values are received.

I hope it helps, hugs

  • Gave this error Notice: Array to string Conversion in C: xamp Projects Moclient 2.0 module send products.php on line 14 , and here var_dump array(3) { [0]=> string(8) "L. Ninho" [1]=> string(6) "Pacoca" [2]=> string(13) "L. Condensate" } , and on line 14 there is this code $sql = mysql_query("INSERT INTO requests (table, size, Qtd, additional, hiddentotal, date, product_id1, attendance_id) values

  • 2

    It is a list of items, when saving need to make a loop and write one by one, according to how is your database

Browser other questions tagged

You are not signed in. Login or sign up in order to post.