scuff Posted October 8, 2008 Share Posted October 8, 2008 testing.php <?php if ($_POST['delete']) { $stringData = "delete"; $ourFileName = "directlink.php"; $ourFileHandle = fopen($ourFileName, 'w'); fwrite($ourFileHandle, $stringData); fclose($ourFileHandle); } ?> <body onLoad="commencer()"> <script language="javascript" type="text/javascript"> function commencer() { setInterval(ajaxFunction2, 1000); } function ajaxFunction2(){ var ajaxRequest2; // The variable that makes Ajax possible! try{ // Opera 8.0+, Firefox, Safari ajaxRequest2 = new XMLHttpRequest(); } catch (e){ // Internet Explorer Browsers try{ ajaxRequest2 = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try{ ajaxRequest2 = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e){ // Something went wrong alert("Your browser broke!"); return false; } } } ajaxRequest2.open('POST', 'testing.php'); ajaxRequest2.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); var title = document.getElementById('delete').value; ajaxRequest2.send(title); } </script> <input type="text" name="delete" id="delete" value="delete"> </body> I'm trying to make a form send every second without refreshing and basically clear another file. I can't get it to work does anyone see what I'm doing wrong? (probably many things because I'm testing) Quote Link to comment https://forums.phpfreaks.com/topic/127616-solved-ajax-send-form/ Share on other sites More sharing options...
Maq Posted October 9, 2008 Share Posted October 9, 2008 Are there errors? What's happening? Quote Link to comment https://forums.phpfreaks.com/topic/127616-solved-ajax-send-form/#findComment-660450 Share on other sites More sharing options...
xtopolis Posted October 9, 2008 Share Posted October 9, 2008 Maybe you're not sending the POST request right? One of my working examples looks like this: function post(val) { var XHRO = new newXHRO(); var author = document.getElementById('name').value; var quote = document.getElementById('quote').value; var url = '../_common/addquote.php'; var params = "author="+escape(author)+""e="+escape(quote); XHRO.open('POST', url, true); XHRO.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); XHRO.setRequestHeader("Content-length", params.length); XHRO.setRequestHeader("Connection", "close"); XHRO.onreadystatechange=function() { if(XHRO.readyState==4 && XHRO.status==200) { x.innerHTML = XHRO.responseText; } } XHRO.send(params); } Mainly, I don't see a XHRO.setRequestHeader("Content-length", params.length); in yours. Quote Link to comment https://forums.phpfreaks.com/topic/127616-solved-ajax-send-form/#findComment-660488 Share on other sites More sharing options...
scuff Posted October 9, 2008 Author Share Posted October 9, 2008 ajaxRequest2.open('POST', 'testing.php'); var title = document.getElementById('delete').value; ajaxRequest2.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); ajaxRequest2.setRequestHeader('Content-length', title.length); ajaxRequest2.send(title); I inserted this instead of ajaxRequest2.open('POST', 'testing.php'); ajaxRequest2.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); var title = document.getElementById('delete').value; ajaxRequest2.send(title); and it still didn't work Quote Link to comment https://forums.phpfreaks.com/topic/127616-solved-ajax-send-form/#findComment-660507 Share on other sites More sharing options...
xtopolis Posted October 9, 2008 Share Posted October 9, 2008 before I mess with this code.. is it because your <script> tags are not in the <head> section of your document.. move them from <body> -> <head> please. Quote Link to comment https://forums.phpfreaks.com/topic/127616-solved-ajax-send-form/#findComment-660510 Share on other sites More sharing options...
scuff Posted October 9, 2008 Author Share Posted October 9, 2008 <?php if ($_POST['delete']) { $stringData = "delete"; $ourFileName = "directlink.php"; $ourFileHandle = fopen($ourFileName, 'w'); fwrite($ourFileHandle, $stringData); fclose($ourFileHandle); } ?> <head> <script language="javascript" type="text/javascript"> function commencer() { setInterval(ajaxFunction2, 1000); } function ajaxFunction2(){ var ajaxRequest2; // The variable that makes Ajax possible! try{ // Opera 8.0+, Firefox, Safari ajaxRequest2 = new XMLHttpRequest(); } catch (e){ // Internet Explorer Browsers try{ ajaxRequest2 = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try{ ajaxRequest2 = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e){ // Something went wrong alert("Your browser broke!"); return false; } } } ajaxRequest2.open('POST', 'testing.php'); var title = document.getElementById('delete').value; ajaxRequest2.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); ajaxRequest2.setRequestHeader('Content-length', title.length); ajaxRequest2.send(title); } </script> </head> <body onLoad="commencer()"> <input type="text" name="delete" id="delete" value="delete"> </body> I didn't even notice that but either way it's still not working.. Quote Link to comment https://forums.phpfreaks.com/topic/127616-solved-ajax-send-form/#findComment-661214 Share on other sites More sharing options...
scuff Posted October 9, 2008 Author Share Posted October 9, 2008 If somebody could play around with it until it works then post code that would help D: I know it's lazy but do what you want Quote Link to comment https://forums.phpfreaks.com/topic/127616-solved-ajax-send-form/#findComment-661397 Share on other sites More sharing options...
Maq Posted October 9, 2008 Share Posted October 9, 2008 I didn't even notice that but either way it's still not working.. What does that mean? Are there any errors? If not do you have display_errors turned on? Try: if (isset($_POST['delete'])) { Quote Link to comment https://forums.phpfreaks.com/topic/127616-solved-ajax-send-form/#findComment-661399 Share on other sites More sharing options...
scuff Posted October 9, 2008 Author Share Posted October 9, 2008 I didn't even notice that but either way it's still not working.. What does that mean? Are there any errors? If not do you have display_errors turned on? Try: <?php if (isset($_POST['delete'])) { I was talking about how I didn't notice that the javascript wasn't in the <head> I don't think there are any errors because when the page loads it doesn't say anything at the bottom, and I don't know how to check if display_errors is turned on. BTW <?php if (isset($_POST['delete'])) { doesn't work either Quote Link to comment https://forums.phpfreaks.com/topic/127616-solved-ajax-send-form/#findComment-661442 Share on other sites More sharing options...
Maq Posted October 9, 2008 Share Posted October 9, 2008 Put this at the top of your script: ini_set ("display_errors", "1"); error_reporting(E_ALL); Run it, and see if there are any errors. Quote Link to comment https://forums.phpfreaks.com/topic/127616-solved-ajax-send-form/#findComment-661444 Share on other sites More sharing options...
scuff Posted October 9, 2008 Author Share Posted October 9, 2008 oh yea I have that on and there are no errors Quote Link to comment https://forums.phpfreaks.com/topic/127616-solved-ajax-send-form/#findComment-661446 Share on other sites More sharing options...
xtopolis Posted October 10, 2008 Share Posted October 10, 2008 My ajax works, but I didn't test your php script. I can successfully make it there though. ps, I renamed the url part of the ajax, so you'll have to change it back. <?php if ($_POST['delete']) { die('made it'); $stringData = "delete"; $ourFileName = "directlink.php"; $ourFileHandle = fopen($ourFileName, 'w'); fwrite($ourFileHandle, $stringData); fclose($ourFileHandle); } ?> <html> <head> <script language="javascript" type="text/javascript"> function commencer() { setInterval(ajaxFunction2, 1000); } function ajaxObj() { try { return new XMLHttpRequest(); } catch(e) {} try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {} try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {} alert("XMLHttpRequest not supported"); return null; } function ajaxFunction2(){ ajaxRequest2 = new ajaxObj; //loading document.getElementById('myDiv').innerHTML = 'Loading'; var params = "delete=1"; ajaxRequest2.open('POST', 'test.php', true); ajaxRequest2.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); ajaxRequest2.setRequestHeader("Content-length", params.length); ajaxRequest2.setRequestHeader("Connection", "close"); ajaxRequest2.onreadystatechange=function() { if(ajaxRequest2.readyState==4 && ajaxRequest2.status==200) { //x.innerHTML += '<br />' + ajaxRequest2.responseText; document.getElementById('myDiv').innerHTML = ajaxRequest2.responseText; }else{ document.getElementById('myDiv').innerHTML = ajaxRequest2.status; } } ajaxRequest2.send(params); } </script> </head> <body onLoad="commencer()"> <div id="myDiv"> Not yet. </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/127616-solved-ajax-send-form/#findComment-661459 Share on other sites More sharing options...
scuff Posted October 10, 2008 Author Share Posted October 10, 2008 Wow thanks! I simplified it to this to fit my needs <?php if ($_POST['delete']) { $stringData = "delete <br>"; $ourFileName = "directlink.php"; $ourFileHandle = fopen($ourFileName, 'a'); fwrite($ourFileHandle, $stringData); fclose($ourFileHandle); } ?> <html> <head> <script language="javascript" type="text/javascript"> function commencer() { setInterval(ajaxFunction2, 1000); } function ajaxObj() { try { return new XMLHttpRequest(); } catch(e) {} try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {} try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {} alert("XMLHttpRequest not supported"); return null; } function ajaxFunction2(){ ajaxRequest2 = new ajaxObj; var params = "delete=1"; ajaxRequest2.open('POST', 'test.php', true); ajaxRequest2.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); ajaxRequest2.setRequestHeader("Content-length", params.length); ajaxRequest2.setRequestHeader("Connection", "close"); ajaxRequest2.send(params); } </script> </head> <body onLoad="commencer()"> </body> </html> Thanks once again Quote Link to comment https://forums.phpfreaks.com/topic/127616-solved-ajax-send-form/#findComment-661465 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.