简体   繁体   English

php ajax在多个文本框中显示数据库中的数据

[英]php ajax display data from database in multiple textboxes

Every one am new to php and ajax. 每个人都是php和ajax的新手。
Am writing a code to fetch data from mysql database and display them in textboxes but is not working. 我正在编写代码以从mysql数据库中获取数据并在文本框中显示它们,但无法正常工作。

Here is my code. 这是我的代码。
I need your help please. 我需要你的帮助。
Thanks in advance 提前致谢

    $('#btn_get').on('click', function(){
     $.ajax({
       type    : "get",
       url     : '/path/to/php/file',       
       success : function(data)
       {
         $('#input-box1').val(data);
         $('#input-box2').val(data);
       }
     });
    });
    <input type="text" id="input-box1">
     <input type="text" id="input-box2">
    <button type="button" id="btn_get">Click</button>
    //get data from db here
    $textbox1 = 'foo';
    $textbox2 = 'deen';
    echo $textbox1;
    echo $textbox2;



Here are some approaches, maybe them can help you: 以下是一些方法,也许它们可以为您提供帮助:

  1. The first approach would be to check your console to make sure that the jQuery version allows you to use the $.ajax() resource. 第一种方法是检查您的控制台,以确保jQuery版本允许您使用$ .ajax()资源。 Some jQuery versions like the "slim" doesn't provide ajax calls. 某些jQuery版本(例如“ slim”)不提供ajax调用。

  2. Once you have checked that put the property error within your ajax call: 一旦检查完,就将属性错误放入ajax调用中:

$('#btn_get').on('click', function(){
 $.ajax({
   type    : "get",
   url     : '/path/to/php/file',       
   success : function(data)
   {
     $('#input-box1').val(data);
     $('#input-box2').val(data);
   },
   error: function(xhr, ajaxOptions, thrownError) {
       console.log(xhr);
   }
 });
});

If you have a error response you will be able to identify it checking your browser's console tool (F12). 如果您有错误响应,则可以通过浏览器的控制台工具(F12)进行识别。

  1. Check your /path/to/php/file to make sure that your file really exists. 检查您的/ path / to / php / file ,以确保您的文件确实存在。
  2. Remember that the success callback gets your echo command as a string. 请记住, 成功回调将您的echo命令作为字符串获取。 So probably your return will be something like this: 因此,您的回报可能是这样的:
foodeen

A good approach would be to return a json response: 一个好的方法是返回一个json响应:

$textbox1 = 'foo';
$textbox2 = 'deen';
echo json_encode(array($textbox1 ,"textBox1"));

Finally when your response is executed in the success callback you'll be able to convert it from plain string to a json format: 最后,当您的响应在成功回调中执行时,您将能够将其从纯字符串转换为json格式:

$('#btn_get').on('click', function(){
 $.ajax({
   type    : "get",
   url     : '/path/to/php/file',       
   success : function(data)
   {
     var response = JSON.stringify(data);
     $('#input-box1').val(response[0]);
     $('#input-box2').val(response[1]);
   },
   error: function(xhr, ajaxOptions, thrownError) {
       console.log(xhr);
   }
 });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM