dongyutan1703 2014-01-05 01:01
浏览 33

我写了一个html页面,它在php动作页面上做了一个简单的jquery ajax帖子。 1,第一部分,第二部分,第三部分,第四部分

I have an hmtl page that does a jquery ajax POST to a php action page, which queries MySQL and then echoes back data, which I capture (as the result variable) in the success function of my jquery ajax function.

Everything works fine, except that I can not get the php page to return the data in a format I can use.

I am trying to get it back in a multiple array or json format something like:

{"Sections":[{"sectionID":"1", "sectionText":"Section One"},{"sectionID":"2", "sectionText":"Section Two"},{"sectionID":"3", "sectionText":"Section Three"}]};

that I can parse and use.

However, this is the best I can get:

"1,Section One2,Section Two3,Section Three4,section four"

If i try to add any characters at all to the echo out, it goves by a bunch of jibberish that looks like html for a table with a bunch of errors thrown in.

Below is the PHP script

// Declare $sql variable with the SQL Query as a SQL Select Statement
    $sql="SELECT sectionID, sectionText FROM Sections";

// Store results of SQL query as a variable called "$result"
$result = mysqli_query($con,$sql);

if (!mysqli_query($con,$sql))
    {
        die('Error: ' . mysqli_error($con));
    }

// Do something with the results - making it a select
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
    {
    echo $row ['sectionID'], ',', $row['sectionText'];
    }

Below is the javascript jquery ajax function from the html page

 $.ajax({
url:"querySections3.php",
success:function(result){
   alert(result);
}
 });
  • 写回答

1条回答 默认 最新

  • dongshi6710 2014-01-05 01:12
    关注
    <?php
    
    // Declare $sql variable with the SQL Query as a SQL Select Statement
    $sql="SELECT sectionID, sectionText FROM Sections";
    
    // Store results of SQL query as a variable called "$result"
    $result = mysqli_query($con,$sql);
    
    if (!mysqli_query($con,$sql))
    {
       die('Error: ' . mysqli_error($con));
    }
    
    $sections = array();
    
    // Do something with the results - making it a select
    while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
    {
        $sections[] = array('sectionId' => $row ['sectionID'], 'sectionText' => $row['sectionText']);
    }
    
    header('Content-type: application/javascript');
    echo json_encode(array('sections' => $sections));
    

    And in your ajax request add dataType: json

    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料