Jump to content

Radio buttons.


kernelgpf

Recommended Posts

I have a long list of items, and beside each item, there's a radio box under several options. Players should be able to select ONE radio box for each item, and the item ID needs to be stored in the array delMsg[]. Here's my code-

 

if($_POST['delete']){

$delMessages = $_POST['delMsg'];	
$type = $_POST['type'];
$num = count($delMessages);

for($i = 0; $i < $num; $i++){
	$messageID = $delMessages[$i];


	print "player wants to $type[$i] the item $messageID | ";
}
}

$query=mysql_query("select iname,itemid from items where ownerid='$sid' and equipped='no' and usf='no' and auction='no'")or die(mysql_error());

print "<table class=tstyle5><th class=tstyle5 width=50>Item</th><th class=tstyle5 width=50>Stock?</th><th class=tstyle5 width=50>Auction?</th><th class=tstyle5 width=50>Give Away?</th><th class=tstyle5 width=50>Donate?</th><th class=tstyle5 width=50>Discard</th><form method=post action=inventory.php?action=multiple>";

while($row=mysql_fetch_array($query)){

print<<<HERE
<tr><td><b>$row[iname]</b></td><td><INPUT type='radio' value='$row[itemid]' name='$row[itemid]'><input type=hidden name='type[]' value=stock></td>


<td><INPUT type='radio' value='$row[itemid]' name='$row[itemid]'><input type=hidden name='type[]' value=auction></td>


<td><INPUT type='radio' value='$row[itemid]' name='$row[itemid]'><input type=hidden name='type[]' value=giveaway></td>


<td><INPUT type='radio' value='$row[itemid]' name='$row[itemid]'><input type=hidden name='type[]' value=donate><</td>


<td><INPUT type='radio' value='$row[itemid]' name='$row[itemid]'><input type=hidden name='type[]' value=discard><</td></tr>
HERE;
}

print<<<HERE

<tr><td><script type="text/javascript" src="http://www.shawnolson.net/scripts/public_smo_scripts.js"></script>

<input type="radio" name="checkall" onclick="checkUncheckAll(this);"/> 

<input type="submit" name="delete" value="delete"></td></tr></table>

</form>
HERE;

 

Now- this allows me to select one option for each item, but doesn't pass anything to the array delMsg[]. Originally, I had the radio box name as delMsg[], which did pass the itemID to the array, but it only allowed me to select one radio box IN ALL, when I need to be able to select one per item.

 

Any ideas?

Link to comment
https://forums.phpfreaks.com/topic/51754-radio-buttons/
Share on other sites

You need to create a multidimensional array...look at the result of the print_r to get a good idea of what's going on.

 

<?php

if ($_POST['actions']) {
foreach ($_POST['actions'] as $key => $value) {
	print "player wants to $key the item $value<br />\n";
}
}

$query=mysql_query("select iname,itemid from items where ownerid='$sid' and equipped='no' and usf='no' and auction='no'")or die(mysql_error());

echo '
<script type="text/javascript" src="http://www.shawnolson.net/scripts/public_smo_scripts.js"></script>
<form method="post" action="inventory.php?action=multiple">
<table class="tstyle5">
	<tr>
		<th class="tstyle5" width="50">Item</th>
		<th class="tstyle5" width="50">Stock?</th>
		<th class="tstyle5" width="50">Auction?</th>
		<th class="tstyle5" width="50">Give Away?</th>
		<th class="tstyle5" width="50">Donate?</th>
		<th class="tstyle5" width="50">Discard</th>
	</tr>';

while ($row = mysql_fetch_array($query)) {
echo '
	<tr>
		<td><b>' . $row[iname] . '</b></td>
		<td><INPUT type="radio" value="actions[stock][' . $row['itemid'] . ']" name="' . $row['itemid'] . '"></td>
		<td><INPUT type="radio" value="actions[auction][' . $row['itemid'] . ']" name="' . $row['itemid'] . '"></td>
		<td><INPUT type="radio" value="actions[giveaway][' . $row['itemid'] . ']" name="' . $row['itemid'] . '"></td>
		<td><INPUT type="radio" value="actions[donate][' . $row['itemid'] . ']" name="' . $row['itemid'] . '"></td>
		<td><INPUT type="radio" value="actions[discard][' . $row['itemid'] . ']" name="' . $row['itemid'] . '"></td>
	</tr>';
}

echo '
	<tr>
		<td>
			<input type="radio" name="checkall" onclick="checkUncheckAll(this);"/> 
			<input type="submit" name="delete" value="delete">
		</td>
	</tr>
</table>
</form>';

echo '<pre>' . print_r($_POST, true) . '</pre>';
?>

Link to comment
https://forums.phpfreaks.com/topic/51754-radio-buttons/#findComment-254945
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.