【问题标题】:no response from a form表格没有回复
【发布时间】:2018-08-01 09:11:45
【问题描述】:

我正在为我的项目开发令牌重定向系统。 这就是我想要做的: 用户插入令牌 => 网站使用此令牌搜索条目,当他找到它时,他使用测试名称 => 重定向到测试页面。

但是我有一个问题,当我按下按钮时我没有任何反应。

这是我的代码:

if (isset($_POST['redibtn'])) {redit();}

function redit(){ if (isset($_POST['redibtn'])) {    
    global $db, $token, $errors;

    // grap form values
    $token = e($_POST['token']);

    // attempt login if no errors on form
    if (count($errors) == 0) {

      $query = "SELECT * FROM tokens WHERE Token='$token' LIMIT 1";
      $results = mysqli_query($db, $query);

      if (mysqli_num_rows($results) == 1) { // user found
        // check if user is admin or user
        $choix = mysqli_fetch_assoc($results);
        if ($choix['test'] == 'Integration') {

          header('location: integrationffg/test.php');     
        }else{

          header('location: aaa.php');
        }
      }else {     
      }
    }   
  }   
}


<form  >
  <center>
<p>Passer un exam :</p>
  </center>

<div class="input-group">
  <span class="input-group-addon" id="basic-addon1">Token</span>
  <input required type="text" name="token" class="form-control" placeholder="Ex. b2f56acc7c212ee5af432e9794ccdbef" >
</div>

<br>
<center>
<button type="button" class="btn btn-primary" name="redibtn">Zuu</button>
</center>

</form>

我的数据库表:

有人可以帮帮我吗?

提前致谢。

【问题讨论】:

  • 按钮式按钮不发送表单,需要按钮式提交))) &lt;button type="submit" class="btn btn-primary" name="redibtn"&gt;Zuu&lt;/button&gt;
  • 提交表单时使用的默认方法是get。将method="post"添加到&lt;form&gt;属性中应该可以了。
  • @Heyne 没用,虽然他没有提交按钮
  • 整件事都不行。
  • ... 并首先打开它。看?同样的事情。

标签: php forms redirect token


【解决方案1】:

我在下面添加了您的脚本的工作版本,并对其进行了一些修改。

从第一个 if:开始

更进一步,我添加了对count($errors) 检查的严格比较,以真正确保这是0 而不是别的。

在开始的表单标记中,我还添加了method 属性,以确保它作为 POST 而不是作为 GET 请求发送,并且第一个如果确实有效。

<?php

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    redit();
}

function redit()
{
    global $db, $token, $errors;

    // grap form values
    $token = e($_POST['token']);

    // attempt login if no errors on form
    if (count($errors) === 0) {
        $query = "SELECT * FROM tokens WHERE Token='$token' LIMIT 1";
        $results = mysqli_query($db, $query);

        if (mysqli_num_rows($results) == 1) { // user found
            // check if user is admin or user
            $choix = mysqli_fetch_assoc($results);
            if ($choix['test'] == 'Integration') {
                header('location: integrationffg/test.php');
                exit;
            } else {
                header('location: aaa.php');
                exit;
            }
        } else {
            // Something went wrong. You should state that.
        }
    }
}
?>

<form method="post">
    <center>
        <p>Passer un exam:</p>
    </center>

    <div class="input-group">
        <span class="input-group-addon" id="basic-addon1">Token</span>
        <input required type="text" name="token" class="form-control" placeholder=
        "Ex. b2f56acc7c212ee5af432e9794ccdbef">
    </div>

    <br>
    <center>
        <button type="submit" class="btn btn-primary">Zuu</button>
    </center>
</form>

【讨论】:

    【解决方案2】:

    试试这段代码,希望对你有帮助:

    <?php
    if (isset($_POST['redibtn'])) {
        redit();
    }
    
    function redit()
    {
        global $db, $token, $errors;
    
        // grap form values
        $token = e($_POST['token']);
    
        // attempt login if no errors on form
        if (!count($errors)) {
            $query = "SELECT * FROM tokens WHERE Token='$token' LIMIT 1";
            $results = mysqli_query($db, $query);
    
            if (mysqli_num_rows($results) == 1) { // user found
                // check if user is admin or user
                $choix = mysqli_fetch_assoc($results);
                if ($choix['test'] == 'Integration') {
                    header('location: integrationffg/test.php');
                } else {
                    header('location: aaa.php');
                }
            } else {
    
            }
        }    
    }
    
    ?>
    
    <form method="POST">
        <center>
            <p>Passer un exam :</p>
        </center>
    
        <div class="input-group">
            <span class="input-group-addon" id="basic-addon1">Token</span>
            <input required type="text" name="token" class="form-control"
                   placeholder="Ex. b2f56acc7c212ee5af432e9794ccdbef">
        </div>
        <br>
        <center>
            <button type="submit" class="btn btn-primary" name="redibtn">Zuu</button>
        </center>
    </form>
    

    您的代码中几乎没有问题

    • 如果表单中没有方法,它会通过方法GET发送,但你试试$_POST
    • Furm 必须有提交按钮
    • 在开始html之前关闭php标签?&gt;

    【讨论】:

      【解决方案3】:

      试试这个

      <input type="button"  class="btn btn-primary" name="test" id="test" /><br/>
      
      <?php
      
      function redit()
      {
         echo "Your redit function on button click is working";
      }
      if(array_key_exists('test',$_POST)){
         redit();
      }
      ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-04-22
        • 2020-05-15
        • 1970-01-01
        • 1970-01-01
        • 2020-03-26
        • 2019-04-30
        • 2011-10-25
        • 1970-01-01
        相关资源
        最近更新 更多