Javascript 不使用下面的代码获取数据
我需要从mysql数据库中获取数据,而不需要使用jQueryAjax刷新页面。我有一个php脚本,它工作得很好。然而,我的JS似乎有一些问题。下面是jquery脚本。 index.phpJavascript 不使用下面的代码获取数据,javascript,php,ajax,Javascript,Php,Ajax,我需要从mysql数据库中获取数据,而不需要使用jQueryAjax刷新页面。我有一个php脚本,它工作得很好。然而,我的JS似乎有一些问题。下面是jquery脚本。 index.php <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>jQuery.post demo</title> <script src="htt
<!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>
jQuery.post演示
//将提交处理程序附加到表单
$(“#搜索表单”).submit(函数(事件){
//阻止表单正常提交
event.preventDefault();
//从页面上的元素获取一些值:
var$form=$(此),
url=$form.attr(“操作”);
//使用post发送数据
var posting=$.post('test.php',{s:$(“.s”).val()}).done(函数(数据){
警报(“HIIIIII”+$(“.s”).val());
});
//将结果放入div中
});
$.getJSON(
“fetch_data.php”,
's='+$('.s').val(),
功能(结果){
$(“#项”).empty();
$.each(result.result,function(){
$(“#项”)。追加(“”+此['s']+”');
});
});
此代码不起作用。请告诉我它是如何工作的?
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));
?>
它怎么不起作用?有什么意想不到的事情发生吗?你有什么错误吗?在你清楚地在术语中已经有了值之后,为什么还要使用类选择器($(“.s”)
)?@DonScott It’s nothing…我从internet复制代码。你为什么不先熟悉一下你正在使用的这些工具呢?尝试