CSharpMinor
CSharpMinor

Reputation: 232

Add item to array on checkbox

First, I know a lot of this is depracted in the new php, I'll update my entire site with the new commands once I get these last few things working.

What I'd like to do is add the value of my checkbox (players) to an array if the checkbox is checked.

Then once I'm on rosterverify.php, using INSERT in a mysql query, create new rows for each item on the array.

I've check on w3schools, and did some google searches, but came up empty.

<form name="addplayers" action='rosterverify.php' method="post">
    <table class="bordered">
        <thead>
            <tr>
                <th>Player Name</th>
                <th>Advanced Class</th>
                <th>Designation</th>
                <th></th>
            </tr>
        </thead>
        <?php
        $sql = "SELECT * FROM reguserstest WHERE `server`='$server' AND `guild`='$guild'";
        $result = mysql_query($sql, $con) or die('A error occured: ' . mysql_error());
        $i=1;                   
        while (($row = mysql_fetch_assoc($result))) {
            if (is_int($i/2)) {
                echo "<tr><td>" . $row['toonname'] . "</td><td>" . $row['aclass']  . "</td><td>" . $row['type'] . "</td><td><input type='checkbox' name='players' value='". $row['toonname'] . "'></td></tr>";
            } else {
                echo "<tr class='alt'><td>" . $row['toonname'] . "</td><td>" . $row['aclass']  . "</td><td>" . $row['type'] . "</td><td><input type='checkbox' name='players' value='". $row['toonname'] . "'></td></tr>";
            }
            $i++;
        }

        mysql_close($con);
        ?>                       
    </table>
    <br />
    <center><input type="submit" name="submit" value="Add Players to Team" class="ebutton" /></center>
</form>

This is rosterverify.php I haven't executed this code yet because I don't want to screw up what I've got going.

$players=$_POST['players'];
$arrlength=count($players);

for($x=0;$x<$arrlength;$x++)
{
    $sql="INSERT INTO rated_teams (players) VALUES ('$players[$x]')";

        mysql_query($sql,$con);
        if (mysql_errno()) {
            echo "MySQL error ".mysql_errno().": ".mysql_error()."\n<br>When executing:<br />\n$sql\n<br />" . $player[$x] . " was not added to the table.";
        }
}

Upvotes: 0

Views: 916

Answers (2)

user2646889
user2646889

Reputation: 1

use this code
<form name="addplayers" action='rosterverify.php' method="post">
    <table class="bordered">
        <thead>
            <tr>
                <th>Player Name</th>
                <th>Advanced Class</th>
                <th>Designation</th>
                <th></th>
            </tr>
        </thead>
        <?php
        $sql = "SELECT * FROM reguserstest WHERE `server`='$server' AND `guild`='$guild'";
        $result = mysql_query($sql, $con) or die('A error occured: ' . mysql_error());
        $i=1;                   
       while (($row = mysql_fetch_assoc($result))) {
            if (is_int($i/2)) {
                echo "<tr><td>" . $row['toonname'] . "</td><td>" . $row['aclass']  . "</td><td>" . $row['type'] . "</td><td><input type='checkbox' name='players[]' value='". $row['toonname'] . "'></td></tr>";
            } else {
                echo "<tr class='alt'><td>" . $row['toonname'] . "</td><td>" . $row['aclass']  . "</td><td>" . $row['type'] . "</td><td><input type='checkbox' name='players[]' value='". $row['toonname'] . "'></td></tr>";
            }
            $i++;
        }


        mysql_close($con);
        ?>                       
    </table>
    <br />
    <center><input type="submit" name="submit" value="Add Players to Team" class="ebutton" /></center>
</form>

only changes with name='players' replace with name='players[]'

Upvotes: 0

u_mulder
u_mulder

Reputation: 54831

Set name for checkboxes like players[]:

<input type='checkbox' name='players[]' value='some_value'>

After submit you will have $_POST['players'] which will contain selected values.

Upvotes: 5

Related Questions