Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-26-2010, 12:58 PM   PM User | #1
simzam
New Coder

 
Join Date: Dec 2010
Posts: 36
Thanks: 1
Thanked 0 Times in 0 Posts
simzam is an unknown quantity at this point
Ajax Post Issue !

Hi
its not posting var to php have a look and let me know why ?

PHP Code:
<!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}";

}
?>

Last edited by simzam; 12-26-2010 at 01:07 PM..
simzam is offline   Reply With Quote
Old 12-26-2010, 04:51 PM   PM User | #2
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,907
Thanks: 10
Thanked 293 Times in 289 Posts
Dormilich is on a distinguished road
is that one file or two files?
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 12-26-2010, 05:30 PM   PM User | #3
DJCMBear
Senior Coder

 
DJCMBear's Avatar
 
Join Date: Mar 2010
Location: United Kindom
Posts: 1,173
Thanks: 14
Thanked 136 Times in 136 Posts
DJCMBear is on a distinguished road
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.
__________________
Official BinPress hand picked coder.
For anyone worried about SQL injection go have a look at my small yet powerful script here.
Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
I am 1 of 65,608 people to get a Pebble Watch :P
DJCMBear is offline   Reply With Quote
Old 12-26-2010, 06:06 PM   PM User | #4
simzam
New Coder

 
Join Date: Dec 2010
Posts: 36
Thanks: 1
Thanked 0 Times in 0 Posts
simzam is an unknown quantity at this point
yes all in 1 file how to make it work where to put responseText
simzam is offline   Reply With Quote
Old 12-26-2010, 06:14 PM   PM User | #5
DJCMBear
Senior Coder

 
DJCMBear's Avatar
 
Join Date: Mar 2010
Location: United Kindom
Posts: 1,173
Thanks: 14
Thanked 136 Times in 136 Posts
DJCMBear is on a distinguished road
Try this out.

PHP Code:
<?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;
}
?>
__________________
Official BinPress hand picked coder.
For anyone worried about SQL injection go have a look at my small yet powerful script here.
Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
I am 1 of 65,608 people to get a Pebble Watch :P

Last edited by DJCMBear; 12-26-2010 at 06:18 PM..
DJCMBear is offline   Reply With Quote
Old 12-26-2010, 07:23 PM   PM User | #6
simzam
New Coder

 
Join Date: Dec 2010
Posts: 36
Thanks: 1
Thanked 0 Times in 0 Posts
simzam is an unknown quantity at this point
its working ! thanks
can we use
Quote:
window.onload = changename();
to call function ty !

i tried this to call my Ajax script but it wouldn't work out
simzam is offline   Reply With Quote
Old 12-26-2010, 07:37 PM   PM User | #7
DJCMBear
Senior Coder

 
DJCMBear's Avatar
 
Join Date: Mar 2010
Location: United Kindom
Posts: 1,173
Thanks: 14
Thanked 136 Times in 136 Posts
DJCMBear is on a distinguished road
You can not use this as it will run the function before the onload
Code:
window.onload = changename();
you have to use this
Code:
window.onload = changename;
or this
Code:
window.onload = function(){changename();};
__________________
Official BinPress hand picked coder.
For anyone worried about SQL injection go have a look at my small yet powerful script here.
Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
I am 1 of 65,608 people to get a Pebble Watch :P
DJCMBear is offline   Reply With Quote
Old 12-28-2010, 08:12 PM   PM User | #8
simzam
New Coder

 
Join Date: Dec 2010
Posts: 36
Thanks: 1
Thanked 0 Times in 0 Posts
simzam is an unknown quantity at this point
can i get
ajatext as variable
like
Quote:
myvar = 'ajatext' ;
this use to get id how to use get in variable thanks
Quote:
res = http.responseText;
document.getElementById('ajaxtext').innerHTML = res;
simzam is offline   Reply With Quote
Old 12-28-2010, 08:16 PM   PM User | #9
DJCMBear
Senior Coder

 
DJCMBear's Avatar
 
Join Date: Mar 2010
Location: United Kindom
Posts: 1,173
Thanks: 14
Thanked 136 Times in 136 Posts
DJCMBear is on a distinguished road
If I understand correctly you want the id name to be stored in a var and then use it in the document.getElementById function.

Code:
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;
__________________
Official BinPress hand picked coder.
For anyone worried about SQL injection go have a look at my small yet powerful script here.
Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
I am 1 of 65,608 people to get a Pebble Watch :P
DJCMBear is offline   Reply With Quote
Old 12-28-2010, 08:20 PM   PM User | #10
simzam
New Coder

 
Join Date: Dec 2010
Posts: 36
Thanks: 1
Thanked 0 Times in 0 Posts
simzam is an unknown quantity at this point
and in php can i get this

myphpvar = myvar;
i want to document.getElementById content to php var
simzam is offline   Reply With Quote
Old 12-28-2010, 08:23 PM   PM User | #11
DJCMBear
Senior Coder

 
DJCMBear's Avatar
 
Join Date: Mar 2010
Location: United Kindom
Posts: 1,173
Thanks: 14
Thanked 136 Times in 136 Posts
DJCMBear is on a distinguished road
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.

Code:
http.send('myvar='+myvar);
And in PHP you would use this.
PHP Code:
$myphpvar $_POST['myvar']; 
__________________
Official BinPress hand picked coder.
For anyone worried about SQL injection go have a look at my small yet powerful script here.
Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
I am 1 of 65,608 people to get a Pebble Watch :P
DJCMBear is offline   Reply With Quote
Old 12-31-2010, 01:43 PM   PM User | #12
simzam
New Coder

 
Join Date: Dec 2010
Posts: 36
Thanks: 1
Thanked 0 Times in 0 Posts
simzam is an unknown quantity at this point
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 Code:
<?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;

?>
simzam is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:25 AM.


Advertisement
Log in to turn off these ads.