【问题标题】:pass php post variable from css从css传递php post变量
【发布时间】:2019-05-21 09:06:36
【问题描述】:

我已经制作了一个 index.php 页面,其中包含一个表单和一些要使用 php 执行的 python 代码,我想在 php 执行 python 代码时显示一个微调器 gif。我发现:https://stackoverflow.com/a/34298686/11160699 但我不知道如何将 POST 变量传递给 php。这是我的代码:

  • 索引形式:
<form action="welcome.php" method="post">
            <div class="row">
                <div class="col-25">
                    <label for="fname">Website url</label>
                </div>
                <div class="col-75">
                    <input type="text" name="site" placeholder="Enter the site to be validated" autocomplete="off"  pattern="(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+" required>
                </div>
            </div>
            <div class="row">
                <!--input type="submit" value="Validate"-->
                <div><input type="submit" name="user_button" value="I'm a normal user"></div>
                <div><input type="submit" name="programmer_button" value="I'm a programmer"></div>
            </div>
        </form>
  • php代码:
 ob_start();
            $url = $_POST["site"];
            $path="pyt";
            $dir = chdir($path); 
            list($scriptPath) = get_included_files();
            echo exec("C:/Users/me/PycharmProjects/env/Scripts/python main.py  '$scriptPath' '$url'");
  • 在 loader.gif 之后调用 php 文件:
<div id="ok">

<h1> Please Wait </h1>
<p> Processing the url. It may take a few seconds. </p>
<img src='images/loader.gif' alt='loading' />

</div>
<style>
#runPHP
{ 

  background-image:url(phps.php);
  display:none;
}
</style>
</head>
<body>

<div id="runPHP"></div>

有什么想法吗?我知道我也可以使用 Javascript,但我不知道该怎么做。

【问题讨论】:

    标签: php post


    【解决方案1】:

    首先:请注意,您执行的代码包含用户输入!因此,用户可以尝试在您的服务器上执行他想要的代码,但您不会严重损坏您的服务器或您的数据!

    要解决您的加载微调器问题,您应该尝试使用 Javascript 和单页进行表单输入和加载微调器。包含 jQuery 后,拦截表单提交并使用 AJAX 发送数据。在请求运行时,您可以显示加载微调器。

    $(function() {
      $("form").submit(function(e) {
        e.preventDefault(); // Prevent the default submission of the form
        var form = $(this);
        var url = form.attr('action'); // Extract the URL from the form
    
        // Show your loading information and hide the form
        $("#loading_div").show();
        $("form").hide();
    
        $.ajax({
               type: "POST",
               url: url,
               data: form.serialize(), // Send the form information as 
               success: function(data) {
                   // Script has finished
                   // Do something here...
               },
               error: function() {
                   // Script execution not successful
               }
        });
      });
    });
    

    还有你的索引:

    <form action="welcome.php" method="post">
                <div class="row">
                    <div class="col-25">
                        <label for="fname">Website url</label>
                    </div>
                    <div class="col-75">
                        <input type="text" name="site" placeholder="Enter the site to be validated" autocomplete="off"  pattern="(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+" required>
                    </div>
                </div>
                <div class="row">
                    <!--input type="submit" value="Validate"-->
                    <div><input type="submit" name="user_button" value="I'm a normal user"></div>
                    <div><input type="submit" name="programmer_button" value="I'm a programmer"></div>
                </div>
            </form>
    
    <div style="display: none;" id="loading_div">
        <h1>Please wait</h1>
        <p> Processing the url. It may take a few seconds. </p>
        <img src="images/loader.gif" alt="loading" id="loader" />
    </div>
    

    我假设您的第二个带有“php 代码”的 sn-p 是welcome.php?

    【讨论】:

    • 我有一个带有索引的页面,一个名为“welcome”的页面,其中包含执行 python 脚本的 php 代码,该脚本执行一些操作并使用 python 脚本的结果创建一个新页面,我必须在执行 python 脚本后显示。我知道,这听起来像是绕口令:D 无论如何,即使有你的建议,我仍然看不到加载微调器...
    • 确保在页面加载后绑定了 Javascript 处理程序。您可以将其包装在 $(function() {...}) 中以实现此目的。此外,浏览器开发者工具向您展示了什么?
    • mmm 好的,现在我可以加载微调器了(Javascript 处理程序是在页面加载之前绑定的,所以我修复了它),但它似乎没有在欢迎页面中调用 php跨度>
    • 再次检查浏览器的控制台和网络标签
    • 使用控制台让我意识到我只需要等待。非常感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-23
    相关资源
    最近更新 更多