0

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.";
        }
}
1
  • Can you please format your code a bit Commented Aug 2, 2013 at 17:34

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Why would you use players[]? Without the square brackets the same thing will happen. I'm just curious.
Without square brackets the last item on form named players will be posted. All other items, which are too named players will be ignored.
@u_mulder Yes thank you that worked. I edited my origional post for you to look at
0
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[]'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.