xProteuSx Posted March 10, 2010 Share Posted March 10, 2010 I am creating a form that uploads a file, but also sends a variable, called user_id, like this: <form id="file_upload_form" method="post" enctype="multipart/form-data" action="upload.php?user_id="+user_id+" \"> This way I can get PHP to upload the file, then also get it to capture the value of user_id using the ol': $_GET['user_id']; But this doesn't seem to work. Any ideas would be much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/194741-fetch-file-and-variable-using-same-form/ Share on other sites More sharing options...
trq Posted March 10, 2010 Share Posted March 10, 2010 Where exactly is user_id being defined? An how is this form tag being written? Can we see some actual code? A little surrounding this line would be good. Quote Link to comment https://forums.phpfreaks.com/topic/194741-fetch-file-and-variable-using-same-form/#findComment-1024126 Share on other sites More sharing options...
xProteuSx Posted March 10, 2010 Author Share Posted March 10, 2010 Ofcourse, and thanks for helping This is what the Javascript string / HTML end up looking like: <form id="file_upload_form" method="post" enctype="multipart/form-data" action="upload_avatar.php?user_id="+user_id+"> <table width="500" cellpadding="3" cellspacing="2"> <tr> <td><h4>Update Your Avatar (Icon)</h4></td> </tr> <tr> <td><input name="file" id="file" size="27" type="file" /><br /><br /></td> </tr> <tr> <td><input type="submit" name="action" value="Update Avatar" /></td> </tr> </table> </form> upload_avatar.php, the action, looks like this, for example: upload_avatar.php?user_id=1 The actual PHP file upload_avatar.php looks like this: <?php $user_id = $_GET['user_id']; echo $user_id; move_uploaded_file($_FILES["file"]["tmp_name"], './avatars/' . $_FILES["file"]["name"]); ?> If I try to do this ===> $user_id = $_GET['user_id'] <=== the php crashes, without errors. I think this has something to do with the enctype, because it is set to 'multipart/form-data'. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/194741-fetch-file-and-variable-using-same-form/#findComment-1024137 Share on other sites More sharing options...
trq Posted March 10, 2010 Share Posted March 10, 2010 This is what the Javascript string / HTML end up looking like: <form id="file_upload_form" method="post" enctype="multipart/form-data" action="upload_avatar.php?user_id="+user_id+"> Well that isn't right. It should end up looking something like... <form id="file_upload_form" method="post" enctype="multipart/form-data" action="upload_avatar.php?user_id=2"> Why don't you post the code that produces your form tag? Quote Link to comment https://forums.phpfreaks.com/topic/194741-fetch-file-and-variable-using-same-form/#findComment-1024155 Share on other sites More sharing options...
gamblor01 Posted March 10, 2010 Share Posted March 10, 2010 I agree with thorpe's comment -- the action string that is being generated isn't correct because the user_id variable isn't being properly resolved to its value. You need to make sure that this is correct before you can proceed. However, it appears that your form method is POST. I don't believe you can just append the parameters to the end of the URL like you are doing and still perform a POST. You will need to send the values through GET instead. I see that you are already using $_GET['user_id'] in update_avatar.php so that is correct. Maybe I'm wrong...maybe you can send params through both POST and GET at the same time, but it seems weird to mix them anyway. I would stick to a single implementation and use it. Therefore, instead of appending the userid to the end of the php script in the action field, you could create a hidden form element that stores the user ID and just leave action as the name of the script only: echo "<form method=\"post\" action=\"upload_avatar.php\">"; echo "<input type=\"hidden\" name=\"user_id\" value=\"$user_id\">"; // the rest of your form here echo "</form>"; Now you can pull out the value of user_id by using $_POST in your upload_avatar.php script: $user_id = $_POST['user_id']; You certainly need to start by making sure that the code generating your HTML form resolves the user_id to an actual value though. Once you are sure of that, you can decide to use GET or POST. Quote Link to comment https://forums.phpfreaks.com/topic/194741-fetch-file-and-variable-using-same-form/#findComment-1024402 Share on other sites More sharing options...
xProteuSx Posted March 10, 2010 Author Share Posted March 10, 2010 Thanks very much guys. I'm sure that the formatting of the action for the form is correct ... just seems awkward because I have it stored in a string variable using javascript, but I have tested all output and input. Anyways, I have found a bit of a work-around. I just stored the user_id in a cookie, then retrieved it. This hidden field idea is really interesting though ... I will give it a try sometime, because it will definitely come in handy if it works. Thanks for all your replies. Quote Link to comment https://forums.phpfreaks.com/topic/194741-fetch-file-and-variable-using-same-form/#findComment-1024426 Share on other sites More sharing options...
trq Posted March 10, 2010 Share Posted March 10, 2010 I'm sure that the formatting of the action for the form is correct ... I'm telling you it is not. Why ask for help if your going to ignore the given suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/194741-fetch-file-and-variable-using-same-form/#findComment-1024445 Share on other sites More sharing options...
xProteuSx Posted February 16, 2011 Author Share Posted February 16, 2011 thorpe, First of all, thank you for your unending aid. I know its been a while since this topic came up, but to answer your question ... I appreciate all of the replies I receive in this forum. I have learned a few tricks and things here, and I owe a lot of my PHP/MySQL knowledge to the admins here. That having been said, please do not take it personally if I don't take all of the advice that I receive, including yours I usually don't implement new suggestions if I have already found a solution. When I have time, which is not often, I will try the alternate solutions so that I can become a better programmer but, again, that all comes down to time ... and there just ain't enough, if you know what I mean. Cheers to you my friend. Also, thank you for all the help that you have given and continue to give in this forum, from myself and the hundreds (if not thousands) of others which you have helped. Quote Link to comment https://forums.phpfreaks.com/topic/194741-fetch-file-and-variable-using-same-form/#findComment-1174901 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.