Welcome Guest, Not a member yet? Register   Sign In
ajax query database with textbox data and show results
#1

[eluser]mandril[/eluser]
Hi there!

Im porting an application, its the old style php embedded in HTML untidy programming u.u

What i need to do is check if a user exists on the database with his ID typed on the textbox, using the onblur javascript event.

THE OLD CODE I WANT TO PORT...

The input: cedula or ci = ID , sry for spanish names Sad

Code:
< input type="text" id="txtCedula" name="txtCedula" class="jsrequired" o n b l u r = " g e t R e a f i l i a c i o n ( '../modulos/scriptReAfiliacion.php?ci= '  +this.value ) " / >

Ok it calls a javascript function with the URL of the *logic* that gets the ci value and check in DB if the user exists or not.. and then it shows the result on a div.

Javascript file

Code:
function getXMLHTTP() {
        var xmlhttp=false;    
        try{
            xmlhttp=new XMLHttpRequest();
        }
        catch(e)    {        
            try{            
                xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(e){
                try{
                xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
                }
                catch(e1){
                    xmlhttp=false;
                }
            }
        }
            
        return xmlhttp;
    }
    
    
    
function getReafiliacion(strURL)
{        
    var req = getXMLHTTP();        
    if (req)
    {
        req.onreadystatechange = function()
        {

            if (req.readyState == 4)
            {            
                if (req.status == 200)
                {                        
                    // the div where the  info is shown
document.getElementById('lblAdvertencia')[removed] = req.responseText;                        
                }
                else
                {
                    alert("Hubo un problema con ajax y el objeto XMLHTTP:\n" + req.statusText);
                }
            }                
         }            
         req.open("GET", strURL, true);
         req.send(null);
    }            
}

And this is the PHP file with the Logic

Code:
<?php
require_once("Programa.php");

$bd = new Mysql5();
$bd->Conectarse();

$ci = $_GET['ci']; //here it gets the ID value from the javascript

//it query for user existence
$resultado = $bd->ConsultaSQL("SELECT id FROM socio WHERE cedula = '".$ci."' AND estadoBaja = -1");

$row = mysql_fetch_array ($resultado);
        
//if it doesnt exists i show an error message... that javascript puts in the div lblAdvertencia

if($row[0] != NULL){
    echo "<b><font color='red'>El socio de la cooperativa esta dado de baja!</font>[ Reafiliar ]</a></b>";
}
?&gt;


Ok so in CodeIgniter what I've done is a model to handle the bd operations and a controller to connect the view and the model.

My controller:

note that socio is like : user in english Tongue

Code:
class socio extends Controller
{

    function __construct ()
    {
        parent::Controller();
        $this->load->model('socio_model');
    }
    
    function index(){}
    
//nuevo is link new.. this method shows the form of user registration were i have to check if the ID is already taken or not
    function nuevo()
    {
        $config['title'] = 'CAOMA 2.0 \ Socio Nuevo';
        $config['header'] = '[removed][removed]';
        $this->load->view('socio_ingreso_form',$config);
    }
    
    //this is the method that connects me with the model that gets the data from DB
        //this medoth recievs the ID($ci) and a optional parameter because im going to use it for non-ajax also :P
    function reafiliar($ci,$ajax = FALSE)
    {
        if ($ajax == TRUE)
        {
                        //ok if its called from ajax go to model and get the info
            $config['reafiliar'] = $this->socio_model->getReafiliacion($ci);
        }else{
            $this->load->view('socio_reafiliar_form');
        }
    }

The model

Code:
class socio_model extends Model
{
    function __construct()
    {
        parent::Model();
    }
    
//this method does the logic of checking in db if the user exists..
//if it does it returns " " if not it returns the message to show.
    function getReafiliacion($ci)
    {
        $this->db->where('cedula',$ci);
        $this->db->where('estadoBaja',-1);
        
        $this->db->select('id');
        $query = $this->db->get('socio');
        
        if(!$query->num_rows() == 0)
        {
            return "<b><font color='red'>El socio de la cooperativa esta dado de baja!</font> [ Reafiliar ]</b>";
        }
        return "";
    }
}


Ok this is my code but i dont know how to relate with javascript in the user registration form (socio_ingreso_form) . In the old one i send data using Post.

I cannot call my : function reafiliar($ci,$ajax = FALSE) and send the variable from the input onblur event , or can I ? :S

Im newbie using javascript and stuck here with this.. need a solution please help me Smile

Thnx a lot =)
#2

[eluser]mandril[/eluser]
sorry i re-post this here:

http://ellislab.com/forums/viewthread/103397/




Theme © iAndrew 2016 - Forum software by © MyBB