Using AJAX to check database for valid value
PermalinkI have 1 field on a form in a single page. I would like the user to be able to enter a "code" into the field, submit it, and then without changing the page, check if that code is in a database, and if so, load the next page, if not, display a message somewhere on the same page indicating so.
Can someone please help point me in the right direction? I have tried using an ajax call for when the form submits, and then from there calling the single page's controller method of choice, but it will return the entire html page, rather than a true or false.
Any direction would help! Thanks!
A couple questions if you could help.. The it works doing it this way, when the code is found, and you do
$(".theForm").submit();
It starts a loop because submitting the form also calls the ajax again, thus it keeps happening over and over again. What could I do to submit the form without calling the ajax function again?
That could be it for now, I will have to figure out how to do the whole finding the tools folder instead of just hardcoding it like I am right now in a local dev environment, but that's future me problems :)
Best Wishes,
Mike
if (ajax_request) { return true; } else { ajax_request = true; }
So that the next time it will run the default submit, but I am running into a problem still. With my code, when submitting, if it finds the code, it will submit again, but it isn't actually submitting. You have to physically press submit again for it to go (in which case it works fine).
Here is my code
$(function() { var ajax_request = false; $(".theForm").submit(function(e) { if (ajax_request) { return true; } else { ajax_request = true; } e.preventDefault(); $.ajax({ url: 'http://localhost:8888/concrete5.6.0.2/index.php/tools/ajax_code_check', statusCode: { 200: function() { //alert('Found Code!'); ajax_request = true;
$(function() { $(".theForm").on('submit',function(e) { $.ajax({ url: 'http://localhost:8888/concrete5.6.0.2/index.php/tools/ajax_code_check', statusCode: { 200: function() { $(".formerror").hide(); $(".theForm").off('submit').submit(); }, 204: function() { $(".formerror").show(); }, 404: function() { alert('Page Not Found'); }
<script> $(function() { $(".theForm").submit(function(e) { e.preventDefault(); $.ajax({ url: 'http://localhost:8888/concrete5.6.0.2/index.php/tools/ajax_code_check', statusCode: { 200: function() { $(".formerror").hide(); $(".theForm").unbind().submit(); }, 204: function() { $(".formerror").show(); }, 404: function() {
When working wih AJAX-y stuff, the "concrete5 way" these days is to create yourself a tools file. The big perk of a tools file is that you get concrete5 all bootstrapped with all the libraries etc loaded up and ready to go.
Let's create a tools file
/tools/check_it_out.php
In that file let's find something in the database and return a result.
Ok, we have our new fancy database checked loaded up with a couple HTTP reposonse codes.
Let's write some javascript!
Now the tricky part here is that we don't have a javascript way of grabbing our tool's location. If you just have a single site at the root of the domain you can get away with just linking straight to it. Otherwise you will need to inject some php in there with a concrete5 library call to get the tools directory. Let's just pretend this is a one off for a site we're building to make things easier.
Anyhow, probably a typo in there. Hope it helps.
Best Wishes,
Mike