Jump to content

[SOLVED] Ajax send form


scuff

Recommended Posts

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)

 

Link to comment
Share on other sites

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)+"&quote="+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.

Link to comment
Share on other sites

        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

Link to comment
Share on other sites

<?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 :P but either way it's still not working..

 

Link to comment
Share on other sites

I didn't even notice that :P 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'])) {

Link to comment
Share on other sites

I didn't even notice that :P 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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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

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.