dsuvs66406 2012-08-13 18:09
浏览 66

GET / POST方法无法使用json将数据从mysql数据库获取到html

I am trying to get data in html page from mysql database using GET/POST method with help of json and Jquery. The scenario is i am taking two value from user interface (searchtenant.html) and POST it to newterms2.html where newterms2.html is getting data from tenantlistmob2.php using JSON. My searchtenant.html is

<!DOCTYPE HTML>
<html>
<body>
<form action="newterms2.html" method="post">
First Name:<input type="text" id="fname" name="fname" placeholder="First Name" required="required"/><br /><br />
Last Name: <input type="text" id="lname" name="lname" placeholder="Last Name" required="required"/><br /><br />
<input type="submit" id="save" name="search" value="Search"/>
</form>
</fieldset>
</body>
</html>

My tenantlistmob2.php is

<?php 
include('connection.php');
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$result = mysql_query("SELECT * FROM `tenanttemp` WHERE `TenantFirstName`='$fname' and `TenantLastName`='$lname'");
//$result = mysql_query("SELECT * FROM `tenanttemp`");
while ($row = mysql_fetch_assoc($result)) 
{
 $array[] = array($row['TenantFirstName']);
 }
echo json_encode($array);
?>

My newterms2.html is like

<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript" src="myjquery.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function() { 
    jQuery.getJSON("tenantlistmob2.php", function (jsonData) {
            jsonData= eval(jsonData);//get json array
            for (i = 0; i < jsonData.length; i++)//iterate over all options
            {
              for ( key in jsonData[i] )//get key => value
              { 
                   var tblRow = "<tr>" + "<td>" + "<a href='#'>" + jsonData[i][key] + "</a>" + "</td>" + "</tr>"
                    $(tblRow).appendTo("#getname tbody");
              }
            }

   });

   });
  </script></head>
 <body>
 <form name="index">
 <div id="getname2"></div>
 <div>
 <table id="getname" border="1">
    <thead>
        <th>Name</th>
    </thead>
  <tbody>

   </tbody>
</table>
</div>
</form>
</body>
</html> 

This is the total scenario.If i call tenantlistmob2.php directly from searchtenant.html using POST method it works fine.But i need html page to show the data because i will use this html page for both android and iPhone. The query ($result = mysql_query("SELECT * FROM tenanttemp");) works fine and return properly to newterms2.html.But when i use POSTed data ($result = mysql_query("SELECT * FROM tenanttemp WHERE TenantFirstName='$fname' and TenantLastName='$lname'");) it works properly in php but not returning data to newterms2.html page.

What is the solution? I like to get the data in html page.Please help.

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • duancong2160 2012-08-27 08:08
    关注

    You are posting the formdata from searchtenant.html to newterms2.html and not to tenantlistmob2.php.

    • jQuery/Javascript can not handle POST data sent to a page.
    • jQuery.getJSON uses GET, not POST.

    What you need to do is to change newterms2.html into a PHP page that can use the POST data from searchtenant.html and use it for the ajaxrequest.

    Javascriptchanges in newterms2.php

    <script type="text/javascript">
    
        $(document).ready(function() { 
            $.ajax({
                url: 'tenantlistmob2.php',
                dataType: 'json',
                data: <?php echo 'fname='.$_POST['fname'].'&lname='.$_POST['lname']; ?>,
                type: 'POST',
                success: function(jsonData){
                    jsonData = eval(jsonData); //get json array
                    for (i = 0; i < jsonData.length; i++)//iterate over all options
                    {
                        for ( key in jsonData[i] )//get key => value
                        { 
                            var tblRow = "<tr>" + "<td>" + "<a href='#'>" + jsonData[i][key] + "</a>" + "</td>" + "</tr>"
                            $(tblRow).appendTo("#getname tbody");
                        }
                    }
                }
            });
        }
    </script>
    

    That should do the trick.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog