Send several checkbox values to the database

Asked

Viewed 179 times

1

I have a logic problem, actually and I can’t evolve what I need to do with a code. Follows description of the problem:

Problem:

I have a list of products and need to inform on a screen if the product is on Promotion or not. That’s what I chose to do checkbox where the value 1 is Allow and value 0 is Not Allow (unchecked). In the database, in the table Products I have a column called isPromotion who gets those zero or one. The problem is that I need to reference in which product that value will be assigned, so I put together each product, a input of the kind hidden containing the value of the id of each product, something common when assembling these screens.

<input type="hidden" name="idProduto[]" value="<?= $Produto->id; ?>">

Now, suppose I want to mark 5 products as Promotion, i will mark the checkbox of each product and with it add to an array of data. So each product has a input of the kind checkbox whose name is declared as an array, see below:

<input class="form-check-input" name="promotion[]" type="checkbox" <?= $Produto->isPromotion != 1 ? 'checked' : ''; ?> value="<?= $Produto->isPromotion ; ?>">

Now we have 2 array: one with the Promotion values and the other with the corresponding product IDS.

Here comes my doubts and problems with logic: The IDS array sends all product IDS because it is already set in input, so it is obvious that the array will return all IDS, and the Promotion Array of values returns True or False.

How can I send these values (Allow / Not allow) to the database, EACH WHICH IN YOUR DATABASE RECORD? I don’t have much code, and I’ve tried a few tricks to make it work.

I accept any idea, and please avoid posting comments on other questions. I know how to select various checkbox values and send them to the database. My problem is how I make so that each product that has its own checkbox and its value is sent to the Database in its respective record.

I do not know if I was very clear, but I am available if you need more information;

Thank you to all who can help! Thank you :)_

  • Take a look at the property Htmlelement.dataset. This property allows you to link arbitrary data to HTML elements. It can be used to link pids, prices, quantities, promotions,...

1 answer

1


Dude, I don’t know if I really understood if this is exactly what you want, but I quickly made a code here to go through the amount of input that you will have created in your Form and identify the values of each one, thus throwing them all to your Bank in separate way " " by lines. It’ll look something like this:

$id_pedido = $connect->insert_id;
$qtd_input = sizeof($_POST['idProduto']);

if(!empty($_POST['idProduto']) || !empty($_POST['promotion'])){
    for($i = 0 ; $i < $qtd_input; $i++) {
        $checkbox = isset($_POST['promotion']);

        $id = $_POST['idProduto'][$i];
        $promo = $checkbox[$i];

        $query = "INSERT INTO table VALUES ('{$id_pedido}', '{$id}', '{$promo}')";

        if(!$connect->query($query) === true)
            die("Erro na inserção dos dados <br>" . $connect->error);
    }
}

With the function sizeof you can check the size of an element or object it is looking for, which in your case is an input. According to the amount of inputs created with the name idProduto it will go through the value of each with the name of idProduto[] and promotion[], taking their values and playing separately for the Bank ( this is the function that that [$i] is running in the code.

NOTE: I don’t know if you will see $_GET or $_POST, anything just change.

Edit: As I said in the comment, it would be interesting for you to have this table in your Bank with a Foreign Key coming from another table, such as "Request", where it pulls the Request ID as a Primary Key, with this, when the code runs in all idProduto[] it will play each ID on a line in the Bank, but always reference from which Order this belongs, that serious function of the $id_pedido in the code. And as to which checkbox is ticking or not, you can check this with the isset(), If he’s marked, he sends the amount to the bank, if not, he’s not in charge. In this case, you can even make a modification in the check, and if it is not marked you arrow the value of this as 0, so you will know which were marked or not.

  • So my brother, thanks for the code, I didn’t get to test it but I will, but looking over it, not if I did something similar. In addition, each product will have its own ID set in the input hidden, then, in this code there will run 21x (amount of products on the screen I have and each one with its ID) however, by default, checkbox not marked are not sent by POST/GET and then it will only send the marked and those not sent, but are called in PHP will give error "Undefined Offset" and since $promo is an array, it will not assign a value 0 or '0' not come front value, understand?

  • This has been one of the problems faced by

  • Dude, to filter only the checkbox marked you can also do a check to send the values. And as for the amount of Ids, this is how it has to run, the interesting thing is that you have in your Bank a kind of primary ID where these Product Ids are linked as a kind of Foreign Key, as for example, an Order ID. I’ll edit the code with the checkbox check.

  • Bro, I’m gonna try your tips, and I’ll get back to you on how it went...

  • Brother, I’m going to set your answer as correct because in the back-end part this code actually works, but I re-structured the front, from Checkbox to Select with Options. Vlww ai by attention and help^^

Browser other questions tagged

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