...

View Full Version : Ajax Post Issue !



simzam
12-26-2010, 12:58 PM
Hi
its not posting var to php have a look and let me know why ?



<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="content-type">
<title></title>
</head>
<body>
<script type="text/javascript">

function changename() {
var ajaxobject;
if (window.XMLHttpRequest){
ajaxobject=new XMLHttpRequest();
}

else if (window.ActiveXObject){
ajaxobject=new ActiveXObject("Microsoft.XMLHTTP");
} else {
alert("Your browser does not support Ajax!");
}

ajaxobject.onreadystatechange=function() {
if(ajaxobject.readyState==4 && ajaxobject.status == 200) {

var ajaxdisplay = document.getElementById('ajaxDiv') ;
var ajaxtextarea = document.getElementById('ajaxtext') ;
ajaxtextarea.innerHTML = ajaxdisplay.innerHTML ;


}
}
ajaxobject.open("POST","ajax.php",true);
ajaxobject.setRequestHeader("Content-type","application/x-www-form-urlencoded");
ajaxobject.send("ajax="+document.getElementById("ajaxDiv").innerHTML);

}

</script>

<div id="ajaxDiv" >My Div Text</div> <br>
<textarea id="ajaxtext" ></textarea>
<input name="Submit" id="Submit" value="Submit" onclick="changename()" type="button">
</body>

</html>


<?php

if(isset($_POST['ajax'])) {
$text = $_POST['ajax'];
echo " <br>{$text}";

}
?>

Dormilich
12-26-2010, 04:51 PM
is that one file or two files?

DJCMBear
12-26-2010, 05:30 PM
1. You haven't used responseText to allow the Javascript to display the callback text
2. if that is one file all the html above the PHP code will also be sent in the callback.

simzam
12-26-2010, 06:06 PM
yes all in 1 file how to make it work where to put responseText

DJCMBear
12-26-2010, 06:14 PM
Try this out.



<?php
if(isset($_POST['ajax'])) {
$text = htmlentities(stripslashes($_POST['ajax']));
print " <br>{$text}";
} else {
print <<<EOD
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="content-type">
<title>Untitled Document</title>
<script type="text/javascript">
function changename() {
var ajaxobject,
ajaxdisplay = document.getElementById('ajaxDiv'),
ajaxtextarea = document.getElementById('ajaxtext');
if(window.XMLHttpRequest) {
ajaxobject = new XMLHttpRequest();
} else if(window.ActiveXObject) {
ajaxobject = new ActiveXObject("Microsoft.XMLHTTP");
} else {
alert("Your browser does not support Ajax!");
}
ajaxobject.onreadystatechange=function() {
if(ajaxobject.readyState == 4 && ajaxobject.status == 200) {
ajaxtextarea.innerHTML = ajaxobject.responseText;
}
}
ajaxobject.open("POST","ajax.php",true);
ajaxobject.setRequestHeader("Content-type","application/x-www-form-urlencoded");
ajaxobject.send("ajax="+ajaxdisplay.innerHTML);
}
</script>
</head>
<body>
<div id="ajaxDiv">My Div Text</div><br>
<textarea id="ajaxtext"></textarea>
<input name="Submit" id="Submit" value="Submit" onclick="changename();" type="button">
</body>
</html>
EOD;
}
?>

simzam
12-26-2010, 07:23 PM
its working ! thanks
can we use
window.onload = changename(); to call function ty !

i tried this to call my Ajax script but it wouldn't work out

DJCMBear
12-26-2010, 07:37 PM
You can not use this as it will run the function before the onload
window.onload = changename(); you have to use this
window.onload = changename; or this
window.onload = function(){changename();};

simzam
12-28-2010, 08:12 PM
can i get
ajatext as variable
like

myvar = 'ajatext' ;

this use to get id how to use get in variable thanks

res = http.responseText;
document.getElementById('ajaxtext').innerHTML = res;

DJCMBear
12-28-2010, 08:16 PM
If I understand correctly you want the id name to be stored in a var and then use it in the document.getElementById function.



var myvar = 'ajaxtext',
res = http.responseText;
document.getElementById(myvar).innerHTML = res;

// OR you can just do this
var myvar = document.getElementById('ajaxtext'),
res = http.responseText;
myvar.innerHTML = res;

simzam
12-28-2010, 08:20 PM
and in php can i get this

myphpvar = myvar;
i want to document.getElementById content to php var

DJCMBear
12-28-2010, 08:23 PM
Oh you want to send the word 'ajaxtext' over to the PHP code that's why you are setting it as a variable ok just do this.



http.send('myvar='+myvar);


And in PHP you would use this.


$myphpvar = $_POST['myvar'];

simzam
12-31-2010, 01:43 PM
when ever i typed somthing and submit it didnt post anything what is wrong with my ajax script do i need to change getElementById to some form.element ?



<?php
print <<<EOD
<html>
<head>
<title> Insert !</title>
</head>
<body>
EOD;


if ((isset($_GET['myajax'])) &&($_GET['myajax']=="true")){

print <<<EOD
<script type="text/javascript">
function doHttpRequest() {
http.open("POST", "ajaxpost.php", true);
http.onreadystatechange = getHttpRes;
var params = "ajax1="+ encodeURI(document.getElementById("ajaxtext").value);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");
http.send(params);
}

function getHttpRes(){
if (http.readyState == 4 && http.status == 200) {
var myvar = document.getElementById('mydiv'),
res = http.responseText;
myvar.innerHTML = res;
}
}

function getXHTTP( ) {
var xhttp;
try {
xhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e2) {
try {
xhttp = new XMLHttpRequest();
} catch (e3) {
xhttp = false;
}
}
}
return xhttp;
}
var http = getXHTTP();
window.onload = doHttpRequest;
</script>
<div id='mydiv'></div><br>
EOD;
}

if ((isset($_GET['myajaxform'])) &&($_GET['myajaxform']=="true")){

print <<<EOD

<script type="text/javascript">
window.onload = submitform;
function submitform()
{
document.forms["myform"].submit();
}
</script>
EOD;
}

print <<<EOD

<form id="myform" action="?myajax=true" method="post" >
<textarea rows='3' cols='40' id='ajaxtext' name ='ajaxtext'></textarea><br>
</form>
<br> <a href = "?myajaxform=true" >Submit </a>



</body>
</html>
EOD;

?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum