不使用下面的代码获取数据

时间:2016-01-09 07:00:39

标签: javascript php ajax

我需要从mysql数据库获取数据而不使用jquery ajax刷新页面。我有一个PHP脚本,工作正常。但是,我的JS似乎遇到了一些问题。这是jquery脚本。 index.php

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>jQuery.post demo</title>
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>

<form action="/" id="searchForm">
  <input type="text" name="s" class="s" placeholder="Search...">
  <input type="submit" value="Search">
</form>
<!-- the result of the search will be rendered inside this div -->
<div id="item"></div>

<script>
// Attach a submit handler to the form
$( "#searchForm" ).submit(function( event ) {

  // Stop form from submitting normally
  event.preventDefault();

  // Get some values from elements on the page:
  var $form = $( this ),

    url = $form.attr( "action" );

  // Send the data using post
  var posting = $.post( 'test.php', { s: $(".s").val() } ).done(function( data ) {
    alert( "hiiiiiiiiii" + $(".s").val() );
  });

  // Put the results in a div


});
$.getJSON(
 'fetch_data.php',
 's='+$('.s').val(),
 function(result){
 $('#item').empty();
 $.each(result.result, function(){
 $('#item').append('<p>'+this['s']+'</p>');
 });
 });
</script>

</body>
</html>

此代码无效。请求给我解决它是如何工作的? fetch_data.php

<?php
if(!mysql_connect("localhost","root",""))
{
    echo "db connection error : ".mysql_error();
    exit;
}
else
{

    if(!mysql_select_db("test"))
    {
    header('Content-type: application/json');
    echo "db selection error : ".mysql_error();
    exit;
    }

}

$sql = "select s from test ";

 $res = mysql_query($sql);

 $result = array();

 while($row = mysql_fetch_array($res)){
 echo $row[0];
 array_push($result, 
 array('s'=>$row[0]));
 }

 echo json_encode(array('result'=>$result));





?>

0 个答案:

没有答案