Use formvalidation on entire form after page reload from submit - php

I'm coding some basic validation (mainly for aesthetic purposes, will do the main stuff in PHP) with formvalidation, and I've noticed a big problem in that once I hit submit, if it goes back to the same page, all the fields are no longer validated. I've tried a couple of lines of code from googling the problem, but none seemed to work.
Here is one line that seems to run on the datepicker alright when you use it, but if I just stick it out in the option for the document ready function (I just use trial and error with jquery), it does nothing.
$('#main_form').formValidation('revalidateField', 'pcn');
From here I tried this:
$('#main_form').validate().form();
But for some reason the entire validation breaks when I use that.
Here is a jsfiddle using the jquery code I'm using, what I basically want is it to instantly say that the field is required when you hit run.
Alternatively, can I set it to default to a certain message? That way I have PHP validate it on page load.

Finally got it working after a lot of trial and error. It turned out to be quite a simple fix, but needed to be in the correct place.
Add this line below all the validation code:
$('#form_id').formValidation('validate');
I'm hiding it behind if(isset($_POST['submit'])), so it'll revalidate the form on submit, but not validate when you first load.

Related

Contact-Form 7 reloads page instead of doing AJAX

I am having my first go at converting a HTML template to a custom WP theme, so far everything is working out quite well and it's a very nice learning process..
I am using Advanced Custom Fields with Advanced Custom Fields: Contact Form 7 plugin. And set it to display in the following way:
<?php echo do_shortcode( '[contact-form-7 id="135" title="contactform"]' ); ?>
Everything is displaying the way it should, however when I press submit (if filled) it seems to reload the page while adding the following to the URL:
/?_wpcf7=135&_wpcf7_version=5.1.6&_wpcf7_locale=da_DK&_wpcf7_unit_tag=wpcf7-f135-o1&_wpcf7_container_post=0&your-name&your-email&your-phone
If it's not filled it just reloads the page.
This indicates that something doesn't work the way it should, since CF7 normally comes with error (if not filled) or a success message (if filled).
Hope someone can figure this one out.
UPDATE
I managed to it from reloading, the reason was that the php statement was wrapped in
<form></form>
However now it doesn't submit, I am only left with a loading icon which turns around. I looked into what the error might be and got the following:
Error
UPDATE - PROBLEM IS MAYBE FIXED
So after looking up solutions I found out that having permalinks settings on postname instead of normal caused the error. I am running a local install however I am not sure how it will react when it comes lives. Anyone who has a solution or knows whats causing this?

PHP Page -- Cache Issue

(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.

PHP Selenium WebDriver using sendKeys causes redirect

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.

jQuery Mobile Form Validation with PHP Failing

I'm trying to add some validation to some form fields in a jQuery Mobile site I'm building with a PHP back end.
I've done this in the past successfully using the jquery.validate plugin but I'm having trouble getting this to work with jQuery Mobile.
The validation is working in this jsFiddle page:
http://jsfiddle.net/GeX5C/5/
but I'm having trouble getting it to fire when I click the submit button from the hosted PHP page. From what I've read I can't use the usual:
$(document).ready(function(){
approach but not sure what to replace this with?
Would really appreciate any assistance here.
Thanks
2 options, in the event document ready is not supported for whatever reason..
one try the load event on the window, which will wait til everything is loaded and completely rendered, similar to the ready function but waits just that much longer course im paraphrasing but hopefully you get the point.. its applied the same as the ready function.
$(window).load(function(){
// Your code here
});
second option is dont use any load/ready. just make a function that fires off as the final line of your code, should whatever your doing truely not require the page to actually be fully rendered to run. logic to this is make a function then in the last lines call the function.
also try to keep a mass majority of your javascript especially pieces that require other things to load first at the bottom of your page just above the final body tag. Yea theres hot debate over this some people insist top is better while others insist bottom.. me Im a bottom guy think about the logic, when downloading the page downloads top to bottom in a sense correct? so.. put the heavier of the weight at the bottom such as external scripts. anyway im getting off point, hopefully this helps some what.
FYI this is js code that works for the form validation:
$('#id-for-the-page-element').live( 'pageinit',function(event){
$('#id-for-the-form').validate(options);
});
Hope this helps someone else.

Curl or Ajax to display only captcha?

I'm trying to make a php script, which would allow me to auto submit a form to a page.
Something like that can be achieved pretty easy with Curl.
But now I'm facing a challenge.
I want to save me some time, so I thought I could make a php script, that would only show me the CAPTCHA. I would only have to write in the CAPTCHA and the script would auto submit it together with all the other form elements.
I actually have no idea where to start. I thought about using Curl, but I don't know if it's possible to use the same resource across different pages. After all, I would have to log in, get the CAPTCHA image and display it (that would be the first part). After I input the CAPTCHA I would have to submit the form (the second part).
Would JavaScript be better suited for such a project? And if so, where can I find an explanation on how to use JavaScript to achieve Curl like functionality? After all, that wouldn't be just an easy AJAX call, I would need to keep the Session open, else the CAPTCHA wouldn't be the same anymore.
I searched everywhere in the hopes of finding something, but I didn't find anything.

Categories