使用Ajax从数据库和php获取数据

使用Ajax从数据库和php获取数据,php,ajax,mysqli,Php,Ajax,Mysqli,我想使用Ajax将文本输入和设置结果(来自数据库)中的数据发送到其他输入中 我曾尝试使用json_encode()将结果解析为json,但由于某些原因,它无法工作,因此作为解决方案,我在结果php页面中创建了一个div,并使用它获取包含内容的div index.php 作为解决方案,我希望您提供一个简单的代码(AJAX、JSON和PHP)请更新JS代码和PHP代码 JS代码: function showinfoclient(){ var iDClient = $('#codecli').v

我想使用Ajax将文本输入和设置结果(来自数据库)中的数据发送到其他输入中

我曾尝试使用json_encode()将结果解析为json,但由于某些原因,它无法工作,因此作为解决方案,我在结果php页面中创建了一个div,并使用它获取包含内容的div

index.php


作为解决方案,我希望您提供一个简单的代码(AJAX、JSON和PHP)

请更新JS代码和PHP代码

JS代码:

function showinfoclient(){
  var iDClient = $('#codecli').val();

    if (iDClient) {
        $.ajax({
        type:'POST',
        url:'client.php',
        data:JSON.stringify(iDClient),
        dataType:'json', 

        })
        .done(function( json ) { 

            $('#lastname').val(json.lastname);
            $('#firstname').val(json.firstname);

        })
        .fail(function( xhr, status, errorThrown ) {

          alert( "Sorry, there was a problem!" );
          console.log( "Error: " + errorThrown );
          console.log( "Status: " + status );
          console.dir( xhr );
        });

    }
}
PHP代码(client.PHP):

在client.php中

$response=array();

while($client = $clients->fetch_assoc()){
$response[]=$client;
}
echo json_encode($response);
在ajax响应之后,您必须为每个客户机信息创建输入元素创建for循环

ajax响应:-

success:function(data){
     var clients=$.parsJSON(data);
var htmlData="";
     if(client.length>0){

       for(i=0;i<client.length;i++){
             htmlData += "<input type='text' name='firstname' value='"+client[i].firstname+"'><br>";
             htmlData += "<input type='text' name='lastname' value='"+client[i].lastname+"'><br>";
       }
        $('.minfoclient').html(htmldata);
      }
}
成功:函数(数据){
var客户端=$.parsJSON(数据);
var htmlData=“”;
如果(客户端长度>0){

对于(i=0;ipass
data
as
data:{idcli:iDClient}
,@DevsiOdedra-OP的操作方式是完全有效的(只要
iDClient
中没有奇怪的字符)实际上,我并没有真正理解你想要做什么。客户端.php
在哪里?它包含了什么/输出?另外,你需要解释当你尝试你的代码时实际发生了什么。仅仅说“它不起作用”并不能给我们提供任何信息。请编辑你的问题,将所有相关的代码都包括在内(包括您的尝试,并解释实际发生的情况)。另外,您向我们展示了一些HTML,然后是
result.php
,而您的ajax代码调用了
client.php
。我们需要对您的代码、结构、问题和预期结果进行适当的解释。我编写的代码显示了client.php包含的所有div都是来自sql的数据。此div替换了index.php中的div。我想替换t他用json_编码的代码你怎么可能知道client.php包含什么?特别是因为OP识别client.php是他的代码?我只放了原始数据。OP应该放他的php代码从数据库中获取数据。
function showinfoclient(){
  var iDClient = $('#codecli').val();

    if (iDClient) {
        $.ajax({
        type:'POST',
        url:'client.php',
        data:JSON.stringify(iDClient),
        dataType:'json', 

        })
        .done(function( json ) { 

            $('#lastname').val(json.lastname);
            $('#firstname').val(json.firstname);

        })
        .fail(function( xhr, status, errorThrown ) {

          alert( "Sorry, there was a problem!" );
          console.log( "Error: " + errorThrown );
          console.log( "Status: " + status );
          console.dir( xhr );
        });

    }
}
$input = urldecode(file_get_contents('php://input'));
$iDClient = json_decode($input,true);

// code to fetch firstname and lastname from database
$client = array('firstname' => 'John', 'lastname' => 'Doe'); // raw data


echo json_encode($client);
$response=array();

while($client = $clients->fetch_assoc()){
$response[]=$client;
}
echo json_encode($response);
success:function(data){
     var clients=$.parsJSON(data);
var htmlData="";
     if(client.length>0){

       for(i=0;i<client.length;i++){
             htmlData += "<input type='text' name='firstname' value='"+client[i].firstname+"'><br>";
             htmlData += "<input type='text' name='lastname' value='"+client[i].lastname+"'><br>";
       }
        $('.minfoclient').html(htmldata);
      }
}