问题
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