三个页面:ajax.js / ajax.php / index.php
ajax.js/**
* 初始化一个xmlhttp对象
*/
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 showHint()
{
//获取接受返回信息层
var msg = document.getElementById("txtHint"); //获取表单对象和用户信息值
var f = document.userlogin;
var UserName = f.UserName.value;
var UserAge = f.UserAge.value; //接收表单的URL地址
var url = "/ajax.php"; //需要POST的值,把每个变量都通过&来联接
var postStr = "&user_name="+ UserName +"&user_age="+ UserAge; //实例化Ajax
var ajax = InitAjax();
//通过Post方式打开连接
ajax.open("POST", url, true); //定义传输的文件HTTP头信息
ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); //发送POST数据
ajax.send(postStr); //获取执行状态
ajax.onreadystatechange = function() {
//如果执行状态成功,那么就把返回信息写到指定的层里
if (ajax.readyState == 4 && ajax.status == 200) {
msg.innerHTML = ajax.responseText;
}
}
}ajax.php <?php
//require("class.php");
$UserName=$_post['user_name'];
$UserAge=$_post['user_age'];
//$cd=new controldata();
//$cd->update(&$UserName,&$UserAge,&$con);
$hint=$UserName.$UserAge;
if ($hint == "")
{
$response="no suggestion";
}
else
{
$response=$hint;
}//output the response
echo $response;?>index.php<link rel="stylesheet" type="text/css" href="css.css" />
<script src="ajax.js"></script>
<?php
echo "<form method=post name=userlogin id=userlogin>UserName:<select name=UserName id=UserName><option value=aaa>aaa</option><option value=bbb>bbb</option></select>UserAge:<input type=text name=UserAge id=UserAge><input type=submit value=提交 onclick='showHint()'></form>";
echo "<div id=txtHint style='width:200px; height:30px; background:#ccc;'></div>";
?>
问题是点击index.php的提交按钮,在txtHint的地方只显示一下就马上又刷新了,是怎么回事?
谁有更好的ajax异步思路,望不吝赐教!
ajax.js/**
* 初始化一个xmlhttp对象
*/
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 showHint()
{
//获取接受返回信息层
var msg = document.getElementById("txtHint"); //获取表单对象和用户信息值
var f = document.userlogin;
var UserName = f.UserName.value;
var UserAge = f.UserAge.value; //接收表单的URL地址
var url = "/ajax.php"; //需要POST的值,把每个变量都通过&来联接
var postStr = "&user_name="+ UserName +"&user_age="+ UserAge; //实例化Ajax
var ajax = InitAjax();
//通过Post方式打开连接
ajax.open("POST", url, true); //定义传输的文件HTTP头信息
ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); //发送POST数据
ajax.send(postStr); //获取执行状态
ajax.onreadystatechange = function() {
//如果执行状态成功,那么就把返回信息写到指定的层里
if (ajax.readyState == 4 && ajax.status == 200) {
msg.innerHTML = ajax.responseText;
}
}
}ajax.php <?php
//require("class.php");
$UserName=$_post['user_name'];
$UserAge=$_post['user_age'];
//$cd=new controldata();
//$cd->update(&$UserName,&$UserAge,&$con);
$hint=$UserName.$UserAge;
if ($hint == "")
{
$response="no suggestion";
}
else
{
$response=$hint;
}//output the response
echo $response;?>index.php<link rel="stylesheet" type="text/css" href="css.css" />
<script src="ajax.js"></script>
<?php
echo "<form method=post name=userlogin id=userlogin>UserName:<select name=UserName id=UserName><option value=aaa>aaa</option><option value=bbb>bbb</option></select>UserAge:<input type=text name=UserAge id=UserAge><input type=submit value=提交 onclick='showHint()'></form>";
echo "<div id=txtHint style='width:200px; height:30px; background:#ccc;'></div>";
?>
问题是点击index.php的提交按钮,在txtHint的地方只显示一下就马上又刷新了,是怎么回事?
谁有更好的ajax异步思路,望不吝赐教!
解决方案 »
- php的array_key_exists函数奇怪问题
- <script>为何不会弹出Confirm对话框
- 用php 怎么播放flash
- php正则问题
- PHP相关问题提问
- 求一条mysql update语句(急。。。)分不够再加!
- 谁给解释一下:smart,adodb,phplib,AppController,Model,Exception
- header()函數到底有什麼用?
- 唠叨,君子兰,变态。。。各位大大进来看看。问个问题:)
- 我新装了windows 2003,可是扩展库,不知道为什么配不好!顶着有分
- ACityName1=%u4e39%u4e1c 这个等号后面的是什么啊?
- 浏览 php文件 报错
哪位还有更好的ajax异步思路啊
<script src="ajax.js"></script>
<?php
echo <<< HTML
<form method=post name=userlogin id=userlogin>
UserName:<select name=UserName id=UserName>
<option value=aaa>aaa</option>
<option value=bbb>bbb</option>
</select>
UserAge:<input type=text name=UserAge id=UserAge>
<input type=button value=提交 onclick='showHint()'>
</form>
HTML;
echo "<div id=txtHint style='width:200px; height:30px; background:#ccc;'></div>";
?>使用 ajax 时,说是“提交”,其实并没有提交按钮
哪位还有更好的ajax异步思路。
学习了