dousha1394 2015-05-24 09:35
浏览 231

ajax jquery表单获取mysql数据

I have troubles with JQuery and Ajax forms. I'm new in Ajax and Jquery. I'm creating a webapp with a search form. The data I'm fetching from MYSQL Database. So I have a request page with a form, then the mysql processing file, a javascript to return the requested data and a html file, where the results are displayed. So, my problem is, that I can't display the results under index.html. Therefore I tried to action directly to landmarks.php and there I can see the reuslts in array like this

([{"id":"1","name":"Big Ben","latitude":"51.500600000000","longitude":"-0.124610000000"}]);

The request.html file

<head>
    <meta charset="UTF-8">
    <title>Updated - loading external data into a PhoneGap app using jQuery 1.5</title>
    <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
</head>




<body>
<form method="post" action="landmarks.php">
    <div data-role="fieldcontain">
        <fieldset data-role="controlgroup" data-type="horizontal" data-role="fieldcontain">
            <label for="l_name"><b>Name</b></label>
                <input type="text" name="l_name" id="l_name" value="" />
                <input class="submit" type="submit" value="Submit" />
        </fieldset>
    </div>
</form>


</body>

Then this file for mysql

<?php
header('Content-type: application/json');

$server = "localhost";
$username = "test";
$password = "test132";
$database = "landmarks";

$l_name = $_POST["l_name"];

$con = mysql_connect($server, $username, $password) or die ("Could not connect: " . mysql_error());
mysql_select_db($database, $con);

$sql = "SELECT id, l_name AS name, l_lat AS latitude, l_long AS longitude FROM landmarks WHERE l_name like '".$l_name."' ORDER BY l_name";
$result = mysql_query($sql) or die ("Query error: " . mysql_error());

$records = array();

while($row = mysql_fetch_assoc($result)) {
    $records[] = $row;
}

mysql_close($con);

echo $_GET['jsoncallback'] . '(' . json_encode($records) . ');';
?>

my json callback looks like this

$(document).ready(function(){
    var output = $('#output');

    $.ajax({
        url: 'landmarks.php',
        dataType: 'jsonp',
        jsonp: 'jsoncallback',
        timeout: 5000,
        success: function(data, status){
            $.each(data, function(i,item){ 
                var landmark = '<h1>'+item.name+'</h1>'
                + '<p>'+item.latitude+'<br>'
                + item.longitude+'</p>';

                output.append(landmark);
            });
        },
        error: function(){
            output.text('There was an error loading the data.')
        }
    });
});

and finally my final page, where the results should be displayed

<!DOCTYPE HTML>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Updated - loading external data into a PhoneGap app using jQuery 1.5</title>

    <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
    <script src="js/load-json.js"></script>
</head>
<body>
    <div id="output"></div>
</body>
</html>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥30 这是哪个作者做的宝宝起名网站
    • ¥60 版本过低apk如何修改可以兼容新的安卓系统
    • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
    • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
    • ¥50 有数据,怎么用matlab求全要素生产率
    • ¥15 TI的insta-spin例程
    • ¥15 完成下列问题完成下列问题
    • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
    • ¥15 YoloV5 第三方库的版本对照问题
    • ¥15 请完成下列相关问题!