I have a form which sends data to a proccess page. Is it possible when the code in the process page is executed for the browser to jump back to the previous page?
EDIT
The reason i want this, is because i have a set of parameters which contains checkboxes. These parameters are echoed out via a while loop. I have a table where it shows which of those parameters are active. I would like to check the checkboxes by defualt where the corresponding parameter is in the table.
|||||||||||||||||||||||||||||||| Example ||||||||||||||||||||||||||||||
PARAMETERS:
T-Shirt: checked
Distance: checked
Race: unchecked
TABLE (parameters)
• T-Shirt
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||the example above checks the box if it already exists in the parameters table. When the user clicks back after the process page, the boxes he selected is checked.
Hope i am clear enough
Regards
I think a nice solution might be to implement a front controller for your webbage, a central script to include the appropirate page. Using a system like that, the user stays on the same page all the time even when other pages are loaded.
I haven't used a front controller in quite some time, so I won't write examples due to potential errors and poor coding. You can probably find some examples using google though, I wouldn't be surprised if the subject has been brought up here on Stackoverflow before either.
Even though I'll have to point out Griffin's solution is the best, if you're dead set on a redirect, simply have your PHP script execute the following lines:
echo '<script>document.history.go(-1);</script>';
die();
Or even
die('<script>document.history.go(-1);</script>');
It's a dirty solution, so I must advise against it.
Related
(Text below updated based on some of the responses ...)
I have done some looking online and not found an answer that makes sense to me, so before someone says this has already been answered, please have patience and read the whole thing.
I have a PHP page that outputs a standard HTML form. The form works fine. The issue that comes up is the processing page ("ProcessThis.php" or whatever). If a user has used this page once, and their browser is caching pages, the next time they use it, fill out the form, and submit to "ProcessThis.php", they get a 404 error. This seems to happen if the user has bookmarked the page for the form and comes to it from the bookmark. If they use the browser's refresh button before filling out the form, that seems to clear the issue.
I am fairly sure this is a cache issue, but I am not 100% positive. What does seem to help is when the form page loads, having the person use the refresh button on their browser.
I have done some searching and all the information I can find on refresh (the PHP refresh() function, and meta tags and all that) is that this causes a loop (refresh every "n" seconds). What I need to do I think is find a way to clear the cache for this, rather than refreshing the page every "n" seconds. I am kind of at a loss on this ... my "google-fu" is failing me, perhaps and I am using the wrong search options. Hints, suggestions, etc. would be useful. Thanks.
The form itself (as a couple of people have asked for the code) is fairly complex, and I am not sure posting the whole thing is useful. It's basic HTML form tags ... the actual form tag that starts the form is this (really basic):
<FORM METHOD="POST" ACTION="ProcessCourtReportForm.php">
This now falls into the "never mind" category. It turns out that the people having the problem were linking to an older copy of the form (which I had forgotten to delete from the server) which was still available, but I had deleted the process page from the server, so the 404 Error was real, because the page being looked for really didn't exist. They have been instructed to modify the bookmarks in their browsers, and I removed the older php file for the form to avoid this issue in the future. D'oh.
IS there a way to post a $var to $PHP_SELF so that I can use it further down the script?
After 2 hours reading dozens of questions which helpfully appear in the sidebar to the right,
it became apparent that they pretty much all assume an HTML Form has been / will be
activated.
But psuedo~code of what I need looks more like this:
< php
$someVariable=y;
$otherVar=X;
// and the usual setup for accessing the `$_POST` of php:
$HokeyDino=`$_POST`["SendOFF"];
$SendOFF=101;
// etc. and then come to a point where I need the script to just automatically post a value
[ the lack of knowledge ]
// which if I had tha codez!
// would permit the use of that $var, $HokeyDino ...
if($HokeyDino==100){
// do stuff
}
I don't like looking foolish, but gotta ask away, because I figure I have missed learning some elementary aspect of programming, being self-taught so far, but not knowing what might be lacking makes it hard to go look productively.
Thanks very much!
EDIT // Clarification.
Wow, this is amazing. half an hour, 24 people reading the question. Blows my mind.
Right. What I have gotten done so far to give more background:
A php script which uses fopen to create on the fly another php / html page, and all the
code on the Authouring originating script, to write (a+) to the newly created temp page, the whole thing.
From a loop on the authouring page, I have code for retrieving POSTS I send TO that temp page, and that code gets written to a very temp page... then I cause the first part of the page to be written, to get placed on the Temp page, by put_contents etc.,
Next, from another loop on the Authouring page, I write code which item by item matches the things which were included in the < head > of the Temp page.
Anyhow, without graphics, it's a bit tough to explain. What I have at the point I have gotten to so far, is the newly created/assemble Temporary page, can be accessed as a WebPage,, and a button click on it, will successfully POST a value back to the originating/Authouring script.
Here's the tricky part: There isn't any way I was able to devise, to dynamically create code ON THE AUTHOURING page, to recieve POSTS from the Temp Page.
But I realized that if, in the Loops on the Authoring Page, I was able to $PHP SELF post a
string which would be the code for creating a * $Var = $ POST; to catch the values from button clicks on the TEMP page, it would work.
Critical, is that the Authoring Page, doesn't know how many buttons will be made over on the Temp Page ~ that depends on the number of items in the database, which the loops are reading and translating into code which builds the Temp Page.
So, there is no way to hard~code, on the Authouring Page, all possible code for recieving posts, but I could use one Universal $Var= $ POST[ X ] if I could generate it on the fly in the loop, on the Authoring Page.
Hence the need to write code which will $SELF POST, and have it triggered just by normal programme flow, and not the click of a button in a form.
Hmm.... clear as mud yet? :) the question still is pretty straight foreward.
Cheers!
//// Loop
Create CViewerTemp
read DB and manipulate data
Loop B
create, and write to VeryTempHead page
code which creates the top of CViewer, HEAD items
create, and write to VeryTempBody page
code which will work there, items one by one matching head items
end Loop B
Write code which is 1ne time only stuff, to begin CViewer.
then transfer the stuff from VeryTempHead page, into CViewer, kill
VeryTempHead
then transfer the stuff from VeryTempBody to CViewer, kill Very Temp Body.
Open CTempViwer, click on a Button, a value gets posted to Authouring Page.
Authouring Page doesn't recieve anything, no code to do so exists [YET! :)]
If you want to create data on the fly, but not from $_POST, you can just populate $_POST from any other source like this:
<?php
// some calculations
$_POST['my_var'] = $some_calculated_stuff;
// later in your code
if(isset($_POST['my_var'])) {
// works as if it had been posted
}
?>
Is this what you're looking to do?
It's a bit hard to follow, so I'll suggest another potential:
Have you taken a look at cURL?
I think you mean hidden input fields. You can print them with php and they will be posted to your next php script.
Please note, that the user can change the values and you shouldn't trust them.
Also, you can consider using $_SESSION. That would be the better way to solve the task.
Please note that you shouldn't use PHP_SELF because it's insecure.
i have a website that uses a number of containers (div's). In this scenario, we have three boxes down the left side and one bigger box on the right of these boxes. The bigger box on the right is the only thing that changes when a link is pressed, this is done with Javascript.
SO i have the index which is the main layout of the website and i also have the "pages" of the site, so when a link is pressed the main box (on the right) loads the new data.
On one of my pages i want to collect data and then run it through a PHP script that is in the head of the file, but when i click the button i realise it refreshes the whole page and it doesn't run the script on the page with the form.
Anyone had a problem like this, or know of something i could do to work around it?
I'm not really sure what code would be useful for helping me but if you need something just ask.
Thanks for the help
Since you are loading all your content via JS already, you could just POST the form data via AJAX to a PHP script to process, then read the output and either provide an error message or remove the form from the page and show your success message.
How to approach your AJAX call is dependant on what you've used as a basis for the rest of your JS.
Personally I like to use the JQuery library, as it makes AJAX calls (and much more) very simple.
How about you make the page design able to do it. Have the backend be able to spit out the state of the page when it posted.
Or use Ajax to post the data back and set the new state like you do already.
The short version of what I am looking to do is make a "safe" and idiot-proof page, at least as much as possible.
I have researching the best way to make every hyperlink on a page (ie. standard menus used on the page or any other hyperlinks outside of a HTML form) submit/POST the form information and then redirect to the page clicked on.
For some reason not all users remember the "Save" button before clicking on something else. Then they are upset because they didn't save their information. I would much rather have all the information be saved before sending them on to the hyperlink's URL.
Helpful information:
- This is on a PHP based project.
- I have already put in place code to "detect" what kind of page is in use and if the feature is needed.
- I have header() redirects in some places for other items, so that is nothing new.
- I have looked at javascripts to add, when needed, so that the form is POSTed, and found some options for that.
At this point I can't get the final steps put together, such as a javascript that would POST the information AND store the URL that the hyperlink was supposed to go to. Process the POST information and then redirect to the stored URL.
Dream Example:
onclick="submitformandforward("Example_form", "http://example.mysite.com/example_page.php?querystring=Y")"
Any suggestions, tips, examples, etc are warmly welcomed!
Thanks.
KH
Javascript is the tool you require...
remember some people may have it switched off...
Wehn some clicks a link - check that the form values have been altered - if not carry on - if they have inform that that changes need saving...
Okay, a little preface: I am still a beginner when it comes to AJAX, and dynamic web stuff. My problem is similar to what was asked in the following, but I think that discussion is using a framework, and I am not.
Potentially related post
Here is my scenario:
I have several select elements in a form. The initial select's option elements are populated each time the page loads. The option elements for the subsequent selects get populated depending on what the user selects in the previous select. So it is like a chain. The options in the selects are populated by making AJAX function calls (just my own javascript) that in turn call a php file to get values from a database and build a responseText to fill each subsequent select based on the selection made in the previous select. Hopefully that is clear? All that works fine and dandy.
My problem starts here:
When the user submits the form, a php file is run to process the data, displaying success or any issues, and then I return the user to the form page. I want to reselect all the options they had selected prior to submitting, since some of this is a repetative task and this would save them time. My first step is to recall the required AJAX functions to repopulate the select elements, and then I thought I could run javascript in the form page to select the previously selected options. However, when I try to run javascript on the form page to select the options, it is running before the AJAX calls finish. Therefore, because the options are populated by the AJAX calls, the options do not exist yet in the selects, so I cannot select them. I tried writing some test code to insert a new option to see when the code runs, and sure enough, that new option gets added to the select before my AJAX populated ones go in. In order to track if options were selected, I am passing back the selection option values through the URL, and then processing the $_GET array in my form page.
So my question boils down to:
Is there something I could do that would prevent my javascript that selects the selected options from running until the AJAX populating functions finish?
I would also accept responses like "Your whole approach is bogus! Where did you get your AJAX coding license??!! A Cracker Jack box??" Although, just a few of those responses please, I'm a fragile flower ;)
Thanks in advance,
Carvell Fenton
P.S. Hopefully that's not too much preamble, but I thought the background was necessary.
How many levels deep are the chained selects? I'm doing this with only 2 levels (1 ajax driven select) and for the second page load I would add some code to my jquery document ready function that would initiate the ajax call to load the select list but also send it the id of the field I wanted selected so when the ajax call completes it will have loaded my select and also set it's necessary value.
It would help if you show us your code.
Are you making the AJAX call synchronously? That is one way to do it, but that will stop execution of any subsequent code until the call completes.
Another (and the more correct, I think) way is to make the call asynchronously, and put the code you want to execute after in the onComplete handler.
http://docs.jquery.com/Ajax/jQuery.getJSON#urldatacallback <-- Take a look at the callbacks with jQuery
Also look at the Start \ Stop http://docs.jquery.com/Ajax/ajaxStart#callback and http://docs.jquery.com/Ajax/ajaxStop#callback - you can execute javascript to do anything you require....
Use the onreadystatechange event listener.
Thanks for all the answers! Once again the Stackoverflow folks have enlightened me. Based on the comments, I think I have several options:
(1) I can avoid a framework and just pass more information to be processed in the onreadystatechange event listener (I think this is what Jeremy Stein was suggesting, even though my comment confused the issue). In this case, when I am dynamically adding the option elements to the select, I would just set one of them to selected. In that case I have to pass the selected option (via URL I guess) to the php file, then have the php file include that in the built responseText so I know which option to set as selected in the onreadystatechange listener.
(2) I can use an AJAX call to wrap the form post and never leave the form page (as Mikeb suggested).
(3) I can add in a framework that does the work for me (CRasco, Ben Hall, OrbMan).
I have spent the last few hours trying to understand the pros and cons of jQuery, Prototype, and MooTools to see which is easier for me to implement and best suits my needs. I have been successful in getting a very basic form page behaving close to what I want with jQuery. It carries out the process and never leaves the form page, so it is options 2 and 3 together I think. Now I just have to figure out a bit more about how the $.ajax call actually works and where I can get the responseText from if I need it. I can look that up though :)
So thanks again all.
What are you supposed to do on here when more than one answer pointed you in the right direction?