为了账号安全,请及时绑定邮箱和手机立即绑定

关于员工创建的问题

我的代码感觉没有问题,为什么在创建员工的时候老是参数错误

http://img1.sycdn.imooc.com//57ec88b90001fb8b10050726.jpg


<!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=gb2312" />
<title>Demo</title>
<style>
*{ font-size:30px; line-height:1.8}
</style>
</head>

<body>
<h1>员工查询</h1>
<label>请输入员工编号:</label>
<input type="text" id="keyword">
<button id="search">查询</button>
<p id="searchResult"></p>

<h1>员工创建</h1>
<label>请输入员工姓名:</label>
<input type="text" id="staffName"><br/>
<label>请输入员工编号:</label>
<input type="text" id="staffNumber"><br/>
<label>请选择员工姓名:</label>
<select id="staffSex">
<option>男</option>
<option>女</option>
</select><br/>
<label>请输入员工职位:</label>
<input type="text" id="staffJob"><br/>
<button id="save">保存</button>
<p id="createResult"></p>
<script>
document.getElementById("search").onclick = function(){
    //发送Ajax查询请求并处理
    var request = new XMLHttpRequest();
    request.open("GET","service.php?number=" + document.getElementById("keyword").value);
    request.send();
    request.onreadystatechange = function(){
        if(request.readyState === 4){
            if(request.status === 200){
                document.getElementById("searchResult").innerHTML = request.responseText;
            }else{
                alert("发生错误:" +request.status);
            }
        }
    }
}

document.getElementById("save").onclick = function(){
    //发送Ajax查询请求并处理
    var request = new XMLHttpRequest();
    request.open("POST","service.php");
    var data = "name=" + document.getElementById("staffName").value
                + "&number=" + document.getElementById("staffNumber").value
                + "&sex=" + document.getElementById("staffSex").value
                + "&job=" + document.getElementById("staffJob").value;
    request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    request.send(data);
    request.onreadystatechange = function(){
        if(request.readyState === 4){
            if(request.status === 200){
                document.getElementById("createResult").innerHTML = request.responseText;
            }else{
                alert("发生错误:" +request.status);
            }
        }
    }
}

</script>
</body>
</html>

正在回答

2 回答

我在我的环境里运行时没有问题的 http://img1.sycdn.imooc.com//580605040001e2cf15590729.jpg

php文件里用<?php  

var_dump($_GET);

?>测试

结果如上图 结果只能是你的php文件有问题

0 回复 有任何疑惑可以回复我~

request.open("GET","service.php?number=" + document.getElementById("keyword").value);

request.open("POST","service.php");这个俩个service.php有问题,找不到,换成server.php;

0 回复 有任何疑惑可以回复我~
#1

名字都给你们起完了 提问者

这个service就只是个php文件,我换成了你说的那个也不行呀
2016-09-29 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
Ajax全接触
  • 参与学习       224758    人
  • 解答问题       708    个

本课程通过一个案例,讲解Ajax的相关概念原理实现方式和应用

进入课程

关于员工创建的问题

我要回答 关注问题
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号