【问题标题】:HTML button submit not activating php codeHTML按钮提交未激活php代码
【发布时间】:2016-11-27 16:21:05
【问题描述】:

我的 html 表单无法正确提交。我是表单的新手,一直试图让登录脚本运行,但它一直失败。我主要使用铬。这是超级愚蠢的代码,希望能证明我的问题。我猜这段代码有什么问题是在扩展代码中。这是我用于登录编码的网站:login account setup site mainly for reference

<?php

//this is here for testing
session_start();

//this shows
echo "something";

//none of these show
if(isset($_POST['submit'])){

    echo "something";
    echo "something";
    echo "something";
    echo "something";
}
if(isset($_POST['submit2'])){

    echo "something";
    echo "something";
    echo "something";
    echo "something";
}if(isset($_POST['submit3'])){

    echo "something";
    echo "something";
    echo "something";
    echo "something";
}

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
</head>
<body>
<footer>
    <!-- I have tried it with and without an action -->
    <form action="button_test.php" method="post" id="this" style="width: 100%; height: 100%; background-color: darkblue;">
        <button type="submit" name="submit" id="submit" class="button" value="Submit">this is something</button>
        <button type="button" name="submit2" id="submit2" class="button" value="this">this is something2</button>
        <input type="button" name="submit3" id="submit3" class="button" value="true"/>
    </form>
</footer>
</body>
</html>

第一个按钮可以预见地重新加载页面,而其他按钮什么也不做。这是我需要工作的扩展主代码。再一次,我猜上面的代码可能解释了下面的代码有什么问题。下面的代码主要用于提供上下文和更准确的答案。

<?php

//require once the DatabaseController.php file,
require_once 'Controllers/DatabaseController.php';
echo "this happend1";
// Creates a data connect object that creates a connection to
// a database
$connection = new DataConnect;
//Run the user_load function to set the user
$connection->user_load();
echo "this happend2";
/*
 * Runs the in_loggedin function from a
 * user object and checks if it is blank
 */
if($user->is_loggedin()!="")
{
    echo "this happend3";
    //return user to logged in home page if
    //they are logged in
    $user->redirect('index.php');
}
echo "this happend4";
/*
 * THIS DOES NOT WORK FOR SOME REASON
 */
if(isset($_POST['btn-login']))
{
    echo "this happend5";
    //Load the variables with the new form data that has been executed
    $uname = $_POST['txt_uname_email'];
    $umail = $_POST['txt_uname_email'];
    $upass = $_POST['txt_password'];

    //If the user->login call is successful, go to home fully logged in
    if($user->login($uname,$umail,$upass))
    {
        //Go to home, fully logged in
        $user->redirect('index.php');
    }
    else
    {
        //Error
        $error = "Wrong Details !";
    }
}
?>


<!DOCTYPE html>
<html lang="en-US">
    <head>
        <meta charset="UTF-8">
        <title> Josiah</title>

        <link href="Assets/Stylesheets/styles-home.css" type="text/css" rel="stylesheet" />
    </head>
    <body>
        <?php
        include_once 'header.php';
        ?>

<!-- recent blog post: contains the most recent blog post-->
<div id="main-content" class="content">
    <div id="main-content-inner" class="inner-content">
        <!-- this is the container for the form -->
        <div class="form-container">
            <!-- create a form that uses the "post" method which allows php to do $_POST on
            corresponding variable names -->
            <form method="post">

                <h2>Sign in.</h2><hr />
                <!-- checks if there was an error. If the error variable is set -->
                <?php
                if(isset($error))
                {
                    ?>
                    <!-- displays the error -->
                    <div class="alert alert-danger">
                        <i class="glyphicon glyphicon-warning-sign"></i> &nbsp; <?php echo $error; ?> !
                    </div>
                    <?php
                }
                ?>
                <!-- this is a form-group -->
                <div class="form-group">
                    <!-- this is a text input. -->
                    <input type="text" class="form-control" name="txt_uname_email" placeholder="Username or E mail ID" required />
                </div>
                <!-- this is a form-group -->
                <div class="form-group">
                    <!-- this is a text input. -->
                    <input type="password" class="form-control" name="txt_password" placeholder="Your Password" required />
                </div>
                <!-- splits parts of the form -->
                <div class="clearfix"></div><hr />
                <!-- this is a form group holdng the submit button -->
                <div class="form-group">
                    <!-- the button interestingly has multiple classes -->
                    <button type="submit" name="btn-login" class="btn btn-block btn-primary">
                        <!-- splits parts of the form. Dont know specifically what this does. -->
                        <i class="glyphicon glyphicon-log-in"></i>&nbsp;SIGN IN
                    </button>
                </div>
                <br />
                <label>Don't have account yet ! <a href="sign-up.php">Sign Up</a></label>
            </form>
        </div>
    </div>
