作者gmotwm2001 (小马怪)
看板Ajax
标题[讨论] get ajax怎麽写回资料库
时间Thu Feb 1 23:08:37 2018
各位大大好:
我用一个htm用ajax写回到php跟资料库,原本用post但网页会产生
405 Method Not Allowed,所以改用get,但是怎样都写不回资料库
网路上找答案都找不到,麻烦各位能帮忙解答 谢谢!
sign1.htm
<html>
<script src="
http://ee.nsysu.edu.tw/key/bootstrap/js/jquery.min.js"></script>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<div id="content_p">
<table width="600px" height="400px">
<p>姓名:<input type="text" name="name" id="name1" size="10">
<p>借用人联络电话:<input type="text" id="phone1" name="phone" size="10"></p>
<!--<p>实验室钥匙编号</p>
<input type="checkbox" name="lab[]" value="EC6018">EC6018
<input type="checkbox" name="lab[]" value="EC6023">EC6023
<input type="checkbox" name="lab[]" value="EC6027">EC6027 -->
<p><button type="button" id="p1"onclick="myFunction() >Traditional Submit</button><input
type="reset" value="重新填写"></p>
</table>
</div>
<script type="text/javascript">
function myFunction() {
$(document).ready(function(){
var name2=$("#name1").val();
var phone2=$("#phone1").val();
$.ajax({
type: "POST",
url: "turnpage1.php",
data: {
name:name2,
phone:phone2
},
error: function(jqXHR, textStatus, errorThrown) {
alert(jqXHR.responseText);
},
success: function(data) {
alert('success: ' + data);
}
});
});
}
</script>
</body>
</html>
turnpage1.php
<?php
$hostdb='localhost';
$namedb='key';
$userdb='root';
$passdb='nsysuee1!';
try {
$conn= new PDO("mysql:host=$hostdb;dbname=$namedb",$userdb,$passdb);
//echo'资料库伺服器连线及资料库开启成功'.'<br>';
$conn->exec("SET CHARACTER SET utf8");
$user=$_POST['name'];
$phone=$_POST['phone'];
$sql2="insert into list(name,lab,phone,usrtime) values
(:user,:mylab,:phone,:usrtime)"; //单冒号宣布冒号里的字串 双冒号是分析这个冒号
里的语法
$sql =$conn->prepare($sql2);
//$sql->bindParam(':user',$user);
//$sql->bindParam(':pass',$pass);
//$sql->execute();
$sql->execute(array(
"user"=>$user,
"phone"=>$phone,
"usrtime"=>date("Y/m/d-H:i:s")
));
echo "新增成功,3秒後跳到下一页<br>";
//header("Refresh:3;URL=sign.htm");
/*$result=$conn->query($sql3);
foreach ($result->fetchALL() as $row)
{
echo $row['name'].'-'.$row['lab']."<br>";
}*/
$conn=null; //结束对象资源
}
catch(PDOEXception $e){
echo $e->getMessage();
}
?>
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 61.227.191.38
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Ajax/M.1517497720.A.EEA.html
1F:→ serotw: 我觉得是你送出按钮语法的问号 type=submit 改成 type=but 02/02 08:47
2F:→ serotw: ton 02/02 08:47
※ 编辑: gmotwm2001 (140.117.166.187), 02/02/2018 09:34:01
※ 编辑: gmotwm2001 (140.117.166.187), 02/02/2018 09:44:54
※ 编辑: gmotwm2001 (140.117.166.187), 02/02/2018 09:47:56
3F:→ gmotwm2001: 可以了~感谢 02/02 09:48