Welcome Guest, Not a member yet? Register   Sign In
On-the-fly validation with AJAX; how to run javscript code if PHP returns true.
#1

[eluser]loathsome[/eluser]
Hi guys,

We're having some issues with an application we're currently developing using CodeIgniter and jQuery. What we currently want to achieve is the following: user input data to a normal input field. This data is validated on the fly against an external ORACLE-database using the ajax-technology (keyup), and if PHP returns true on the data, I want a dialog to appear (for example ui's $dialog()) or some other javascript function to be ran. How shall we proceed with this? I've tried outputing [removed]-tags directly with php, but without luck. I've also had a look at JSON, but I'm not sure exactly how to utilize it correctly.

To break it down; input data into form, validate using ajax and php, if php says "ok", I want another javascript function to run.

Appreciate all your input, folks! Thanks.
#2

[eluser]Randy Casburn[/eluser]
So getScript won't work for you?

Randy
#3

[eluser]kgill[/eluser]
Have you looked at the documentation for the jquery post function, specifically callbacks? Post your data to your PHP validation have it return your "ok" as simple text, JSON or XML - whatever floats your boat, then you simply hand that returned data to your callback to process and it decides whether or not you call your other js function.
#4

[eluser]Nick Husher[/eluser]
Using jQuery, you'd do something like this:

Code:
jQuery('.my-form-field').keyup(function() {
    var request = {
        'fieldName': this.name,
        'fieldValue': this.value
    };
    
    jQuery().post('/ajax/validateField', request, function(response) {
        if(response.result == true) {
            jQuery('.my-form-field').css('color','red');
        } else {
            jQuery('.my-form-field').css('color','');            
        }
    });
});

On the server side PHP, you'd do something like this in your controller named Ajax:

Code:
function validateField() {
    $fieldName = $this->input->post('fieldName');
    $fieldValue = $this->input->post('fieldValue');

    // validate here and store boolean to $validates
    
    if($validates) {
        echo json_encode(array('result'=>true));
    } else {
        echo json_encode(array('result'=>false));
    }
}

(code untested)
#5

[eluser]Randy Casburn[/eluser]
That's all already been done Nick....

Quote:To break it down; input data into form, validate using ajax and php, if php says “ok”, I want another javascript function to run.

the OP just wants to execute a callback upon successful CI (or PHP) validation.

My suggestion was to send the script back using the jQuery getScript method and kgill offered a standard callback. Either of those will work fine.

Randy
#6

[eluser]simshaun[/eluser]
Just as a side note, echo'ing directly inside the method won't work if you have the compress_output feature enabled. In that case, you need to use CI's set_output function.
#7

[eluser]loathsome[/eluser]
Thanks, guys! I got it solved. I ended up using json_encode on an array, passing it over to the javascript, then check the values on callback and execute actions from there. I thought of this problem way too advanced.

Basically, it looks something like this

Code:
// ajax stuff
onSuccess: function(win){
  if(win.json.RESPONSE == 'NOT_IN_DB'){
    $('#dialog').dialog();
    // or whatever I want to do
  }
}

Couldn't have done it without you, highly appreciate it.
#8

[eluser]Nick Husher[/eluser]
Quote:That’s all already been done Nick... the OP just wants to execute a callback upon successful CI (or PHP) validation.

Which, if you read the script I posted above, is exactly what's going on--I'm surprised you didn't get it. The code listens for an onkeyup on the relevant form element(s) at which point it sends a post request to the server, the server responds some number of milliseconds later, and the anonymous function on the client (i.e. the callback) performs an action (turns the input field text red).

You could easily replace this with a global custom event that an arbitrary number of event listeners could subscribe to, or include some logic in the closure to execute a certain appropriate function with the given scope.




Theme © iAndrew 2016 - Forum software by © MyBB