php 将参数传递给 HTML 中的表单操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24453038/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:19:30  来源:igfitidea点击:

Pass parameter to form action in HTML

phphtml

提问by user3193843

I am trying to implement a simple file upload using the files here :

我正在尝试使用此处的文件实现简单的文件上传:

http://www.sanwebe.com/2012/05/ajax-image-upload-and-resize-with-jquery-and-php

I have it working and managed to change the styles etc via CSS and change the filenames etc etc.

我让它工作并设法通过 CSS 更改样式等并更改文件名等。

I now need to use this in an iframe and pass a variable for the filename.

我现在需要在 iframe 中使用它并为文件名传递一个变量。

In my app I can use either localstorage,setItem('clientID',10) or add a parameter to the iframe url. www.xxx.com/uploads?clientID= 10.

在我的应用程序中,我可以使用 localstorage,setItem('clientID',10) 或向 iframe url 添加参数。www.xxx.com/uploads?clientID= 10。

The plugin has an html file that then calls the php file with :

该插件有一个 html 文件,然后使用以下命令调用 php 文件:

<form action="processupload.php" method="post" enctype="multipart/form-data" id="MyUploadForm">

So what is the 'correct' method to get my clientID variable to the final.php file.

那么将我的 clientID 变量获取到 final.php 文件的“正确”方法是什么。

Can I use javascript in the HTML file and pass the variable to the form action ie:

我可以在 HTML 文件中使用 javascript 并将变量传递给表单操作,即:

<form action="processupload.php?"+clientID method="post" enctype="multipart/form-data" id="MyUploadForm">

Or can I grab the url parameter in the html file and add to the form action.

或者我可以在html文件中获取url参数并添加到表单操作中。

I tried this after the body tag:

我在 body 标签之后尝试了这个:

<?php
$clientID='100';
?>

Then changed the form action to :

然后将表单操作更改为:

<form action="processupload.php?clientID=" <?php echo $clientID ?> method="post" enctype="multipart/form-data" id="MyUploadForm">

Then in the PHP file I added:

然后在我添加的 PHP 文件中:

$clientID = $_GET["clientID"];

and changed the filename details to:

并将文件名详细信息更改为:

$NewFileName        = $clientID.$File_Ext; //new file name

The process works as I get the file uploaded but the clientID is blank. eg the file is name '.png' instead of '100.png'

该过程在我上传文件时起作用,但 clientID 为空。例如,文件名为“.png”而不是“100.png”

MrWarby

沃比先生

回答by Mark Ormesher

If you want to do it through HTML....

如果你想通过 HTML 来做....

Instead of

代替

<form action="processupload.php?clientID=" <?php echo $clientID ?> method="post" enctype="multipart/form-data" id="MyUploadForm">

Try this:

尝试这个:

<form action="processupload.php?clientID=<?php echo $clientID ?>" method="post" enctype="multipart/form-data" id="MyUploadForm">

If that still doesn't work (some servers don't like mixing POST and GET), then try swapping for this:

如果这仍然不起作用(某些服务器不喜欢混合使用 POST 和 GET),请尝试交换:

<form action="processupload.php" method="post" enctype="multipart/form-data" id="MyUploadForm">
    <input type="hidden" name="clientID" value="<?=$clientID;?>" />
    <!-- the rest of your form -->
</form>

And then in your PHP file, swap the $_GETfor $_POST.

然后在您的 PHP 文件中,交换$_GETfor $_POST.