Is there a way to save the form data so that if the submit fails they don't have to retype everything? For some reason, I'm getting an error on my script with Chrome but not with FireFox. It doesn't always happen with Chrome, and it's not exactly the main problem. If they submit and then the page doesn't load, they lose everything they typed when they go back. I've thought of only one thing so far, but it doesn't seem practical. It would be to save the form data to cookies when they press submit.
Is there a better way?
It depends on what you mean by "submit fails." If you mean that there is a server error, and they have to press 'back', it's hard.
However, if you mean fails as in a they-forgot-to-put-an-email kind of fails, here is one solution:
You can grab the previous values from the $POST or wherever and stick them back into the HTML tags. For instance:
<?php
$prevCustEmail = makeItASafeString($_POST['custEmail']);
?>
<input type="text" id="custEmail" name="custEmail" value="<?php echo $prevCustEmail; ?>" />
Edit: For the server thing, the cookie hack may be the best. Here is another hack idea: You could modify your 500 server error page to have a button on it that says "go back to form", which would only show up in the event that this $_POST data was just went. The button would actually be submitting an identical form where every is type=hidden. This data would then get transferred to the previous page and stuffed in as above.
A third hack would be to load the page in an iframe, and have the success page jump out of the iframe. The server error would not do that. Then use Javascript onSubmit to detect that something has gone wrong, or just hope that the user notices nothing happened and clicks "submit" again.
Related
I'm having a problem trying to submit the form automatically using php-webdriver client, everything went fine until one day my script was no longer able to submit the form. The process of filling form is really simple:
I have my fields that I want to populate in that form and click submit button with attribute name="submit". Everything worked fine but now the same code somehow doesn't cut it.
$input = $this->driver->findElement(WebDriverBy::name('first_name'));
$input->sendKeys('Example');
$submit = $this->driver->findElement(WebDriverBy::name('submit'));
$submit->click();
This is pretty much what my script does it finds a few fields and sends some values after that I simply click the submit button. After the submission, I checked what response I get using getPageSource() method, and surprisingly it showed me different page, but in the right circumstances it should be on the same page. If I delete every line of code that uses sendKeys() method then it stays on the same page which is the correct behavior. But I'm not understanding why sendKeys in combination with $submit->click() causes something like 'redirect' it redirects to index page which is initial website page and it used to work correctly, I checked everything many times and it looks that I provide all required fields correctly and able to submit the form. But instead of successful form submission response, I see that I'm on the index page.
Maybe there are known issues with this but I'm just not aware of it? Some advice, ways of how could I debug it would be really helpful because now I'm clueless, there's simply no indication of what went wrong.
Problem:I have an RSS feed. As some of you may know, RSS feeds do not always update promptly (I'm using FeedBurner) so I'd like to provide the option on my webpage to update the RSS feed. This is a simple process, and I just need to ping an address. The catch is this: I'd like to stay on the initial page, and ideally refresh it.
I've seen some "solutions" around with using hidden iframes, and javascript, Ajax, etc.. What I am wondering is if there is an elegant way to do this using php/html.
Below is a flowchart illustrating exactly how I would like the system to function.
EDIT:
Here is the simple form code which I currently have:
<form action="http://url.to.ping" method="post">
<input type="submit" value="Refresh" />
</form>
This is a standard form, performing an action on submit. I require now that the browsers destination (as seen from the user) is a different url than that in the action. It is worth noting that the action page is not in my domain, and is not part of a domain which I own or have access to.
Thanks!
What i meant was,
/contactme.php
once they've submitted and come back to the page is there any additional variables like
/contactme.php?thanks=1
basically is there anything to declare they have just submitted and come back to the original page, if so..
You could do;
<?php
if(isset($_GET['thanks']))
{
$pingServer = file_get_contents('http://www.the.server.to.ping.com/pingit.php');
unset($pingServer);
}
?>
at the bottom of the page and it'll just hit that page.
This way you are not relying on JavaScript being enabled and the user is not hopped around multiple URLs.
What I have done when I needed the landing page to be different from the processing page is add a JavaScript redirection where one would put their "thanks for filling out my form" material.
So, the code process would be:
user fills out form, clicks submit
server-side validation and processing.
if success then location.href(URL, 0); else do error case
user is redirected to new URL (your refresh page)
I am a newbie to programming. I have a PHP website which works as follows
Index Page - Search Results - Show a Product
The site user enters search critera on Index Page and the page is POSTed to Search Results page. From there, the site user clicks on a Product href that takes him to the Product Details. This is working fine till here.
The problem occurs when the user click the browser BACK button. The Search Result page comes up totally crashed and the user has to press F5/Browser Refresh to re-submit it. Any idea/technqiue that I can use to avoid this crash?
When a browser goes back to a page that comes from POSTing some data, the browser often times needs to re-POST the data in order to get the same page back. Since that can sometimes be bad (e.g. re-POSTing an order form), many browsers require the user to force a refresh with a warning.
You can generally use a GET instead of a POST form to avoid this.
An idea would be using GET for the method of your search form instead of POST (that apparently you are using). That way, even if going back in browser history, your server could re-supply its search results.
You would need the following:
change method="post" to method="get" in your search form
change every $_POST relating to the search form data to $_GET in your search form processing php file.
Of course, it could not work for your specific usecase. That's just an idea.
I have a webpage that contains a form, and i need to refresh the page right before the form is submitted. Is there a way to have the Submit button refresh the current page and then submit the form to the proper php document?
I am using a general form with the submit button looking like
<input name="Submit" value="Submit" type="submit" />
Fix the page - do not pursue making this problem larger than it already is. I think it's some tunnel vision, you've made some mistakes on how you have setup your form or page, and now you are trying to continue down the wrong path instead of correcting your original problem.
Refreshing the page to submit some javascript data and then submit the form etc... is incorrect and needs to be fixed.
Best of luck and happy coding!
You can give the form a target to submit it in a new window and bind an event handler to it that refreshes the page on submit.
Please clarify what exactly you're trying to achieve. If you're trying to get some data from the server that's required to submit the form, it's possible to do (though it's not the best way to handle it). You'll need to intercept form submit, stop the events, perform an AJAX request to the server and then submit the form from code.
I've looked around for an answer to this problem, and I'm slowly accepting the fact that I've probably gone about things the wrong way, but here's a request for some wonderful pearl of wisdom that might help!
I'm working on a website which has a directory-style navigation bar on the left. So at first the user will only see two directories (Grammar / vocabulary - it's an English learning site) and by clicking on the directory, it will "open" to reveal its contents (other pages or directories).
The way I have this working is that each directory is a form submit button that reloads the page when clicked, but with a value in $_POST that tells the site to open or close a folder (this is done within an array in $_SESSION). It works great! The issue, though, is that if a user clicks the back button in their browser, they get the prompt asking if they wish to resubmit data, which is not something I want.
I was wondering if there was a way I can detect in php if the back button has been pressed, so that instead of reloading the page and making changes to the side bar, the side bar will remain the same and instead the user gets taken to the last page they had visited.
I'm aware that the real solution to this is probably just to rethink the side bar, and any advice on a better way to do that would be much appreciated also!
Michael
The reason you're getting the prompt is because POST is supposed to be used if the data being sent is going to modify something, so submitting a POST request using the back button might have unexpected behaviour (like duplicate data).
Replacing POST with GET should disable the prompt.
HTML
From:
<form action="page.php" method="post">
</form>
To:
<form action="page.php" method="get">
</form>
PHP
Change references from $_POST[''] to $_GET['']
DEMO: http://aseptik.net/php5/demo/deal-with-undesirable-behaviour-when-clicking-the-back-button-on-my-website
<?php
session_start();
if( $_GET['t'] == $_SESSION['token'] || $_SESSION['directory'] == $_GET['q'] || !isset($_GET['q']) ) {
echo "<h1><span>you going</span> forward</h1>";
} else {
echo "<h1><span>you going</span> backward</h1>";
echo "<p>should be <strong>{$_SESSION['directory']}</strong> directory</p>";
}
$token = uniqid();
$_SESSION['token'] = $token;
$_SESSION['directory'] = $_GET['q'];
?>
grammar
vocabulary
Back button re-submit form data ($_POST)
Prevent Back button from showing POST confirmation alert
As a general rule in web development you should only do a post when you are submitting data that you don't want to be resubmitted, or that you don't want to show up in your logs on the server. And once you do a post, you should redirect using get parameters or some other method.
If you use get parameters instead of post then you can just make the page navigation stateless and just fetch the page they ask for.
There are methods for detecting if the back button has been pressed, but they are difficult to make work consistently, and can be frustrating to the user, so it is recommended you go about it another way.