GET and POST in one form?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
zylkaa
Forum Newbie
Posts: 8
Joined: Mon Mar 23, 2009 7:32 pm
Location: Places

GET and POST in one form?

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: GET and POST in one form?

Post 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">
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: GET and POST in one form?

Post 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.
Last edited by McInfo on Mon Jun 14, 2010 1:47 pm, edited 1 time in total.
zylkaa
Forum Newbie
Posts: 8
Joined: Mon Mar 23, 2009 7:32 pm
Location: Places

Re: GET and POST in one form?

Post by zylkaa »

Could you give me an example of how this would be implemented in my code?

Thanks,
Alex
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: GET and POST in one form?

Post 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!";
?>
zylkaa
Forum Newbie
Posts: 8
Joined: Mon Mar 23, 2009 7:32 pm
Location: Places

Re: GET and POST in one form?

Post 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?
Post Reply