单击按钮运行 PHP 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8552945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Run PHP File On Button Click
提问by IRHM
I wonder whether someone could help me please.
我想知道是否有人可以帮助我。
I've been looking through this, and many other sites and tutorials to find out how to add a button to a form which opens a PHP file, in this case, a pop up form that allows a user to upload a file to a mySQL database.
我一直在查看这个,以及许多其他网站和教程,以了解如何向打开 PHP 文件的表单添加按钮,在这种情况下,是一个允许用户将文件上传到 mySQL 的弹出表单数据库。
In addition to the opening of the file, I'd like to carry over the 'id' field value from the main form to the pop 'File Upload' form.
除了打开文件之外,我还想将主表单中的“id”字段值转移到弹出的“文件上传”表单中。
From the research I've carried out there seems to be a number of ways to do this, but from a beginners perspective I'm not sure what is the best way to do this.
从我进行的研究来看,似乎有很多方法可以做到这一点,但从初学者的角度来看,我不确定什么是最好的方法。
Could someone perhaps please advise on what is the best way to go about this.
也许有人可以就解决此问题的最佳方法提供建议。
Many thanks and kind regards
非常感谢和亲切的问候
采纳答案by Tim
To pass values between pages:
在页面之间传递值:
Main form:
主要形式:
<form action="myuploadform.php" method="get">
ID: <input type="text" name="id">
<input type="submit" value="Open Form">
</form>
The value of the ID text box will be accessible as $_GET['id']
in myuploadform.php
.
ID 文本框的值可以像$_GET['id']
中那样访问myuploadform.php
。
Using GET
parameters is the simplest way of passing values. Another way to pass in this GET
value would be in the URL:
使用GET
参数是传递值的最简单方式。传递此GET
值的另一种方法是在 URL 中:
.../myuploadform.php?id=35
where the ID then becomes 35.
.../myuploadform.php?id=35
然后 ID 变为 35。
回答by Frank
Here's a sample from my site. All it does is allow the uploading of files to the server. It should serve as a tutorial.
这是我网站上的一个示例。它所做的只是允许将文件上传到服务器。它应该作为一个教程。
<html>
<head>
<script type="text/javascript">
var form_object = null;
var button_object = null;
function submit_form(obj)
{
form_object = obj.parentNode;
form_object.submit();
form_object.disabled = true;
button_object = obj;
button_object.disabled = true;
}
function enable_form()
{
form_object.disabled = false;
button_object.disabled = false;
}
function Add_HTML(html)
{
if(navigator.appName == 'Microsoft Internet Explorer')
{
document.body.insertAdjacentHTML('beforeEnd', html);
}
//Firefox uses the Netscape engine (the Netscape version that really sucked)
if(navigator.appName == 'Netscape' && parseInt(navigator.appVersion) == 5)
{
var freaky_object = document.createRange();
freaky_object.setStartBefore(document.body);
html = freaky_object.createContextualFragment(html);
document.body.appendChild(html);
}
}
</script>
</head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data" target="upload">
<label>File:</label> <input type="file" name="file" />
<br />
<label>File:</label> <input type="file" name="swntic" />
<br />
<input type="button" value="SUBMIT"
onclick="submit_form(this);" />
</form>
<iframe src="about:blank" style="display:none;" id="upload" name="upload"></iframe>
</body>
</html>
server side code:
服务器端代码:
<?
$confirmation = "";
while(list($name) = each($HTTP_POST_FILES)) {
?>
<? if(is_uploaded_file($HTTP_POST_FILES[$name]["tmp_name"])) { ?>
<?= $HTTP_POST_FILES[$name]["name"] ?>
<br />
<?= $HTTP_POST_FILES[$name]["type"] ?>
<br />
<?= $HTTP_POST_FILES[$name]["tmp_name"] ?>
<br />
<?= $HTTP_POST_FILES[$name]["error"] ?>
<br />
<?= $HTTP_POST_FILES[$name]["size"] ?>
<br /><br />
<? } ?>
<?
if(is_uploaded_file($HTTP_POST_FILES[$name]["tmp_name"]))
{
move_uploaded_file($HTTP_POST_FILES[$name]["tmp_name"], "./uploads/" . $HTTP_POST_FILES[$name]["name"]);
chmod("./uploads/" . $HTTP_POST_FILES[$name]["name"], 0644);
$confirmation .= "<a href=\"./uploads/" . $HTTP_POST_FILES[$name]["name"] . "\">" .
$HTTP_POST_FILES[$name]["tmp_name"] . "</a> " . $HTTP_POST_FILES[$name]["type"] . ", " . $HTTP_POST_FILES[$name]["size"] . " bytes<br />";
}
}
?>
<html>
<script>
var confirmation = '<?= $confirmation ?>';
</script>
<body onload="parent.enable_form(); parent.Add_HTML(confirmation);">
</body>
</html>
It's not perfect, but can be used as a learning tool.
它并不完美,但可以用作学习工具。