求一个例子就是一个button按钮,click后执行一段php代码,而不是连接到php页面 我看网上说是用ajax,但是我不会用,求一段jquery $.ajax()方法写的一个小例子 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 PHP run on server. JQuery(javascript) run on browser. That's different. 测试过了直接用,把文件也保存为utf-8格式的,中文也不会乱码,希望对你有用,jquery要包含,例子中我包含到线上的http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js,实际从官网下载//a.html<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title> ajax 提交 </title> <meta http-equiv="Content-Type" content="text/html" charset="utf-8"/> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script> </head> <body><input name="txt_name" id="txt_name" type="text" value=""><input name="txt_age" id="txt_age" type="text" value=""><input type="button" onclick="btnClick();" value="提交"><script type="text/javascript"> function btnClick(){ var txtName = $('#txt_name').val(); var txtAge = $('#txt_age').val(); dataVal={txt_name:txtName,txt_age:txtAge}; $.ajax({ type: "GET", url: "a.php", data:dataVal, // data: "name=John&location=Boston", success: function(msg){ alert(msg); } }); }</script> </body></html>//a.php//打印出从a.html传递过来的表单数据,用来测试用,//实际中做相应处理,如提交到表操作等var_dump($_GET); 恩 看到了 谢谢 我还想弄明白,我不传值,只是想点击之后执行一句sql语句写入数据库,比方说我前台有个按钮叫新生入库(就是把所有报到的学生信息载入到学生信息表中),后台有段php代码我想不跳转页面不刷新的前提下直接点击就执行这个操作 不跳转的话就是ajax异步传输喽。花半天时间看看吧http://www.w3school.com.cn/ajax/index.asp AJAX吧。不然就是要客户端的DLL http://www.w3school.com.cn/ajax/index.asp 里面的例子很详细。另外你可以看看php100.com里面有专门的关于jquery ajax的视频,讲的比较详细,好像是146节左右。 兄弟赶紧耐心学习吧。这些问题都是基本的码。不会这些,怎么混饭吃。例子,有没有错我的就不知道了:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script type="text/javascript" src="http://lib.sinaapp.com/js/jquery/1.6/jquery.js"></script> </head><body> <input type="button" id="submit" value="提交"><script type="text/javascript"> $("#submit").click(function(){ $.ajax({url: 'p.php', type: 'GET', dataType: 'html', timeout: 1000, error: function(){alert('Error');}, success: function(result){alert(result);} }); return false; }); </script></body></html><?php$link = mysql_connect('localhost', 'root', '');if (!$link) { die('Could not connect: ' . mysql_error());}//mysql_select_db('mydb');//mysql_query("INSERT INTO mytable (product) values ('kossu')"); exit("ok"); <!-- 当前页面xxx.php --><?phpif(isset($_GET['new'])){ $sql="……"; ……}?><form name="form" action="xxx.php"> <input type="button" name="new" value="新生入库" onclick="location.href='xxx.php?new=1'" /></form> 用异步传输可以实现。流行点的就用ajax,去下个jquery的库,使用很简单的。如果不想麻烦,也可以用旧式的,就是在页面中使用iframe的方式实现。 <script type="text/javascript"> $("#submit").click(function(){ $.ajax({url: 'p.php', type: 'GET', dataType: 'html', timeout: 1000, error: function(){alert('Error');}, success: function(result){alert(result);} }); return false; }); </script> php数据库连接问题 问一个学习php前的一个基本问题 有熟悉Kohana框架的吗? php 菜鸟的两个问题 HELL Appserv安装后无法启动 字符串替换问题: 问题 请问怎样编写??? 很急,求教,有谁知道pconnect? 这是不是PHP的一个bug啊!大家来看看!研究一下!! 一个简单的php画图问题、、、 Discuz!论坛中帖子列表中显示的用户信息是关联查询还是其它方式获取的?
例子中我包含到线上的http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js
,实际从官网下载//a.html<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> ajax 提交 </title>
<meta http-equiv="Content-Type" content="text/html" charset="utf-8"/>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
</head> <body><input name="txt_name" id="txt_name" type="text" value="">
<input name="txt_age" id="txt_age" type="text" value="">
<input type="button" onclick="btnClick();" value="提交"><script type="text/javascript">
function btnClick(){
var txtName = $('#txt_name').val();
var txtAge = $('#txt_age').val();
dataVal={txt_name:txtName,txt_age:txtAge};
$.ajax({
type: "GET",
url: "a.php",
data:dataVal,
// data: "name=John&location=Boston",
success: function(msg){
alert(msg);
}
});
}
</script>
</body>
</html>//a.php
//打印出从a.html传递过来的表单数据,用来测试用,
//实际中做相应处理,如提交到表操作等
var_dump($_GET);
例子,有没有错我的就不知道了:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="http://lib.sinaapp.com/js/jquery/1.6/jquery.js"></script>
</head><body>
<input type="button" id="submit" value="提交">
<script type="text/javascript">
$("#submit").click(function(){
$.ajax({url: 'p.php',
type: 'GET',
dataType: 'html',
timeout: 1000, error: function(){alert('Error');}, success: function(result){alert(result);} });
return false;
});
</script>
</body>
</html><?php$link = mysql_connect('localhost', 'root', '');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
//mysql_select_db('mydb');//mysql_query("INSERT INTO mytable (product) values ('kossu')");
exit("ok");
<!-- 当前页面xxx.php -->
<?php
if(isset($_GET['new'])){
$sql="……";
……
}
?>
<form name="form" action="xxx.php">
<input type="button" name="new" value="新生入库" onclick="location.href='xxx.php?new=1'" />
</form>
如果不想麻烦,也可以用旧式的,就是在页面中使用iframe的方式实现。
$("#submit").click(function(){
$.ajax({url: 'p.php',
type: 'GET',
dataType: 'html',
timeout: 1000,
error: function(){alert('Error');}, success: function(result){alert(result);} });
return false;
});
</script>