我正在尝试使用这个标签中的php保存我的动态生成的复选框,标签和选择标签的值到mysql数据库我正在从不同的数据库中检索值,我正在尝试使用标签值保存已检查字段的数据并选中选项价值,但我无法做到,请建议我在哪里做错了。
<script language="javascript">
function getmatchplayer1(mid)
{
window.location="?mid="+mid;
}
</script>
</head>
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
// select database on server
mysql_select_db("$db_name")or die("cannot select DB");
if(isset($_POST['SaveTeam'])){
$tmnmA = $_POST['TeamNameA'];
$tmnmB = $_POST['TeamNameB'];
$mch_id = $_POST['mtch_id'];
$chk_id = $_POST['$chkid'];
$cric_nm = $_POST['$ply_nm'];
$chk_rnk = $_POST['$chkrnk'];
$sqlbat = "INSERT INTO board(cricketer_id, status,Runs,Sixes,Fifty,Hundred,Total_Score,Total_Points)VALUES('$cric_nm', 'NP','0','0','0','0','0','0')";
$retval = mysql_query( $sqlbat );
if(! $retval )
{
die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
}
$sql=mysql_query( "SELECT * FROM tbl_info LIMIT 5");
$cric_id = $resultmp['player_id'];
?>
<body style="align:center">
<form method="post" id="form1" name="form1" action="">
<div style="width:850px; margin-left:55px; text-align:right">
<input type="submit" id="SaveTeam" name="SaveTeam" value=" Save " />
</div>
<table align="center" width="900" height="auto" style="border-top:1px solid #CCCCCC;border-right:1px solid #CCCCCC; border-left:1px solid #CCCCCC; cellspacing:0px; cellpadding:0px;">
<tr style="border:1px solid #CCCCCC">
<td width="450" style="border:1px solid #CCCCCC"> Today's Match:-
<select name="mtch_id" id="mtch_id" onchange="getmatchplayer1(this.value)">
<option> Select Team </option>
<?php
$i=1;
$sqlm=mysql_query("SELECT * FROM t_match LIMIT 5");
while ($resultm=mysql_fetch_array($sqlm))
{
$mtch=$resultm['match_no'];
echo "<option id='".$i."' value='".$mtch."'>";
echo $mtch;
echo "</option>";
$i++;}
?>
</select></td>
<td width="450" style="border:1px solid #CCCCCC">
<?php
$j=1;
if (isset($_REQUEST['mid']))
{
$mid=$_REQUEST['mid'];
}
$sqlm1=mysql_query("SELECT * FROM t_match WHERE match_no = '$mid'");
while ($resultm1=mysql_fetch_array($sqlm1)){
$mtch=$resultm1['match_no'];
$mtch1=$resultm1['team1'];
$mtch2=$resultm1['team2'];
$chkid = 'a'.$a;
?>
</td>
</tr>
<tr style="border:1px solid #CCCCCC">
<td style="text-align:center; border:1px solid #CCCCCC">
<?php
echo "Team - ".$mtch1;
?>
</td>
<td style="text-align:center; border:1px solid #CCCCCC">
<?php
echo "Team - ".$mtch2;
}
?>
</td>
</tr>
<tr>
<td height="auto" colspan="2">
<div id="update" style="width:900px; height:24px;">
<div style="float:left; width:450px; height:24px;">
<?php
$a=1;
$b=2;
$k=1;
$sqlmp=mysql_query("SELECT * FROM tbl_info WHERE player_team = '$mtch1' LIMIT 5");
while ($resultmp=mysql_fetch_array($sqlmp)){
echo "<SCRIPT LANGUAGE='JavaScript'>
//on checked disabled enabled condition
function codenameA".$a."()
{
if (document.form1.a".$a.".checked)
{
document.form1.b".$a.".disabled = false;
}
else
{
document.form1.b".$a.".disabled = true;
}
}
/*Condition for limit the checked up to 11 for team A*/
//initial checkCount of zero
var checkCount = 0
//maximum number of allowed checked boxes
var maxChecks = 11
function setChecksA".$a."(obj)
{
//increment/decrement checkCount
if (obj.checked)
{
checkCount=checkCount+1
}
else
{
checkCount=checkCount-1
}
//if they checked a 11th box, uncheck the box, then decrement checkcount and pop alert
if (checkCount>maxChecks)
{
obj.checked=false
document.form1.b".$a.".disabled=true;
checkCount=checkCount-1
alert('you may only choose up to '+maxChecks+' options')
}
}
//-->
</script>";
?>
<div style="float:left; width:80%; height:24px; border:1px solid #CCCCCC">
<input onclick='codenameA<?php echo $a; ?>(), setChecksA<?php echo $a; ?>(this)' type='checkbox' id='a<?php echo $a; ?>' name='a<?php echo $a; ?>' value='<?php echo $resulttmp['player_id']; ?>' /><input type="hidden" id="lt<?php $a; ?>" name="lt<?php $a; ?>" value="<?php echo $resulttmp['player_name']; ?>" /> <?php echo $resultmp ['player_name']; ?>
</div>
<div style="float:right; width:19%; height:24px; border:1px solid #CCCCCC">
<select disabled='disabled' id='b<?php echo $a; ?>' name='b<?php echo $a; ?>' style='width:100%;'>
<option>
- -
</option>
<option value='1'>
1
</option>
<option value ='2'>
2
</option>
<option value='3'>
3
</option>
<option value='4'>
4
</option>
<option value='5'>
5
</option>
<option value='6'>
6
</option>
<option value='7'>
7
</option>
<option value='8'>
8
</option>
<option value='9'>
9
</option>
<option value='10'>
10
</option>
<option value='11'>
11
</option>
</select>
</div>
<?php
$a++;
$b++;
}
?>
</div>
</td>
</tr>
</table>
</form>
答案 0 :(得分:1)
如果我理解正确,您将使用$ a增量变量以dinamically方式生成输入的名称。但由于某些原因,您在发布时无法重新计算这些名称,因此您无法获得发布的值。您可以通过多种方式解决此问题,一种方法是以这种方式生成输入名称:
<input type="checkbox" name="blah[$a]"/>
<select name="bleh[$a]">...</select>
这将产生一个包含如下数组的帖子:
array(
"blah" => array($a => valueofcheckbox),
"bleh" => array($a => valueofselect)
)
你可以用简单的foreach解析
foreach ($_POST["blah"] as $a => $value) {
...
}
foreach ($_POST["bleh"] as $a => $value) {
...
}
我不太明白启用/禁用逻辑,正如我之前所说,这是一些混乱的代码,但你应该知道禁用的字段不会被发布。
PS:我发布的代码是伪代码。