Page 1 of 1

GET and POST in one form?

Posted: Sun Apr 12, 2009 6:40 pm
by zylkaa
I need to use GET and POST by either having two forms or one form using GET and POST both in it.

I have this code:
<form enctype="multipart/form-data" action="upload.php" method="POST">
Please choose a file: <input name="uploaded" type="file" /><br />
<input type="submit" value="Upload" /><input name="name" value="
<?php
$name = $_GET["name"];
echo $name
?>" type="hidden">
</form><br><br>
<?php
$name = $_GET["name"];

echo "Thanks for using File Fetchers, $name!";
?>

I need to get $name to upload.php using GET, but I need the file to be uploaded to upload.php
What can I do?

Thanks,
Alex

Re: GET and POST in one form?

Posted: Sun Apr 12, 2009 6:58 pm
by Chris Corbyn
Stick the GET parameters inside the action attribute of the for, but keep the form's method attribute set to post:

Code: Select all

<form action="script.php?some=get&parameters=here" method="post">

Re: GET and POST in one form?

Posted: Sun Apr 12, 2009 7:14 pm
by McInfo
Though not critical, it's a good idea to use urlencode() on the GET variables before you put them in the form.

Edit: This post was recovered from search engine cache.

Re: GET and POST in one form?

Posted: Sun Apr 12, 2009 7:23 pm
by zylkaa
Could you give me an example of how this would be implemented in my code?

Thanks,
Alex

Re: GET and POST in one form?

Posted: Sun Apr 12, 2009 10:30 pm
by Chris Corbyn

Code: Select all

<?php
$name = isset($_GET["name"])
  ? $_GET["name"] //If $_GET['name'] is there, use it
  : "some default"; //Otherwise use this default
?>
<?php /* Add the name parameter back into the form action */ ?>
<form enctype="multipart/form-data" action="upload.php?name=<?php echo htmlspecialchars(urlencode($name)); ?>" method="POST">
Please choose a file: <input name="uploaded" type="file" /><br />
<input type="submit" value="Upload" />
</form><br><br>
<?php
echo "Thanks for using File Fetchers, $name!";
?>

Re: GET and POST in one form?

Posted: Mon Apr 13, 2009 6:54 am
by zylkaa
Thanks! That helped. Could I send the name in a form (to me) and upload the file with upload.php? I mean, could I have two form functions in one page?