我是ajax的新手,我对ajax并不了解。我试图使用ajax从url传递值,但我无法这样做。我在google上做了很少的搜索并找到了一些ajax函数代码,我试图在我的代码上实现但是一个代码不起作用而另一个代码给出了错误。我在下面留下了两个代码: -
错误:-1 ajax代码
<script> function loadXMLDoc(loadXMLDoc) { var xmlhttp; if
(window.XMLHttpRequest) {
Safari xmlhttp=new XMLHttpRequest(); } else {
IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4
&& xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
} } xmlhttp.open("GET","1.php?check=<?php if(isset($_GET['check'])){echo $_GET['check'];}else{echo "i am
mad";} ?>&name="+loadXMLDoc,true); xmlhttp.send(); } </script>
html + php code
<?php if(isset($_GET['check'])){echo $_GET['check'];} ?> <?php
if(isset($_GET['name'])){echo $_GET['name'];} ?> <div id="myDiv"><h2
style="display:none;">Let AJAX change this text</h2></div> <input
type="text" onChange="loadXMLDoc(this.value)">
错误: - 2 ajax代码:
<script>
$(document).ready(function () {
$("input#delete-btn").click(function(){
$.ajax({
type: "GET",
url: "1.php",
data: {id: 1234 }
});
});
});
</script>
html + php code:
<?php
if(isset($_GET['id'])){echo $_GET['id'];} ?> `<div id="myDiv"><h2 style="display:none;">Let AJAX change this text</h2></div>`
<input type="text" onChange="loadXMLDoc(this.value)">
我只是想使用ajax将url中的值传递到同一页面,但我无法这样做。希望我能在这里得到帮助。提前谢谢。
答案 0 :(得分:0)
好吧,数据作为请求的主体传递。 将您的标题请求中的内容类型设置为application / x-www-form-urlencoded,以便PHP可以$ _GET []它。
答案 1 :(得分:0)
我认为你要求将值传递给数据库
<html>
<head>
</head>
<body>
<form>
enter digit : <input type="text" id="id" name="id" /> <br />
<input type='button' onclick='showUser(this.value)' value='select'/>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here...</b></div>
<script>
function showUser() {
httpRequest = new XMLHttpRequest();
if (!httpRequest) {
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}
var id = document.getElementById("id").value;
httpRequest.onreadystatechange = alertContents;
httpRequest.open("GET", "http://localhost/cart/guser.php?id="+id+"&rand="+true);
httpRequest.send();
}
function alertContents() {
if (httpRequest.readyState === XMLHttpRequest.DONE) {
if (httpRequest.status === 200) {
document.getElementById("txtHint").innerHTML = httpRequest.responseText;
}
}
var id = document.getElementById('id').value;
}
</script>
</body>
</html>
as this progrm is to show the details of the user but there is error in the code in passing the value
&#13;