使用php在DB中搜索的Ajax函数

时间:2016-04-25 23:17:35

标签: php jquery ajax path

大家好我想让这个功能正常工作但不是,我需要的是在我离开1个输入的焦点后,该函数应该在DB中搜索该值,如果存在则返回该人的信息(如果不存在)允许手动填写信息......无论如何这就是我所拥有的:

$('#cedula').focusout(function(){
    var cedula = $('#cedula').val();
    if($.trim(cedula) !== ''){
        $.post('ajax/buscar.php', {cedula: cedula}, function(data){
            if(data !== ''){
                console.log("I found this " + data);
                var mitad = data.split('/');
                $('#nombre').val(mitad[1]);
                $('#telefono').val(mitad[2]);   
                $('#serial').focus();   
                $('#cliente_enc').prop("checked", true);

            }else{
                console.log("I didn't got any data =( for this ID" + cedula);
                $('#cliente_enc').prop("checked", false);
            }
        });
    }
    if($('#cedula').val().length >= 5)
        $('#barra').css('width','15%');
});

我的php buscar.php是这样的:

if(isset($_POST['cedula']) === true && empty($_POST['cedula']) === false){
        $ced = trim($_POST['cedula']);
        require dirname(__FILE__).'/'.'../db/connect.php';
        $statement = $conexion->prepare("SELECT * FROM clientes WHERE cedula = :cedula;");
        $statement->execute(array(
            ':cedula' => $ced
        ));

        $resultado = $statement->fetch();
        if($resultado != false){
            echo $resultado[1]."/".$resultado[2];
        }else
            echo "";
    }

我的connect.php文件:

<?php
use Project\Helpers\Config;
require dirname(__FILE__).'/'.'../app/Config.php';

$config = new Config;
$config->load('config.php');

try {
    //Datos para realizar la conexion
    $dbhost = $config->get('db.hosts.local');
    $dbname = $config->get('db.name'); 
    $dbuser = $config->get('db.user'); 
    $dbpass = $config->get('db.password'); 

    $conexion = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);

} catch (PDOException $e) {
    echo "Error " . $e->getMessage();
}   
?>

和我的Config.php文件:

<?php 
/*Esta clase permite cargar las configuraciones del sistema*/
namespace Project\Helpers;
class Config
{
    protected $data;
    protected $default = null;
    public function load($file){
        $this->data = require $file;
    }
    public function get($key, $default = null){
        $this->default = $default;
        $segments = explode('.', $key);
        $data = $this->data;

        foreach ($segments as $segment) {
            if(isset($data[$segment])){
                $data = $data[$segment];
            }else{
                $data = $this->default;
                break;
            }
        }
        return $data;
    }
    public function exists($key){
        return $this->get($key) !== $this->default;
    }
}
?>

这是config.php文件:

<?php  
return [
    'db' => [
        'hosts' => [
            'local' => 'localhost',
            'externo' => '217.169.87.94',
        ],
        'name' => 'cl55',
        'user' => 'gord',
        'password' => 'Darker1356'
    ],
    'mail' => [
        'host' => 'smtp.gmail.com'
    ]
];
?>

我有数据库和我正在测试ID的人,但我一直在获取空数据......

这是我项目结构的图片

project structure

0 个答案:

没有答案