Pass parameter to form action in HTML

不羁的心 提交于 2021-02-20 02:50:10

问题


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.

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

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.

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

<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.

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

<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.

I tried this after the body tag:

<?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:

$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'

MrWarby


回答1:


If you want to do it through 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:

<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 $_GET for $_POST.



来源:https://stackoverflow.com/questions/24453038/pass-parameter-to-form-action-in-html

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!