Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在HTML表单中输入文本后,如何使按钮出现?_Php_Html_Forms_Validation - Fatal编程技术网

Php 在HTML表单中输入文本后,如何使按钮出现?

Php 在HTML表单中输入文本后,如何使按钮出现?,php,html,forms,validation,Php,Html,Forms,Validation,我正在编写一个HTML表单,在这里我实现了一个反垃圾邮件系统,其中生成了两个随机数,并要求用户在文本字段中输入总和。如果答案正确,则会出现“提交”按钮,用户可以继续。如果答案不正确,则会发出通知,说明“答案不正确”。HTML表单本身在一个表中。在最后一行单元格中,我输入了以下代码: <td> <?php $firstnumber = rand(0, 5); $secondnumber = rand(0,6); echo 'Anti-spam: '.$firstnumber.'

我正在编写一个HTML表单,在这里我实现了一个反垃圾邮件系统,其中生成了两个随机数,并要求用户在文本字段中输入总和。如果答案正确,则会出现“提交”按钮,用户可以继续。如果答案不正确,则会发出通知,说明“答案不正确”。HTML表单本身在一个表中。在最后一行单元格中,我输入了以下代码:

<td>
<?php 
$firstnumber = rand(0, 5);
$secondnumber = rand(0,6);
echo 'Anti-spam: '.$firstnumber.' + '.$secondnumber.' = ?</td><td><input name="human" placeholder = "Do the math">'; 
?>
</td>
<tr>
<td colspan="2">
By submitting this form you are agreeing to the <a href="http://http://j2partners.net/index.php?site=tos" target="_blank">terms of service and agreements.</a><br><br>
<?php 
$answer = $_POST['human'];
if($answer == $firstnumber + $secondnumber) {
echo '<input id="submit" name="submit" type="submit" value="I Agree"> <input id="reset" name="reset" type="reset" value="Reset">'; } 
else {
echo '<font color=#ea596c>Incorrect answer</font>';
?>
</td>

但是当答案输入框中时,“提交”按钮不会再次出现:

您在每次页面加载时都会生成新的随机数。因此,当用户进行计算并提交表单时,会发生一个新的请求,您的随机数会生成新的,因此计算不能随机匹配,可能是相同的结果

提交表单后,您必须将数学结果存储在会话中,或存储在编码的隐藏输入字段或其他您知道结果的内容中


或者,您想在JavaScript中检查结果以显示按钮,但我不会对客户端进行人性化检查。

您在每次加载页面时都会生成新的随机数。因此,当用户进行计算并提交表单时,会发生一个新的请求,您的随机数会生成新的,因此计算不能随机匹配,可能是相同的结果

提交表单后,您必须将数学结果存储在会话中,或存储在编码的隐藏输入字段或其他您知道结果的内容中


或者您想用JavaScript检查结果以显示按钮,但我不会对客户端进行人性化检查。

您需要在表单中添加一些JavaScript来实现这一点。 在输入时添加按键事件,并检查输入是否有效

<input id="antispam" name="human" onkeyup="checkEntry" placeholder = "Do the math">'; 
?>

<script>
function checkEntry()
{
var x=document.getElementById("antispam");
if (x=={correct answare})
  {
    [add submite button]
  }
else
{
   [add incorrect text]
}
</script>

您需要在表单中添加一些JavaScript来实现这一点。 在输入时添加按键事件,并检查输入是否有效

<input id="antispam" name="human" onkeyup="checkEntry" placeholder = "Do the math">'; 
?>

<script>
function checkEntry()
{
var x=document.getElementById("antispam");
if (x=={correct answare})
  {
    [add submite button]
  }
else
{
   [add incorrect text]
}
</script>
选项1不太安全添加正确答案的隐藏输入

<input type="hidden" name="answer" value="<?=$firstnumber+$secondnumber;?"/>
选项2:在会话中记住正确答案。如果要执行服务器端检查,必须显示“提交”按钮-数据必须发送到服务器。要显示/隐藏提交按钮,必须执行客户端检查并使用javascript,请参见选项3

<?
  session_start(); // necessary to use session vars
  $firstnumber = rand(0, 5);
  $secondnumber = rand(0, 6);
  if(!empty($_SESSION["answer"]) && $_SESSION["answer"]==@$_POST["human"]) {
    // the math was ok
  }
  $_SESSION["answer"] = $firstnumber + $secondnumber; // must be AFTER the check
?>
<form method="post">
  <?="$firstnumber + $secondnumber = ";?>
  <input name="human" type="text" placeholder="do the math"/>
  <input type="submit"/> <!-- can't be hidden without javascript -->
</form>
选项3客户端javascript解决方案,类似vasiljevski的建议

<span id="first"></span> + <span id="first"></span>
<input oninput="check(this)" placeholder="do the math"/>
<input type="submit" id="submit" style="display: none"/>

<script>
  var first=Math.floor(Math.random()*10);
  var second=Math.floor(Math.random()*10);
  var gid = document.getElementById.bind(document);
  gid("first").innerHTML = first;
  gid("second").innerHTML = second;
  function check(input) {
    gid("submit").style.display = input.value==first+second ? "inline" : "none";
  }
</script>
选项1不太安全添加正确答案的隐藏输入

<input type="hidden" name="answer" value="<?=$firstnumber+$secondnumber;?"/>
选项2:在会话中记住正确答案。如果要执行服务器端检查,必须显示“提交”按钮-数据必须发送到服务器。要显示/隐藏提交按钮,必须执行客户端检查并使用javascript,请参见选项3

<?
  session_start(); // necessary to use session vars
  $firstnumber = rand(0, 5);
  $secondnumber = rand(0, 6);
  if(!empty($_SESSION["answer"]) && $_SESSION["answer"]==@$_POST["human"]) {
    // the math was ok
  }
  $_SESSION["answer"] = $firstnumber + $secondnumber; // must be AFTER the check
?>
<form method="post">
  <?="$firstnumber + $secondnumber = ";?>
  <input name="human" type="text" placeholder="do the math"/>
  <input type="submit"/> <!-- can't be hidden without javascript -->
</form>
选项3客户端javascript解决方案,类似vasiljevski的建议

<span id="first"></span> + <span id="first"></span>
<input oninput="check(this)" placeholder="do the math"/>
<input type="submit" id="submit" style="display: none"/>

<script>
  var first=Math.floor(Math.random()*10);
  var second=Math.floor(Math.random()*10);
  var gid = document.getElementById.bind(document);
  gid("first").innerHTML = first;
  gid("second").innerHTML = second;
  function check(input) {
    gid("submit").style.display = input.value==first+second ? "inline" : "none";
  }
</script>

首先回显$answer并求和$firstnumber+$secondnumber。检查两者是否匹配,然后尝试此$answer==$firstnumber+$secondnumbery您需要在填充$\u POST之前发布具有该值的表单。否则您将需要Javascript。首先回显$answer并求和$firstnumber+$secondnumber。检查两者是否匹配,然后尝试此$answer==$firstnumber+$secondnumbery您需要在填充$\u POST之前发布具有该值的表单。否则您将需要Javascript。我使用了选项2$_会话[answer]工作正常,但仍然无法识别用户输入的答案:我使用了选项2$_会话[答案]工作正常,但仍无法识别用户输入的答案: