간단한 검색을 수행 한 후에 값이있는 양식을 채우려하고 있지만 스크립트를 실행할 때 "undefined"또는 빈 응답이 표시됩니다. 경고 (데이터);PHP : ajax를 사용하여 쿼리 결과로 html 양식 필드를 채우는 방법
search.php
<?php
include_once 'config/config.php';
include_once 'config/connect.php';
$conn = dbconn();
if(isset($_POST['btn-search']))
{
$sub_id = mysqli_real_escape_string($conn, $_POST['user_id']);
$sql = "SELECT * FROM users WHERE user_id = '".$sub_id."'";
$res = mysqli_query($conn, $sql);
$row = mysqli_fetch_array($res);
$found = array();
while ($row = mysqli_fetch_array($res))
{
$found[] = array(
'name' => $row['full_name'],
'init_date' => $row['init_date'],
'position' => $row['position'],
'email' => $row['email'],
);
echo json_encode($found);
}
}
mysqli_close($conn);
?>
스크립트
<script>
/*
simple validation script
*/
$('document').ready(function()
{
$('#register-submit-btn').attr('disabled', 'disabled');
/* validation */
$("#register-form").validate({
rules:
{
user_id: {
required: true,
minlength: 5,
},
},
messages:
{
cedulaid:{
required: "The user ID is required",
minlength: "The user ID should be no less than 5 characeters long.",
},
},
submitHandler: submitForm
});
/* validation */
/* get data */
function submitForm()
{
var data = $("#details-form").serialize();
$.ajax({
type : 'POST',
url : 'search.php',
data : data,
beforeSend: function()
{
$("#detailsmsg").fadeOut();
$("#btn-search").html('<img src="include/btn-ajax-loader.gif" /> Looking...');
$('#btn-search').attr('disabled', 'disabled');
},
success : function(data)
{
alert(data);
$("#detailsmsg").fadeIn(1000, function(){
$("#detailsmsg").removeClass("alert-danger").addClass("alert-success");
$('#btn-search').removeAttr('disabled');
$("#btn-search").html('Search again?');
$('#register-submit-btn').removeAttr('disabled');
$("#detailsmsg").html("<span>Data Found</span>");
});
$("#name").val(data.name); // not sure how to do this part. this is probably very wrong but still not getting anything on the alert.
$("#init_date").val(data.init_date);
$("#position").val(data.position);
$("#email").val(data.email);
}
});
return false;
}
/* get data */
});
</script>
내가 얻을 것이 무엇을보고 싶어하기 때문에 나는 성공 함수의 상단에있는 경고 (데이터)가 돌아 오지만 그것은 다시 비어있게됩니다. 내가 경고 (데이터 [0]) 할 경우 "정의되지 않은"메시지가 나타납니다.
콘솔 POST 반환이 :
name=&init_date=&position=&email=
이 스크립트에서 많은 실수가 아마 나는 내가 할 수있는 모든 피드백과 도움에 열려있어. 다른 세부 정보가 필요한지 알려주세요. 사전에
감사합니다, 단지 빨리이 중첩 된 배열을 반환하는 PHP 코드를보고
는 echo json_encode ($ found)를 넣으려고 시도 할 수 있습니다. search.php에서 벗어난 루프 –