Jump to content

Ajax not sending to PHP Help


simzam

Recommended Posts

haY tell me where im doing wrong its not sending mydiv text to <textarea>

 

<!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()
{
//Declare the variable that will hold the XMLHttp Object.
var ajaxobject;
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  ajaxobject=new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {
  // code for IE6, IE5
  ajaxobject=new ActiveXObject("Microsoft.XMLHTTP");
  }
else
  {
  alert("Your browser does not support Ajax!");
  }

ajaxobject.onreadystatechange=function()
{
if(ajaxobject.readyState==4)

  {
  document.getElementById('namehere').innerHTML = ajaxobject.responseText;
  }
}
ajaxobject.open("GET","ajax.php?name="+document.getElementById('name').value,true);
ajaxobject.send(null);
}
</script>

<textarea id="namehere" name="namehere"></textarea>
<div id="name" name = "name">My Text Div</div> <br>

<input name="Submit" id="Submit" value="Submit"
onclick="javascript:changename()" type="button"></body>
</html>

 

<?php
    if ( !empty($_GET["name"])  &&  isset($_GET['name']) ){
    echo  $_GET['name'];
    }
?>

Link to comment
Share on other sites

still not working !

 

	var ajaxDisplay = document.getElementById('ajaxtext');
	ajaxDisplay.innerHTML = ajaxobject.responseText;

 

var ajaxtextarea = document.getElementById('ajaxDiv').innerHTML;
ajaxobject.open("GET","ajax.php?name="+ajaxtextarea, true);
ajaxobject.send(null);

 

<div id="ajaxDiv" >My Text Div</div> <br>
<textarea id="ajaxtext" ></textarea>

 

 

Link to comment
Share on other sites

What exactly isn't working? I suggest using jQuery for your ajax calls, it's easier than writing your own and probably more reliable. Plus you can use its JSON implementation to easily transfer php arrays to javascript and vicer-versa.

 

You don't even have to host the jQuery include yourself, just link to the one that Google hosts:

 

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>

Link to comment
Share on other sites

I'm Trying to display <div>My Text Div</div> in <textarea>My Text Div</textarea>  when i press submit nothing happens  :shrug:

 

<!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" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js">
function changename()
{
//Declare the variable that will hold the XMLHttp Object.
var ajaxobject;
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  ajaxobject=new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {
  // code for IE6, IE5
  ajaxobject=new ActiveXObject("Microsoft.XMLHTTP");
  }
else
  {
  alert("Your browser does not support Ajax!");
  }

ajaxobject.onreadystatechange=function()
{
if(ajaxobject.readyState==4)

  {

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

ajaxobject.open("GET","ajax.php?ajax="+ajaxtextarea.innerHTML, true);
ajaxobject.send(null);
}
</script>


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

<?php
if ( !empty($_GET["ajax"])  &&  isset($_GET['ajax']) ){
echo  $_GET['ajax'];
}
?>

 

Link to comment
Share on other sites

Programming does not involve randomly changing your code in the hope it will work. You must know why you are changing something.

 

You changed the - ajaxobject.open( ... ); statement so that it uses your ajaxtextarea variable, but you are setting the ajaxtextarea variable inside of a conditional statement that is only executed when the response is sent back from the web server. Your ajaxtextarea variable does not exist at the time you are using it in the ajaxobject.open( ... ); statement.

 

The only thing you should have changed in the ajaxobject.open( ... ); statement was to use .innerHTML (and perhaps change the id being used to match what you randomly changed your <div> id to.)

Link to comment
Share on other sites

yes sorry !  here it works properly i sort-out the problem

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

 

<!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("GET","ajax.php?namehere="+document.getElementById('ajaxDiv').innerHTML,true);
ajaxobject.send(null);
}

</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 ( !empty($_GET["namehere"])  &&  isset($_GET['namehere']) ){
echo  "{$_GET['namehere']}";
}
?>

Link to comment
Share on other sites

Please have a look and let me know why its not posting var to php i rechecked many times but cannot figure out !

 

<!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 ;


  }
}
var ajaxhttpDiv = document.getElementById("ajaxDiv");

ajaxobject.open("POST","ajax.php",true);
ajaxobject.setRequestHeader("Content-type","application/x-www-form-urlencoded");
ajaxobject.send('ajax='+ajaxhttpDiv.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}";

}
?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.