[英]passing value through ajax
我是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代码
<?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代码:
<?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传递到同一页面,但我无法这样做。 希望我能在这里得到帮助。 先感谢您。
好的,数据作为请求的主体传递。 将标头请求中的Content-type设置为application / x-www-form-urlencoded,以便PHP可以$ _GET []它。
我认为您正在要求将价值传递给数据库
<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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.