如何在php中验证表单?

如何在php中验证表单?,php,forms,post,submit,Php,Forms,Post,Submit,如何验证此表单。当用户在未选择选项的情况下提交时,他必须收到警报 我的代码: 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)) { ech

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

我的代码:

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']; }
}
<form method='post' id='submit' action='checkresult.php'>
<input type='radio' name='the_name' value='the_value' />
<input type="submit">
</form>
echo $_POST["the_name"];
// Output = the_value
echo”“;
$sql=“从cquestions中选择*,其中showdate='$today'限制2”;
$result=mysql\u查询($sql);
while($row=mysql\u fetch\u数组($result)){
回显“”$row['cqtext']。“

”; $sql2=“选择*来自cqid=”.$row['cqid']; $result2=mysql\u查询($sql2); 而($row2=mysql\u fetch\u assoc($result2)) { 回显“%$row2['aatext'];}” }
您要查找的是$\u POST变量。当您提交一个使用action='checkresult.php'的表单时,您将能够在checkresult.php上使用$\u POST命令检索变量值

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

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']; }
}
<form method='post' id='submit' action='checkresult.php'>
<input type='radio' name='the_name' value='the_value' />
<input type="submit">
</form>
echo $_POST["the_name"];
// Output = the_value

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

<?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.
?>
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.

在浏览器中打开test.php后发布其屏幕截图…无法发布屏幕截图。需要更多的声誉。在imgur中上载图像如何,您可以提供该链接…@elavarasanley我将屏幕截图作为链接发布。@elavarasanley我已存储上一次更新,并检查了当前日期。我只想运行一次更新查询。怎么做?检查更新的代码…很好的捕获,想知道为什么它会被否决。我否决了一个关于AJAX的被删除的答案,可能是报复我正在显示数据库中的问题和答案选项。用户应该回答这些问题并提交回…不工作!我不知道提交的值是否从test.php传递到checkresult.php?当submit按钮单击时,哪些值被传递到checkresult.php???如何检查?
var\u dump($\u POST)这将打印发布到checkresult.php的所有数据。如果此项为空,则不会发布任何内容。。。!数组(3){[200]=>string(1)“1”[201]=>string(1)“1”[“提交”]=>string(14)“提交答案”}这就是我在checkresult中得到的结果。php这意味着传递了Post值。。。问题在于您的查询。。。!可以这意味着问题出现在checkresluts.php页面中。。。正确的?