Mert METİN
Mert METİN

Reputation: 1288

Php - Checkbox does not work

i try to make checkboxes. When i click checkbox it makes isPremium = 1 if i click a checked checkbox it makes isPremium = 0

However: when i click a checked checkbox it does not work..

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
require 'connectDB.php';
$mysql = new mysql();
$mysql->connect();

$dbResult = mysql_query("select * from profiles");






echo "<form action='#' method='post'>";

$dbResult = mysql_query("select * from profiles");


while ($info = mysql_fetch_array($dbResult)) {

    if ($info['isPremium'] == 0)
        echo "<input type=checkbox name='check2[]' id='check2' value=" . $info['id'] . ">";
    else
        echo "<input type=checkbox name='check1[]' id='check1' value=" . $info['id'] . " checked>";

    echo $info['profileName'] . "<br />";
}

echo "<p><input type='submit' name='btnPremium' /></p>";
echo "</form>";

if (isset($_POST['btnPremium'])) {


    if (isset($_POST['check2'])) {
        $arrPremium = $_POST['check2'];
        foreach ($arrPremium as $result) {
            mysql_query("UPDATE profiles set isPremium=1 where id=" . $result . "");
        }
    }
    else 
    {
        $arrPremium = $_POST['check1'];
        foreach ($arrPremium as $result2) {
            mysql_query("UPDATE profiles set isPremium=0 where id=" . $result2 . "");
        }
    }



}
?>

when i click a checked checkbox it makes another checkbox unclick.

This is the checkbox page

enter image description here

Upvotes: 1

Views: 1408

Answers (3)

J. Bruni
J. Bruni

Reputation: 20492

I have refactored your code into this:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
require 'connectDB.php';
$mysql = new mysql();
$mysql->connect();

$update = (isset($_POST['check']) && is_array($_POST['check']));

$dbResult = mysql_query("select * from profiles");

echo "<form action='#' method='post'>";

while ($info = mysql_fetch_array($dbResult))
{
    if ($update)
    {
        $info['isPremium'] = (in_array($info['id'], $_POST['check']) ? 1 : 0);
        mysql_query("UPDATE profiles SET isPremium = " . $info['isPremium'] . " WHERE id = " . $info['id']);
    }

    echo "<input type=checkbox name='check[]' value=" . $info['id'] . ($info['isPremium'] == 0 ? "" : "checked") . " />";
    echo htmlspecialchars($info['profileName']) . "<br />";
}

echo "<p><input type='submit' name='btnPremium' /></p>";
echo "</form>";

?>

There were several problems with your original code:

  1. Several HTML input elements with the same ID. This is wrong. We can have several elements with the same name attribute, but the id attribute should be unique for each element.

  2. The database UPDATE code runs after displaying the form. This is wrong. In this case, we should update the database prior to generating the HTML output.

  3. IMPORTANT: There is no need of two different POST arrays (check1 and check2). We only need one array. The checked boxes will be posted by the browser. The unchecked boxes will not be posted by the browser. As the id is the value, we can use the in_array function to verify if the checkbox for an item was checked or not.

  4. It is a good idea to escape things you will output as HTML from the database. Otherwise, the application is vulnerable for some kinds of attack. The function htmlspecialchars is useful for this purpose.

Upvotes: 2

Feysal
Feysal

Reputation: 623

If I understand correctly what you're trying to achieve, your code is needlessly complicated. You should use isset to check whether the value of a checkbox was included in the $_POST array. If yes, the checkbox was checked.

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
require 'connectDB.php';
$mysql = new mysql();
$mysql->connect();

echo "<form action='#' method='post'>";

$dbResult = mysql_query("SELECT * FROM profiles");
$profileid = array();

while ($info = mysql_fetch_array($dbResult)) {
    echo "<input type=\"checkbox\" name=\"" . $info['id'] . "\" " . ($info['isPremium'] != 0 ? "checked " : "") . "/>";
    echo $info['profileName'] . "<br />";
    $profileid[] = $info['id'];
}

echo "<p><input type='submit' name='btnPremium' /></p>";
echo "</form>";

if (isset($_POST['btnPremium'])) {
    foreach ($profileid as $id) {
        if (isset($_POST[$id])) {
            mysql_query("UPDATE profiles SET isPremium=1 WHERE id=" . $id);
        } else {
            mysql_query("UPDATE profiles SET isPremium=0 WHERE id=" . $id);
        }
    }
}
?>

Upvotes: 1

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324620

Checkboxes typically send the value "on" to the server, regardless of what value attribute is set. If you can, try to use radio buttons instead, as these send the proper value to the server. If that's not an option, have the name of the checkbox be check1[".$info['id']." and access array_keys($_POST['check1']).

Upvotes: -1

Related Questions