jyushinx Posted February 8, 2008 Share Posted February 8, 2008 Hi, I have searched and searched for an answer to this question, and while there are many posts on it floating around, I have yet to find both a) a solution to my exact problem and b) one that actually works. Let me explain the situation so you can see what I am trying to do. I have a page, let's call it message.php, which contains a form where a person can write a message to be sent. When they click Submit, the form redirects to another script, let's call it message_processor.php, which deals with the information from message.php. Now, message_processor.php checks for valid data, and if it finds one of the fields is invalid (empty, too long, etc.) I want it to redirect back to message.php, where the form can be displayed with an error message. Now in the past, I have always used GET variables to deal with this, so in message_processor.php if there were an error, we'd use something like this: header("Location: http://www.mysite.com/message.php?errorId=34"); Which works fine, except if I want to send a large amount of data. Too large for a query string. So say I want to return to message.php with not only the errorId, but also the form data, so the user doesn't have to re-enter it. And say that one of these form elements is very large, say 2500 characters. Is there a way to do this using a POST or something similar? From my readings, I understand the contradiction in this request. POSTS are sent from Browsers to the server. So I can redirect to an address using header(), or I can send POST info using cURL, etc. to a script, but what I need is to essentially do both at once. Send POST info to a script and have that script become the location in the browser. So first question, what is the actual max length of a Query String, assuming you are using a browser that isn't from the Stone Age? I've read differing reports, so I'm a bit lost on this. Second question, is there any way to achieve what I described either with POST variables or something similar using PHP? Thanks for the help. I tried to be as descriptive as I could, but let me know if something is unclear in my request. Quote Link to comment https://forums.phpfreaks.com/topic/90027-solved-sending-post-variables-to-a-script-and-redirectingimpossible/ Share on other sites More sharing options...
haku Posted February 8, 2008 Share Posted February 8, 2008 If you include your processing script in the same file as the form itself, you won't have to send the data back, it will still be there to access at $_POST variables. Structure your site like this: 1) check to see if $_POST['submit'] has been set (this will make sense later) 2) if it has, process form data - if there are errors, enter them into a variable - if there are none, use the location header to forward the user to a different page 3) if $_POST['submit'] hasn't been set, or there are any errors, output your doctype and <html> tag (this will not happen if the user has submitted the form and all information is correct) 4) Check to see if there are any variables set for script errors, output the necessary error warning if there are 5) output the form. Give the submit button a name attribute of 'submit'. By doing this, when the person submits the form, $_POST['submit'] will be set, and step 2 will be entered. Quote Link to comment https://forums.phpfreaks.com/topic/90027-solved-sending-post-variables-to-a-script-and-redirectingimpossible/#findComment-461574 Share on other sites More sharing options...
haku Posted February 8, 2008 Share Posted February 8, 2008 Sorry, I left out a couple points on this. 1) the set the 'action' of the form to be the same page as the form is on. 2) Since you are in the same page, if there are any errors for which the form data is necessary, you can use $_POST['data'] to output that data. Quote Link to comment https://forums.phpfreaks.com/topic/90027-solved-sending-post-variables-to-a-script-and-redirectingimpossible/#findComment-461576 Share on other sites More sharing options...
jyushinx Posted February 8, 2008 Author Share Posted February 8, 2008 Excellent. That makes perfect sense. I've done something like that before, but was so wrapped up in figuring it out "this way" I didn't step back and realize that. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/90027-solved-sending-post-variables-to-a-script-and-redirectingimpossible/#findComment-461938 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.