doubledee Posted February 12, 2012 Share Posted February 12, 2012 In the past I always combine my PHP Form Processing Code with my HTML Forms in one script, and when the User clicks "Submit", the Form/Page reloads itself. Is it possible to load an Upload Form to itself? Something like this... <form enctype="multipart/form-data" action="" method="post"> Debbie Quote Link to comment https://forums.phpfreaks.com/topic/256964-load-upload-form-to-itself/ Share on other sites More sharing options...
SergeiSS Posted February 12, 2012 Share Posted February 12, 2012 Yes, it's possible. And from my point of view it's the preferred method. Because if something is wrong in the input data, I can show all data entered and give to user a chance to change it. When data is correct I can process it and do anything I wish. If I need, I can root to another script or do something here, in this script. Quote Link to comment https://forums.phpfreaks.com/topic/256964-load-upload-form-to-itself/#findComment-1317330 Share on other sites More sharing options...
gizmola Posted February 12, 2012 Share Posted February 12, 2012 You can omit the action and it will default to itself, however most people will explicitly set the action programmatically using a variable. Of the available methods, there is PHP_SELF, REQUEST_URI and SCRIPT_NAME. I'll leave it to you to investigate the differences between those options, but one that is frequently the best choice is to use: </pre> <form enctype="multipart/form-data" action="<?php%20echo%20%24_SERVER%5B'SCRIPT_NAME'%5D;%20?>" method="post"> < Quote Link to comment https://forums.phpfreaks.com/topic/256964-load-upload-form-to-itself/#findComment-1317335 Share on other sites More sharing options...
doubledee Posted February 12, 2012 Author Share Posted February 12, 2012 Yes, it's possible. And from my point of view it's the preferred method. Because if something is wrong in the input data, I can show all data entered and give to user a chance to change it. When data is correct I can process it and do anything I wish. If I need, I can root to another script or do something here, in this script. I like to do that too. Debbie Quote Link to comment https://forums.phpfreaks.com/topic/256964-load-upload-form-to-itself/#findComment-1317339 Share on other sites More sharing options...
doubledee Posted February 12, 2012 Author Share Posted February 12, 2012 You can omit the action and it will default to itself, however most people will explicitly set the action programmatically using a variable. Of the available methods, there is PHP_SELF, REQUEST_URI and SCRIPT_NAME. I'll leave it to you to investigate the differences between those options, but one that is frequently the best choice is to use: <form enctype="multipart/form-data" action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="post"> Why can't I just do... action="" That is what I have always done in the past. Debbie Quote Link to comment https://forums.phpfreaks.com/topic/256964-load-upload-form-to-itself/#findComment-1317340 Share on other sites More sharing options...
gizmola Posted February 12, 2012 Share Posted February 12, 2012 I said you could omit it, by which I meant you could leave it empty or not include the action at all. They are essentially the same, but as far as picking one or the other, using action="" is probably better, as it will validate while omitting the action entirely probably will not. The main reason not to use action="" is that it triggers "magic behavior" in the browsers. All the main web browsers handle this situation fine, and redirect, however, there are fringe browsers used on some mobile phones that are known not to behave in the way you desire. So it is better to explicitly provide a uri, if you want to maximize your compatibility across all user agents. Whether you care about that or not, is up to you. Quote Link to comment https://forums.phpfreaks.com/topic/256964-load-upload-form-to-itself/#findComment-1317344 Share on other sites More sharing options...
doubledee Posted February 12, 2012 Author Share Posted February 12, 2012 I said you could omit it, by which I meant you could leave it empty or not include the action at all. They are essentially the same, but as far as picking one or the other, using action="" is probably better, as it will validate while omitting the action entirely probably will not. The main reason not to use action="" is that it triggers "magic behavior" in the browsers. All the main web browsers handle this situation fine, and redirect, however, there are fringe browsers used on some mobile phones that are known not to behave in the way you desire. So it is better to explicitly provide a uri, if you want to maximize your compatibility across all user agents. Whether you care about that or not, is up to you. Yes, I want the best code I can write. So it sounds like you think this is best... action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" (I know that PHP_SELF is a no-no!) Debbie Quote Link to comment https://forums.phpfreaks.com/topic/256964-load-upload-form-to-itself/#findComment-1317348 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.