</div>

<!-- footer: contains social media, site map, and an up arrow-->
<?php
include 'footer.php'
?>
</body>
</html>

【问题讨论】:

  • 让我们测试一下:在您执行var_dump($_POST)echo file_get_contents('php://input') 的脚本顶部,每行显示什么?
  • 我得到:array(0) { } submit=Submit
  • 它似乎是按按钮值进行的,但我希望它按按钮名称进行,我虽然这样做了。我引用的网站和扩展代码没有使用按钮中的值字段,我觉得除非绝对需要,否则我想保持这种方式。似乎 if 语句
  • 看看我的回答
  • 我的顶级代码可以在其他人的计算机上运行吗?我现在在两台计算机上尝试了这段代码,但都不起作用

标签: php html forms htmlbutton


【解决方案1】:

由于php://input 显示表单值(根据我让你在 cmets 中运行的测试),我们知道 PHP 正在正确接收数据。

默认情况下,PHP 会根据需要将请求参数放在$_GET$_POST 中。在您的情况下,这似乎没有发生。我建议您查看您的 php.ini 文件并添加以下设置:

variables_order = "GPCS"

可能已经有variables_order 行,所以在这种情况下只需编辑它 (documentation)。

【讨论】:

  • 也没有评论吧? (前面是;)。如果是这样,请取消注释。您还需要重新启动 PHP 才能使更改生效。
  • 是的,它已经在那里了。 $_POST 一直运行良好。我设置“$_POST['this thing'] = 6;”在 var 转储之前,将 if 语句之一设置为“if(isset($_POST['this thing']))”并且它可以工作。似乎 html 表单没有设置帖子。如果我移动“$_POST['this thing'] = 6;”到页面末尾然后什么也没有发生。
  • 我可以尝试将 php 拆分为一个完全不同的文件,然后在按下按钮时调用,但我觉得我应该能够像网站一样在一个文件中使用它们
  • 在脚本中设置$_POST['x'] 并读回它并不能告诉我们$_POST 是否有效。测试是:PHP 是否使用从表单接收到的值自动填充它。如果您在表单中添加另一个 &lt;input type='text'&gt; 并填写文本,文本是否显示在:1)var_dump($_POST) 2)var_dump($_REQUEST) 或 3)echo file_get_contents('php://input')(当然将所有这些行放在开头脚本)
  • 文本不显示。 echo file_get_contents('php://input') 说 submit=Submit 指的是第一个提交按钮。其他的是空数组。这是我的 ini 中的行:;为这些阵列的注册付费,因为 ENV 并不常见;与其他使用一样,不建议在生产服务器上使用 ENV。你 ;如果您仍然可以通过 getenv() 访问环境变量;需要。 ;默认值:“EGPCS”;开发价值:“GPCS”;产值:“GPCS”; ; php.net/variables-order variables_order = "GPCS"
【解决方案2】:

所以经过一番搜索后,我忘记透露我正在使用 PHPstorm。我没有发生这种情况的原因是 PHPstorm 没有安装在我一直在使用的另一台计算机上,但是由于我在两台计算机上都使用 Intellij,因此 PHPstorm 中的这个错误转移了,因为 Intellij 将使用 PHPstorm 插件。似乎 10.1-10.3 这个错误在 POST 保持为空的情况下仍然存在。我不确定它是否已修复,所以我转移到 Apache 并让它做网络服务。一切正常,或者至少工作得更好。两种解决方案要么等待 PHPstorm 修复此错误,要么更好地转移到 Apache。

link to site post

【讨论】:

    猜你喜欢
    • 2021-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-14
    • 2017-04-09
    • 2021-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多