我制作了一个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>
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'");
<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,但我不知道该怎么做。
答案 0 :(得分:1)
首先:请注意,您执行的代码包含用户输入!因此,用户可以尝试在他想要的服务器上执行代码,但是您不会严重损坏服务器或数据!
要解决加载微调器的问题,应尝试使用Javascript和单个页面进行表单输入和加载微调器。包含jQuery后,截取Form Submit并使用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代码”片段是welcome.php?