0

如何验证此表格。当用户在没有选择选项的情况下提交时,他必须得到警报。

我的代码:

echo "<form method='post' id='submit' action='checkresult.php'>";
$sql="SELECT * FROM cquestions where showdate='$today' limit 2";
$result=mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
echo "<p>" . $row['cqtext'] . "</p>";
$sql2="SELECT * FROM canswers where cqid=".$row['cqid'];
$result2=mysql_query($sql2);
while($row2=mysql_fetch_assoc($result2))
{
echo "<input type='radio' name='".$row['cqid']."' value='".$row2['cqans']."' />".$row2['aatext']; }
}
4

2 回答 2

1

您正在寻找的是 $_POST 变量。当您提交使用 action='checkresult.php' 的表单时,您将能够在 checkresult.php 上使用 $_POST 命令检索变量值。

test.php 页面(使用表单输出的内容)

<form method='post' id='submit' action='checkresult.php'>
<input type='radio' name='the_name' value='the_value' />
<input type="submit">
</form>

检查结果.php:

echo $_POST["the_name"];
// Output = the_value
于 2013-05-16T15:08:26.190 回答
0

您使用的方法是正确的,但语法错误:

<?php
$marks+=$_POST['$cqid']; //Not Correct!
//1st You haven't defined $cqid. Its $qid.
//2nd You can't use a variable inside single quotes.
//PHP will consider it as normal String. But you can use it inside double quotes.
//But remember you can't use array ($row['cqid']) inside double quotes.
?>


这是正确的方法:

<?php
while ($row = mysql_fetch_array($result)) {
    //$qid=$row['cqid'];
    //$marks+=$_POST[$qid]; //Correct!
    //But, Not needed You can directly use $row['cqid'] as an index.
    $marks+=$_POST[$row['cqid']];
}
?>


更新:[用于调试]

while ($row = mysql_fetch_array($result)) {
    $marks+=$_POST[$row['cqid']];
    echo $marks.'<br/>';
}
$insert="insert into result(email,marks)values('$email',$marks)";
$insert = mysql_query($insert);
if(!$result) {
    die('Unable to perform insert action. The following error occured: '.  mysql_error());
} else {
   echo 'The following Query: <b>'.$insert.'</b> executed successfully!';
}

还要检查$email我看不到从哪里获得该值的值。

并且$login_session = $_POST['email'];此行重复了两次,但我确信此值始终为空,因为在test.php中您已注释了以下行:

echo "<input type='hidden' name='email' value='email' />";

value 属性:value='email'显然好像不对!

检查所有这些东西,我想你现在可以从这里继续...... :) 如果没有,我仍然很乐意帮助你......

更新:[用于在查询中设置限制]

SELECT * FROM `cquestions` LIMIT 0,3;
//Will fetch first three records from cquestions.
SELECT * FROM `cquestions` LIMIT 2,3;
//Will fetch 3rd, 4th and 5th records from cquestions.
于 2013-05-18T06:52:19.773 回答