Direct query in a form

graaooor 03 Aug, 2016
Hello,
I have a simple form with a dropdown box and a submit button.
When the form is submitted, I make a request to find records in a database who match the selection.
Simple.

Now, I want to do the same thing but I want to execute a database query each time the user selected a value.
The result will be displayed in a div.
This new form is more complex, with a lot of dropdown, radio box, text box
I need to stock all the values selected in a session.

For example, my form has a dropdown.
If I choose the first value, a query search the number of matching records and displays the result in a div.
If I choose the second, a new request and displays the result.
There is no submit button in this form.

How can I do this ?
graaooor 03 Aug, 2016
Thank you for this answer but this doesn't solve my problem.
I just want to have an example, or some solutions to do this.
When a dropdown value is selected, I want to interrogate the database and show the result in a div, in the same form.
GreyHead 03 Aug, 2016
Hi graaooor,

I think that you'd have to custom code this using JavaScript AJAX queries to send the selected data back to a form event and then custom PHP in the form event to get 'the number of matching records' and return it to the form.

Bob
graaooor 03 Aug, 2016
Hi GreyHead,
Have you an example to do this ? I tried a lot of solutions but nothing works.
Graaooor
GreyHead 03 Aug, 2016
Hi graaooor,

Not that I recall, there may be something in the forums but this is not a common request.

What is the code you have tried?

Bob
graaooor 03 Aug, 2016
My form is like that (form01 and form02).
The first javascript :
// handles the click event for link 1, sends the query
function getOutput(str1,str2) {
alert("STR1="+str1); // Here I have the correct value of the select
  getRequest(
      'index.php?option=com_chronoforms5&chronoform=testfp2&event=page2&type=submit', // Call Page2
       drawOutput,  // handle successful request
       drawError    // handle error
  );
  return false;
}  
// handles drawing an error message
function drawError() {
    var container = document.getElementById('output');
    container.innerHTML = 'Bummer: there was an error!';
}
// handles the response, adds the html
function drawOutput(responseText) {
    var container = document.getElementById('output');
    container.innerHTML = responseText;
}
// helper function for cross-browser request object
function getRequest(url, success, error) {
    var req = false;
    try{
        // most browsers
        req = new XMLHttpRequest();
    } catch (e){
        // IE
        try{
            req = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {
            // try an older version
            try{
                req = new ActiveXObject("Microsoft.XMLHTTP");
            } catch(e) {
                return false;
            }
        }
    }
    if (!req) return false;
    if (typeof success != 'function') success = function () {};
    if (typeof error!= 'function') error = function () {};
    req.onreadystatechange = function(){
        if(req.readyState == 4) {
            return req.status === 200 ? 
                success(req.responseText) : error(req.status);
        }
    }
    req.open("GET", url, true);
    req.send(null);
    return req;
}

The second javascript :
jQuery(document).ready(function($){
    $("#effectif").change(function(){
        getOutput($("#effectif").val());
    });
});

And the PHP code is :
<?php
echo "VAL=[".$form->data['effectif']."]";
exit;
?>

The $form->data['effectif'] is empty. I need this value to construct the query, execute, and return the results.

Is it clear ?
GreyHead 04 Aug, 2016
Hi graaooor,

I can see a few things here:

+ I would use the jQuery methods to write the JavaScript, it is always available on a CF page and can be very helpful. See the Ajax docs here

+ As far as I can see the getRequest function isn't defined (or it's coming from some other library); and doesn't pass any data back to the form event. You have the variables str1 and str2 but they aren't included in the URL or in a data package.

+ the page2 form event is not linked to the current user - it is responding to the Ajax call - so can't usefully load or save data to the session - it only has the data from the AJAX call. So there is no $form->data['effectif'] to return. You can test this with your browser web developer tools. If you use the Network tab in Chrome (or the equivalent in other browsers) you can see what data is being returned.

Bob
This topic is locked and no more replies can be posted.