I am new to ajax and i don't have much idea about ajax. I am trying to pass the value from url using ajax but i am not being able to do so. I have done few search on google and found some ajax function code and i tried to implement on my code but one code is not working and another code is giving error. I have left both code below:-
error:-1 ajax code
<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)">
error:- 2 ajax code:
<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)">
I just want to pass the value from url to the same page using ajax but i am not being able to do so. Hope i will get help over here. Thank You in advance.
well, the data is passed as the body of the request. Set your Content-type in your header request to application/x-www-form-urlencoded so that PHP can $_GET[] it.
i think you are asking to pass value into databse
<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
</div>