simzam Posted December 25, 2010 Share Posted December 25, 2010 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']; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/222639-ajax-not-sending-to-php-help/ Share on other sites More sharing options...
BlueSkyIS Posted December 25, 2010 Share Posted December 25, 2010 don't you want to set the value of a form element, not innerhtml? Quote Link to comment https://forums.phpfreaks.com/topic/222639-ajax-not-sending-to-php-help/#findComment-1151382 Share on other sites More sharing options...
simzam Posted December 25, 2010 Author Share Posted December 25, 2010 yes ! so i need to change ? .innerhtml Quote Link to comment https://forums.phpfreaks.com/topic/222639-ajax-not-sending-to-php-help/#findComment-1151384 Share on other sites More sharing options...
simzam Posted December 25, 2010 Author Share Posted December 25, 2010 simply when i press Submit My Text Div should set on <textarea> Quote Link to comment https://forums.phpfreaks.com/topic/222639-ajax-not-sending-to-php-help/#findComment-1151391 Share on other sites More sharing options...
PFMaBiSmAd Posted December 25, 2010 Share Posted December 25, 2010 You need to access the <div> using .innerHTML (div's don't have values, form fields have values.) Quote Link to comment https://forums.phpfreaks.com/topic/222639-ajax-not-sending-to-php-help/#findComment-1151392 Share on other sites More sharing options...
simzam Posted December 25, 2010 Author Share Posted December 25, 2010 how to access div using innerhtml i'm kinda noob example would b helpful Quote Link to comment https://forums.phpfreaks.com/topic/222639-ajax-not-sending-to-php-help/#findComment-1151397 Share on other sites More sharing options...
PFMaBiSmAd Posted December 25, 2010 Share Posted December 25, 2010 The id of your <div> is id="name" The line of javascript that is referencing that contains - getElementById('name') Quote Link to comment https://forums.phpfreaks.com/topic/222639-ajax-not-sending-to-php-help/#findComment-1151399 Share on other sites More sharing options...
simzam Posted December 25, 2010 Author Share Posted December 25, 2010 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> Quote Link to comment https://forums.phpfreaks.com/topic/222639-ajax-not-sending-to-php-help/#findComment-1151401 Share on other sites More sharing options...
the182guy Posted December 25, 2010 Share Posted December 25, 2010 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> Quote Link to comment https://forums.phpfreaks.com/topic/222639-ajax-not-sending-to-php-help/#findComment-1151403 Share on other sites More sharing options...
simzam Posted December 25, 2010 Author Share Posted December 25, 2010 I'm Trying to display <div>My Text Div</div> in <textarea>My Text Div</textarea> when i press submit nothing happens <!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']; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/222639-ajax-not-sending-to-php-help/#findComment-1151407 Share on other sites More sharing options...
PFMaBiSmAd Posted December 26, 2010 Share Posted December 26, 2010 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.) Quote Link to comment https://forums.phpfreaks.com/topic/222639-ajax-not-sending-to-php-help/#findComment-1151503 Share on other sites More sharing options...
simzam Posted December 26, 2010 Author Share Posted December 26, 2010 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']}"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/222639-ajax-not-sending-to-php-help/#findComment-1151515 Share on other sites More sharing options...
simzam Posted December 26, 2010 Author Share Posted December 26, 2010 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}"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/222639-ajax-not-sending-to-php-help/#findComment-1151529 Share on other sites More sharing options...
PFMaBiSmAd Posted December 26, 2010 Share Posted December 26, 2010 ajaxobject.responseText is how you access the response that is sent back from the web server. In some of the other random changes you made to your code, you removed the reference to ajaxobject.responseText Quote Link to comment https://forums.phpfreaks.com/topic/222639-ajax-not-sending-to-php-help/#findComment-1151561 Share on other sites More sharing options...
PFMaBiSmAd Posted December 27, 2010 Share Posted December 27, 2010 Moving this thread to the Ajax forum section, since this has nothing to do with the php part of your code... Quote Link to comment https://forums.phpfreaks.com/topic/222639-ajax-not-sending-to-php-help/#findComment-1151772 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.