PDA

View Full Version : Add Checkboxes to Form


Bestrafung
07-23-2014, 05:21 PM
I am trying to add checkboxes to the free agent signup form in the vbTournaments mod. I have created the extra field in the database table and a single selection works without issue. The problem I have is that when multiple checkboxes are selected only the last selection is saved to the database instead of all of the choices.

tmnt_newfreeagent template:
<form class="vbform block" action="teams.php?do=addfreeagent" method="post">
<div class="blockbody formcontrols">
<div class="blockrow">
<label for="game">Game</label>
<div style="overflow:auto">
<input type="checkbox" name="game[]" value="Aliens vs. Predator">Aliens vs. Predator<br />
<input type="checkbox" name="game[]" value="Chivalry: Medeival Warfare">Chivalry: Medeival Warfare<br />
<input type="checkbox" name="game[]" value="Counter-Strike: Global Offensive">Counter-Strike: Global Offensive<br />
<input type="checkbox" name="game[]" value="Insurgency">Insurgency<br />
</div>
<p class="description">Please select the game(s) you play.</p>
</div>
</div>
<div class="blockfoot actionbuttons">
<div class="group">
<input type="submit" class="button" value="{vb:rawphrase submit}" />
</div>
</div>
</form>

teams.php:
if ($_POST['do'] == 'insertfreeagent')
{
if (!$tmntp['canjointeam'])
{
print_no_permission();
}
$count = $vbulletin->db->query_first("
SELECT COUNT(*) AS countrows
FROM " . TABLE_PREFIX . "tmnt_members
WHERE userid = '".$vbulletin->userinfo['userid']."'
");
if ($count['countrows'] != 0)
{
print_no_permission();
}
$userid = $vbulletin->userinfo['userid'];
$available_day = $vbulletin->input->clean_gpc('p', 'available_day', TYPE_NOHTML);
$available_time = $vbulletin->input->clean_gpc('p', 'available_time', TYPE_NOHTML);
$reason = $vbulletin->input->clean_gpc('p', 'reason', TYPE_NOHTML);
$description = $vbulletin->input->clean_gpc('p', 'description', TYPE_NOHTML);
$game = $vbulletin->input->clean_gpc('p', 'game', TYPE_STR);
if (!$description || !$available_day || !$available_time || !$reason)
{
$_REQUEST['do'] = 'addfreeagent';
$incomplete_fields = true;
}
else
{
$ip = $_SERVER['REMOTE_ADDR'];
$vbulletin->db->query_write("INSERT INTO " . TABLE_PREFIX . "tmnt_members
(userid, teamid, time, ip, available_day, available_time, reason, description, game)
VALUES (
'".$vbulletin->db->escape_string($userid)."',
'0',
".TIMENOW.",
'".$vbulletin->db->escape_string($ip)."',
'".$vbulletin->db->escape_string($available_day)."',
'".$vbulletin->db->escape_string($available_time)."',
'".$vbulletin->db->escape_string($reason)."',
'".$vbulletin->db->escape_string($description)."',
'".$vbulletin->db->escape_string($game)."'
)
");
$vbulletin->url = "teams.php?do=freeagents";
eval(print_standard_redirect('redirect_insertfreea gent'));
}
}


I've tried both $vbulletin->input->clean_gpc('p', 'game', TYPE_STR) and $vbulletin->input->clean_gpc('p', 'game', TYPE_NOHTML) with no differences. I really don't understand the rest of this to proceed any further. If anyone could help me determine the issue I would greatly appreciate it.

vBNinja
07-23-2014, 05:39 PM
Try TYPE_ARRAY

Also serialize $game before saving it to the db

Dave
07-23-2014, 05:41 PM
It's because you're sending an array of values to the server.

$vbulletin->input->clean_array_gpc('p', 'game', TYPE_STR);

$vbulletin->GPC['game']; should then contain the games separated by commas.

Bestrafung
07-23-2014, 09:46 PM
Try TYPE_ARRAY

Also serialize $game before saving it to the db
I tried TYPE_ARRAY shortly after posting earlier and just get an error message on submit, no data is sent for that field. Could you please explain or point me in the right direction to "serialize $game" since I'm not familiar with this. I'm a novice when it comes to PHP and my vb experience is even more limited. I'm just trying to teach myself the basics by checking out other mods so any info is useful.
It's because you're sending an array of values to the server.

$vbulletin->input->clean_array_gpc('p', 'game', TYPE_STR);

$vbulletin->GPC['game']; should then contain the games separated by commas.
Yeah, I thought as much but have been unable to determine how to submit the array.

Thank you both for the replies.

Bestrafung
07-25-2014, 07:13 PM
Does anyone have any ideas how to resolve this I've spent a lot of time on this script and fixed everything but this issue. I added regular text fields without issue, I never thought a simple checkbox selection would cause this much trouble. I'm sure the form HTML itself is fine it's just the PHP code handling the submission I'm not sure about.

I tried looking into the serialization of $game but it's over my head.