ok so i am an avid php developer and i am starting to get into ajax so i can keep file sizes and code down to a minimum.

i am looking in the w3schools ajax examples but i am a little lost at how to send through a variable to the page i am using to process.

what i would like to do is send through an id that is being recalled from the database to tell it what to save over.

any help would be very helpful. thanks in advance

ajax

function action()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {
  // code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4)
  {
  document.myForm.time.value=xmlhttp.responseText;
  }
}
xmlhttp.open("POST","action.php",true);
xmlhttp.send(null);
}

html form

<form action="#" method="post"> 
        <button name="save" type="submit" value="Save" onclick="action();">Save</button>
<input type="text" name="address" />
</form>

ok so i am an avid php developer and i am starting to get into ajax so i can keep file sizes and code down to a minimum.

i am looking in the w3schools ajax examples but i am a little lost at how to send through a variable to the page i am using to process.

what i would like to do is send through an id that is being recalled from the database to tell it what to save over.

any help would be very helpful. thanks in advance

ajax

function action()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {
  // code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4)
  {
  document.myForm.time.value=xmlhttp.responseText;
  }
}
xmlhttp.open("POST","action.php",true);
xmlhttp.send(null);
}

html form

<form action="#" method="post"> 
        <button name="save" type="submit" value="Save" onclick="action();">Save</button>
<input type="text" name="address" />
</form>

Sending data:
The easiest way to send data to your server from the form is using _GET variables
You can take the variables from your form and add them to your string like this

function action()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {
  // code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4)
  {
  document.myForm.time.value=xmlhttp.responseText;
  }
}
var textfielddata = document.getElementByID('a_field').value;
xmlhttp.open("GET","action.php?Value="+textfielddata);
xmlhttp.send(null);
}

And this would be your new HTML

<form action="#" method="post"> 
<input id="a_field" name='SomeField' type='text' />
        <button name="save" type="submit" value="Save" onclick="action();">Save</button>
<input type="text" name="address" />
</form>

You can do this with as many variables as you like

alright. but will the address field get sent through to the php file or will i have to build in a var to send it over?

Also the id (which is a mysql row id) that i would like to send through doesn't need to be shown in a input field, and the row id is not going to be the same through out. it will be different based upon the file there looking at.

thanks

The request URL would have _GET varaibles in, just create the file action.php with the contents

<?php
print_r($_GET);
?>

and view the output in Firebug (XHTML requests section)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.