Accessing database object after AJAX call

Permalink
I would like to connect to a mysql database after a form submission. The form is handled through an AJAX call. I'm directing to a single page called "handlesearch". I added the single page like told in the documentation.
After form submission the message "Access Denied." was returned to me. Any help? Thanks in advance!

AJAX call
$("#searchform").submit(function() {
    var url = "single_pages/handlesearch.php";
    $.ajax({
           type: "POST",
           url: url,
           data: $("#searchform").serialize(),
           success: function(data)
           {
             alert(data); 
           },
           error: function()
           {
             alert("Error!");
           }
         });


Single page named: handlesearch
<?php defined('C5_EXECUTE') or die("Access Denied."); 
$db= Loader::db();
?>

GreyhorseDesign
 
JohntheFish replied on at Permalink Reply
JohntheFish
What you have will try and use the single page view method. The view method tries to render the whole page with a full c5 environment.

The closest to what you are doing is to ajax call an actionXX method in the single page controller. This will just respond to the specific method in a minimal environment.

You could also look up making ajax calls to a tools file (which is my usual preference)

There is more in the documentation and howtos. There are some working examples in my AJAX lessons howto and associated addon - a bit tired these days, but still basically valid.

Depending on the context, you may also want to look up using the validation helper to validate a token.
GreyhorseDesign replied on at Permalink Reply
GreyhorseDesign
Thanks for the quick reply.
I've looked into the documentation (JavaScript, jQuery and concrete5) that you suggested. But when simply running the following line in a tool file:
defined('C5_EXECUTE') or die(_("Access Denied."));


I get the message "Access Denied." There must be something i'm missing here..
JohntheFish replied on at Permalink Best Answer Reply
JohntheFish
Most likely guess, on minimal information, is that the tools url needs to be generated with the c5 helper. That way it will be processed by the dispatcher and c5 knows how much environment to set up for it.
GreyhorseDesign replied on at Permalink Reply
GreyhorseDesign
Well i tried using the URL:
/tools/required/handlesearch.php

Instead of:
/tools/handlesearch.php

This does works..
titanve replied on at Permalink Reply
titanve
Dear Gregory,

How did you do in order to make that /tools/required/....php call work?
What should I do with my tools php file??

Thank you in advance