2015-05-18 97 views
0

我正在使用PHP(buscador.php)的搜索頁面,我需要的是顯示(總是在同一頁面buscador.php中)員工信息取決於類型ID,因爲我編碼了一個JQuery函數使用$ .ajax爲了通過AJAX和JSON格式獲得數據(數據庫查詢編碼在process.php)

我的問題是,當我定義的ID,然後點擊提交按鈕,它重定向我process.php,而不是告訴我裏面buscador.php特殊區域的信息,該信息顯示process.php是:

{"users":{"status":"OK","0":{"ID":"001","Nombre":"algo","cargo":"algo cargo"},"1":{"ID":"PHP001","Nombre":"Pablo Ernesto Tobar Mayora","cargo":"Web Programmer"},"2":{"ID":"PHP002","Nombre":"Pabletoreto Blogger","cargo":"Blogger Manager"},"3":{"ID":"PHP003","Nombre":"prueba de stored procedure en MySQL","cargo":"Database Administrator"},"4":{"ID":"PHP004","Nombre":"Prueba de funciones en MySQL","cargo":"Database Tester"}}} 

這不是結果我'或者你可以看到對數據庫的查詢已經完成,除此之外,信息以JSON格式顯示,但我無法在buscador.php頁面中顯示該信息,請問我是誰做錯了,請,我的整個代碼是:

buscador.php

<html lang="es-ES"> 
<head> 
<meta name="tipo_contenido" content="text/html;" http-equiv="content-type" charset="utf-8"> 
<link type="text/css" rel="stylesheet" href="content/estilos.css"> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 
<script src="javascript/jquery.js" type="text/javascript"></script> 

<title>BUSCADOR</title> 
</head> 
<form method="post" id="form_id" action="process.php"> 
<fieldset> 
<legend> Buscador Asincrono</legend> 
<p>ID a buscar: <input type="text" name="ID_name" id="ID_id"/><div id="estado_id"></div></p> 
<p><input type="submit" id="submit_id" value="Buscar"/></p> 
<img src="imagenes/cargando.gif" id="LoadingImage" style="display:none" align="center"/> 
<div id="ajax_id"><b>Person info will be listed here...</b></div> 
<div id="msg"> 
<table id="infoemp" border="1" style="display:none" align="center"> 
<thead> 
<th>ID</th> 
<th>Nombre</th> 
<th>Cargo</th> 
</thead> 
<tbody></tbody> 
</table> 
</div> 
</fieldset> 
</form> 
</html> 

的jquery.js

$(document).ready(function() { 
    $("#form_id").submit(function(e){ 
    e.preventDefault(); 
    if(validaForm()){ 
     requestInfo(); 
    } 
    }); 
}); 

function validaForm(){ 
     var id_val = $("input#ID_id").val().trim(); 
     //var id_val = id.val().trim(); 
     if((id_val=="") || id_val.replace(/s+/,'') == ''){ 
      alert("Favor ingrese el ID"); 
      $("input#ID_id").addClass("posicionamiento"); 
      $("#ajax_id").html("<div class='error'>Debe especificar el nombre</div>"); 
      return false; 
     }else{ 
     $("input#ID_id").removeClass("posicionamiento"); 
     $("#div_id").empty(); 
     } 
     return true; 
} 

    function requestInfo(){ 

    $("#submit_id").hide(); 
    $("#ajax_id").html(""); 
    $("#LoadingImage").show(); 
    $("#ajax_id").html("<div class='cargando'> realizando busqueda</div>"); 

    var url = $("#form_id").attr('action'); 
    var data = $("#form_id").serialize(); 
    var type = $("#form_id").attr('method'); 
    alert(url); 

    $.ajax({ 
    url:url,   
    data:data,  
    type:type,  
    cache: false, 
    contentType: "application/x-www-form-urlencoded", 
    dataType: 'json', 
    encode: true, 
     }); 

.done(function(data) { 
if(data.status == "OK"){ 
$("#submit_id").show(); 
$("#ajax_id").html(""); 
$("#LoadingImage").fadeOut(); 
$("#infoemp").show(); 

$.each(data.users, function(i,user){ 
var tblRow = 
"<tr>" 
+"<td>"+user.ID+"</td>" 
+"<td>"+user.Name+"</td>" 
+"<td>"+user.cargo+"</td>" 
+"</tr>" ; 
$(tblRow).appendTo("#infoemp tbody"); 
}); 
} else { $("#ajax_id").html(data.status).addClass("cargando"); } 
}); 

.fail(function(jqXHR, textStatus, errorThrown) { 
    if (console && console.log) { 
     console.log("La solicitud a fallado: " + textStatus); 
    } 
    }); 

    } 

process.php

<?php 

$bd = "ejemplo"; 
$server ="localhost"; 
$user = "root"; 
$password = ""; 

if (isset($_POST['Submit']) && isset($_POST['ID_name'])) { 
    $valor = filter_var($_POST['ID_name'], FILTER_SANITIZE_STRING); 

    $var = array(); 

    if ($valor == null) 
    $var["status"]="ERROR"; 
    exit(); 
    } else { 

    $mysqli = mysqli_connect($server, $user, $password, $bd); 
    if(! $mysqli) die("Error de conexion ".mysqli_connect_error()); 

$var["status"]="OK"; 
$sql = "SELECT * FROM empleado_php"; 

$result = mysqli_query($mysqli, $sql); 

while($obj = mysqli_fetch_assoc($result)) { 

$var[] = $obj; 
} 

$mysqli->close(); 
    } 
echo '{"users":'.json_encode($var).'}'; 
header('Content-type: application/json; charset=utf-8'); 

?> 
+0

爲什麼你這樣做'echo'{「users」:'。json_encode($ var)。'}'? – Akar

+0

您已經在阻止默認操作。有沒有錯誤? –

+0

你好,是的,我已經阻止了默認的動作,我不明白爲什麼它重定向到process.php –

回答

0

你有JS錯誤。

刪除後在這個片段中})分號:

encode: true, 
 
     }); 
 

 
.done(function(data) { 
 
if(data.status == "OK"){

而對於這個片段做同樣的,後刪除分號})

} else { $("#ajax_id").html(data.status).addClass("cargando"); } 
 
}); 
 

 
.fail(function(jqXHR, textStatus, errorThrown) { 
 

這將解決這個特定的問題,如果你遇到PHP錯誤或更多的JS錯誤,而循環請先谷歌你的問題,並張貼在這裏,如果你無法找到它。另外,請嘗試this question以瞭解如何查找JS錯誤。注意:我剛纔看到你在發佈答案之前已經找到了如何找到JS錯誤。好工作:)

+0

謝謝@ rick6我已經刪除了)};但沒有解決這個問題,我已經GOOGLE了,但沒有找到答案,但將繼續搜索 –

+0

@PabloTobar爲兩者做。那就是問題所在。 – rick6

+0

@PabloTobar只刪除分號,而不是整個圓括號和括號。你太快了。慢下來,看我的答案:) – rick6