<label><input type="radio" name="rval<?php echo $qid; ?>[]" value="2" id="rval<?php echo $qid; ?>_1" style="cursor: pointer">2 </label>
我在php中添加了上面的代码。并将此值称为插入sql,如下所示。
<?php
$fqid = mysql_real_escape_string($fqid);
$qrval = isset($_POST["rval$fqid"]);
?>
但我没有从单选按钮获取任何值。 qid
&amp; fqid
是一样的。任何帮助将不胜感激。
完整代码:
<?php
$pid=$_GET["pid"];
$user=$_SESSION["user"];
$centre=$_SESSION["centre"];
$credate=date('d-m-Y');
if(isset($_POST["pf_submit"])){
$query = "insert into s_answer (qnid,pid,answer,centre) values ";
foreach($_POST["fqid"] as $i => $fqid)
{
// Get values from post.
$fqid = mysql_real_escape_string($fqid);
$anstype=$_POST["atype"];
$qfrval=isset($_POST['frval']);
$qtval=isset($_POST['tval']);
$antype=$anstype[$i];
$rval = mysql_real_escape_string($_POST["rval$fqid"][$i]);
$frval = mysql_real_escape_string($qfrval[$i]);
$tval = mysql_real_escape_string($qtval[$i]);
if($antype=='Linear'){ $ans=$rval;}
else if($antype=='Radio'){ $ans=$frval;}
else if($antype=='Text'){ $ans=$tval;}
// Add to database
$query = $query." ('$fqid','$pid','$rval','$centre') ,";
}
$query = substr($query,0,-1); //remove last char
$result = mysql_query($query);
$sug=isset($_POST["sug"]);
if($sug != ""){
mysql_query("insert into suggestion (sug,pid,centre) values ('$sug','$pid','$centre') ");
}
if($result){
echo ("<SCRIPT LANGUAGE='JavaScript'>
window.alert('Feedback has been submitted successfully. Thank you for your valuable feedback!')
window.location.href='home.php'
</SCRIPT>");
}
else
{
$error=mysql_error();
echo "<script type='text/javascript'>alert(\"$error\");</script>";
}
}
?>
&#13;
表格代码:
<form class="form-horizontal bucket-form" action="#" method="post" enctype="multipart/form-data">
<?php
$qres=mysql_query("select * from s_question order by qid asc");
$i=0;
while($qrow=mysql_fetch_array($qres))
{
$i++;
$qid=$qrow["qid"];
$atype=$qrow["anstype"];
?>
<div style="display: none"><input name="fqid[]" value="<?php echo $qid; ?>" ></div>
<div style="display: none"><input name="atype[]" value="<?php echo $atype; ?>" ></div>
<?php
if($atype=="Linear")
{
$ansres=mysql_query("select * from q_answer where qid='$qid' and anstype='Linear'");
$ansrow=mysql_fetch_array($ansres);
?>
<div class="form-group">
<label><?php echo $qrow["qn"]; ?></label>
<p>
<label><?php echo $ansrow["l1"]; ?> 
<input type="radio" name="rval<?php echo $qid ?>[]" value="1" id="rval<?php echo $qid; ?>_0" style="cursor: pointer">
1 </label>
<label>
<input type="radio" name="rval<?php echo $qid ?>[]" value="2" id="rval<?php echo $qid; ?>_1" style="cursor: pointer">
2 rval<?php echo $qid; ?></label>
<label>
<input type="radio" name="rval<?php echo $qid ?>[]" value="3" id="rval<?php echo $qid; ?>_2" style="cursor: pointer">
3 </label>
<label>
<input type="radio" name="rval<?php echo $qid ?>[]" value="4" id="rval<?php echo $qid; ?>_3" style="cursor: pointer">
4 </label>
<label>
<input type="radio" name="rval<?php echo $qid ?>[]" value="5" id="rval<?php echo $qid; ?>_4" style="cursor: pointer">
5 </label>
<label>
<input type="radio" name="rval<?php echo $qid ?>[]" value="6" id="rval<?php echo $qid; ?>_5" style="cursor: pointer">
6 </label>
<label>
<input type="radio" name="rval<?php echo $qid ?>[]" value="7" id="rval<?php echo $qid; ?>_6" style="cursor: pointer">
7 </label>
<label>
<input type="radio" name="rval<?php echo $qid ?>[]" value="8" id="rval<?php echo $qid; ?>_7" style="cursor: pointer">
8 </label>
<label>
<input type="radio" name="rval<?php echo $qid ?>[]" value="9" id="rval<?php echo $qid; ?>_8" style="cursor: pointer">
9 <?php echo $ansrow["l2"]; ?></label>
</p>
</div>
<?php } } ?>
<div class="form-group">
<button type="submit" name="pf_submit" class="btn btn-danger">Next</button>
</div>
</form>
&#13;
var_dump结果: 数组([fqid] =&gt;数组([0] =&gt; 1 [1] =&gt; 3)[atype] =&gt;数组([0] =&gt;线性[1] =&gt;线性)[rval1 ] =&gt;数组([0] =&gt; 2)[rval3] =&gt;数组([0] =&gt; 3)[sug] =&gt; [pf_submit] =&gt;)