js通过php查mysql

danqe 2009-11-24 09:57:48
1 function data_handle(x){
2 var value_1=document.getElementById(x).value;
3 value_1=value_1.toUpperCase();
4 document.getElementById(x).value=value_1; //使输入到x的内容不区分大小写
5
6 var fen="fen"+i;
7 //这里需要读库,获得数据 var data
8 document.getElementById(fen).value="获取data";
9 //document.getElementById(fen).value=100; 这样测试正常。
10 这里的x和fen都是文本框。
11 }

12 注释部分的内容是一段php,在另一个文件getdata.php里:
13 include("conn.php");
14 $resault=mysql_query("SELECT * FROM ... WHERE `filed`='js里的value_1' LIMIT 1",$link)or die(...);
15 $date=mysql_fetch_array($resault);
16 $value_2=$data[5];
17 把$value_2写到上面的第8行等号后面
18 即:
20 document.getElementById(fen).value=$value_2;
21 各位高手,怎么弄?
...全文
151 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
phnessu4 2009-11-24
  • 打赏
  • 举报
回复
可以用jquery 写的代码量少很多...
复制代码另存为网页就可以用了...
(注意目录下要有getdata.php,jquery的404错误不显示,需要自己判断)


<!--jquery -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>

<script type="text/javascript">
$(document).ready(function(){
data_handle();
});
function data_handle(){
/*
* 转换为大写
* $('#x').val() 取值
* $('#x').val().toUpperCase() 转大写
* $('#x').val($(this).val().toUpperCase()) 赋值
*/
//alert($('#x').val().toUpperCase()); //debug
$('#x').val($('#x').val().toUpperCase());

var fen="fen"+i;

//提交url
var url = 'getdata.php';

//ajax的post提交
$.post(url,function(data){
//赋值
//alert(data); //debug
$('#fen').val(data);
}
);
}
</script>
<input type="text" value="asd" id="x" />
<input type="text" value="123" id="fen" />


getdata.php页面:

<?php
include_once("conn.php");
$resault=mysql_query("SELECT * FROM ... WHERE `filed`='js里的value_1' LIMIT 1",$link)or die(...);
$date=mysql_fetch_array($resault);
$value_2=$data[5];
echo $value_2;
//页面打印的内容即为上面ajax获取的data
?>
danqe 2009-11-24
  • 打赏
  • 举报
回复
var fen="fen"+i;//这个i是哪来的
i是全局变量。没有问题。
阿_布 2009-11-24
  • 打赏
  • 举报
回复
用AFAX可以。

function initAjax(){
var ajax = false;
try{
ajax = new ActiveXObject("Msxml2.XMLHTTP");
}catch(e){
try{
ajax = new ActiveXObject("Microsoft.XMLHTTP");
}catch(E){
ajax = false;
}
}
if(!ajax && typeof XMLHttpRequest != 'undefined'){
ajax = new XMLHttpRequest();
}
return ajax;
}
function data_handle(x){
var value_1=document.getElementById(x).value;
value_1=value_1.toUpperCase();
document.getElementById(x).value=value_1;
var fen="fen"+i;//这个i是哪来的
var ajax = initAjax();
ajax.open("post","getdata.php",true);
ajax.onreadystatechange=function(){
if(ajax.readyState==4 && ajax.status==200){
document.getElementById(fen).value
=decodeURIComponent(ajax.responseText);
}
}
ajax.send(null);
}
getdata.php页面:
<?php
include_once("conn.php");
$resault=mysql_query("SELECT * FROM ... WHERE `filed`='js里的value_1' LIMIT 1",$link)or die(...);
$date=mysql_fetch_array($resault);
$value_2=$data[5];
echo $value_2;
?>
dzxccsu 2009-11-24
  • 打赏
  • 举报
回复
ajax
foolbirdflyfirst 2009-11-24
  • 打赏
  • 举报
回复
AJAX

21,891

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