PHP Selenium WebDriver using sendKeys causes redirect - php

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.

Related

Wordpress stop multiple form submission

I am making a simple form with a submit button inside of it. In order to do this I used a shortcode which returns the form. I would like to prevent the user from submitting the form multiple times. However, the answers I have found tend to rely on disabling the button using javascript in some way. If I understand correctly this has two problems.
If the user stops the page, the button will be permanently disabled.
The client code can be changed manually by the user to send multiple requests even if the button is disabled.
I was wondering if there is some way to handle this on the server side in php?
The main problem I am having is that the shortcode needs to return something. It is possible to store a unique code using a nonce, the database or other methods to uniquely identify the submission. However, I can't find a way to cancel execution before the shortcode starts.
Also, when I was trying things I found that if I sleep the function so I can spam the submit button, only the final shortcode submission is returned. So I can't simply return a working copy of the form either.
It seems this would be a standard problem, so am I missing something?

PHP - Browser BACK button crashes the website

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.

Get request on button click

here is the challenge, I have a webpage, with a form in it , and the button that corresponds to it of course causes a submit. The other button that I have, essentially must trigger a database call that displays content from the database, on this page.
So technically my mind understands that this button should send a GET request to the server and then I retrieve the necessary information and it is displayed. But syntactically, I do not understand how to pull this off, one way to go about doing it is to encapsulate this button in another form, that has a method called GET, and in my server side, I retrieve my information, encode it and send it back to the client. But the idea of encoding this inside a form does not appeal to me as this isn't a form, all I really want to understand is how I can trigger the GET method by clicking a button.
Thanks!
There are two simple ways to send a GET request. One is as you figured out, a new form that has that button as an input and nothing is wrong with that so I'm not sure what is not appealing to you. The second way could be via link, for example:
Link

Saving Form Data?

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.

PHP MYSQL Updatable Form

I am trying to create a form that you can enter data into mysql using php and then it will update the table on the same page instantly here is what I have
Index_test.php: http://pastebin.com/03fndSHG
Update.php: http://pastebin.com/jQraSskS
index_style.css: (http://)pastebin.com/PhYxttFu
When I submit this I get a double entry in my form and I want the update to be seamless like on the index_test.php it shows a line that says "An entry has been added" and the table refreshes automatically without any movement to another page. I ahve tried finding something with Ajax but nothing I try works
If I were doing this, I would use jQuery for the ajax. There's lots of documentation here: http://docs.jquery.com/Main_Page. It simplifies ajax, if you know how jQuery works. To use jQuery, you'll need to know some javascript as well. Without jQuery, just javascript & php is enough, but trickier because internet explorer does ajax differently than the other browsers.
Without ajax, you should probably submit the form to the same page as the form, which then redraws itself with the new table row. Even with ajax, if the user turns off javascript, then the form needs to submit to the same page, or another page that has the form in it. This is known as progressive enhancement or graceful degradation, meaning, the web page still works if javascript is disabled.

Categories