Submitting form in php file accessed by Ajax?

Posted by dronnoc on Stack Overflow See other posts from Stack Overflow or by dronnoc
Published on 2011-01-05T05:15:38Z Indexed on 2011/01/05 6:53 UTC
Read the original article Hit count: 179

Filed under:
|
|
|

Hi people,

I am trying to figure out how to submit a form in a page being accessed by Ajax?

here are some code snippets to help demonstrate what i am trying to say.

HTML BODY - THIS IS WHAT THE USER WILL SEE.

  <html>
    <head>
     <script language="javascript" src="linktoajaxfile.js">
    </head>
    <body onLoad="gotoPage(0)">
    <div id="fillThis">
    </div>
  </body>
</html>

AJAX FILE

var xmlhttp

function gotoPage(phase)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  }
var url="pageofstuffce.php";
url=url+"?stg="+phase;
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}

function stateChanged()
{
if (xmlhttp.readyState==4)
{
document.getElementById("fillThis").innerHTML=xmlhttp.responseText;
}
}

function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
{
// code for IE6, IE5
return new ActiveXObject("Microsoft.XMLHTTP");
}
return null;
}

PAGE OF DATA

<?php
  $stage = $_GET['stg'];

  if($stage == 0)
  {
     echo '<a onClick="gotoPage(1)">click me</a>';
  }
  elseif($stage == 1)
  {
    <form>
      <input type="text" name="name">
      <input type="submit" name="submit">
    </form>
  }
  elseif(somehow can reach here)
  {
     show data from form.
  }
?>

Can anyone perhaps help me get past the form and display the data in the same page? Also, i have looked around, and i don't think anything around has what i need... correct me if i'm wrong though :)

Thanks in advance, and i hope i didn't put in too much detail :) Dronnoc

EDIT
Forgot to mention what I've tried; I have tried submitting the form to itself (same file) and that destroyed the ajax link, and opened the page. i have also tried just having the button move the page onto another step, but the $_POST variable is empty... i am at a loss, so does anyone else have any ideas?

© Stack Overflow or respective owner

Related posts about php

Related posts about AJAX