【问题标题】:how to validate form in php?如何在php中验证表单?
【发布时间】:2013-05-16 14:50:48
【问题描述】:

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

我的代码:

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']; }
}

【问题讨论】:

  • 在浏览器中打开 test.php 后将其截图发布...
  • 无法发布屏幕截图。需要更多的声誉。
  • 如何在imgur中上传图片,你可以给那个链接...
  • @elavarasanlee 我将屏幕截图作为链接发布。
  • @elavarasanlee 我已经存储了 last_update 并检查了当前日期。我只想运行一次更新查询。怎么做?检查更新的代码...

标签: php forms post submit


【解决方案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>

checkresult.php:

echo $_POST["the_name"];
// Output = the_value

【讨论】:

  • 不错,想知道为什么它确实被否决了。我否决了一个被删除的关于 AJAX 的答案,也许是报复。 :)
  • 我正在显示来自数据库的问题和答案选项。用户应该回答这些问题并提交回来......
【解决方案2】:

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

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

【讨论】:

  • 不工作!我不知道提交的值是否从test.php传递到checkresult.php?当提交按钮点击什么值被传递给checkresult.php???锄头来检查?
  • var_dump($_POST); 这将打印发布到 checkresult.php 的所有数据。如果这是空的,那么什么都不会发布......!
  • array(3) { [200]=> string(1) "1" [201]=> string(1) "1" ["submit"]=> string(14) "提交答案”} 这就是我在 checkresult.php 中得到的结果
  • 这意味着 Post 值正在通过...问题出在您的查询上...!
  • 好的。这意味着问题出在 checkresluts.php 页面中......对吗?
猜你喜欢
  • 2011-07-30
  • 1970-01-01
  • 2012-08-31
  • 2017-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-13
  • 1970-01-01
相关资源
最近更新 更多