Javascript 通过ajax传递值
我是ajax新手,对ajax不太了解。我正在尝试使用ajax从url传递值,但我无法这样做。我在google上做了一些搜索,找到了一些ajax函数代码,我尝试在我的代码上实现,但一个代码不起作用,另一个代码给出了错误。我在下面留下了两个代码:- 错误:-1 ajax代码Javascript 通过ajax传递值,javascript,php,html,ajax,Javascript,Php,Html,Ajax,我是ajax新手,对ajax不太了解。我正在尝试使用ajax从url传递值,但我无法这样做。我在google上做了一些搜索,找到了一些ajax函数代码,我尝试在我的代码上实现,但一个代码不起作用,另一个代码给出了错误。我在下面留下了两个代码:- 错误:-1 ajax代码 <script> function loadXMLDoc(loadXMLDoc) { var xmlhttp; if (window.XMLHttpRequest) { Safari xmlhttp
<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>
函数loadXMLDoc(loadXMLDoc){var xmlhttp;如果
(window.XMLHttpRequest){
Safari xmlhttp=new XMLHttpRequest();}else{
IE5 xmlhttp=newActiveXObject(“Microsoft.xmlhttp”);}
xmlhttp.onreadystatechange=function(){if(xmlhttp.readyState==4
&&xmlhttp.status==200)
{
document.getElementById(“myDiv”).innerHTML=xmlhttp.responseText;
}}xmlhttp.open(“GET”,“1.php?check=&name=“+loadXMLDoc,true”);xmlhttp.send();}
html+php代码
<?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)">
让AJAX更改此文本
错误:-2
ajax代码:
<script>
$(document).ready(function () {
$("input#delete-btn").click(function(){
$.ajax({
type: "GET",
url: "1.php",
data: {id: 1234 }
});
});
});
</script>
$(文档).ready(函数(){
$(“输入#删除btn”)。单击(函数(){
$.ajax({
键入:“获取”,
url:“1.php”,
数据:{id:1234}
});
});
});
html+php代码:
<?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更改此文本`
我只想使用ajax将url中的值传递到同一个页面,但我无法这样做。希望我能在这里得到帮助。提前感谢您。好的,数据作为请求主体传递。
将标题请求中的内容类型设置为application/x-www-form-urlencoded,这样PHP就可以$\u获取[]它。我认为您要求将值传递到数据库中
输入数字:
人员信息将在此处列出。。。
函数showUser(){
httpRequest=新的XMLHttpRequest();
如果(!httpRequest){
警报(“放弃:(无法创建XMLHTTP实例”);
返回false;
}
var id=document.getElementById(“id”).value;
httpRequest.onreadystatechange=alertContents;
httpRequest.open(“GET”http://localhost/cart/guser.php?id=“+id+”&rand=“+true”);
httpRequest.send();
}
函数alertContents(){
if(httpRequest.readyState==XMLHttpRequest.DONE){
if(httpRequest.status==200){
document.getElementById(“txtHint”).innerHTML=httpRequest.responseText;
}
}
var id=document.getElementById('id')。值;
}
由于此程序显示用户的详细信息,但在传递值时代码中存在错误
实际使用ajax时要做什么?要将值从url传递到同一页面,为什么要使用ajax?@Khushboo我实际上想通过ajax传递值,这样页面就不会重新加载。我使用javascript传递值,但它们是page重新加载问题,所以我想使用ajax来传递它。@ArunprasanthKV我想使用ajax,这样页面就不会重新加载。我从javascript中知道这一点,但他们的问题是页面重新加载。如果是,他们是否有其他方法,这样页面就不会重新加载?请共享。是否要使用ajax从数据库中删除记录,而不重新加载?