Send over values using AJAX?

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

Send over values using AJAX?

Post by Sindarin »

I am trying to use AJAX method to delete posts in my cms. However I don't know how to pass the id variable.

At xmlHttp.open("GET","delete.php",true); I am guessing it would be like xmlHttp.open("GET","delete.php?id=1",true); but what is the equivalent in javascript for php $_GET[]?

Code: Select all

function ajaxCall()
{
var xmlHttp;
try
  {
  // Firefox, Opera, Safari, Chrome
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
  
  /*  START AJAX CALL */
  
  xmlHttp.onreadystatechange=function()
    {
    
      if(xmlHttp.readyState==0)
      {
      //request is not initialized
      document.form1.showmsg.value='not started...';
      }
      if(xmlHttp.readyState==1)
      {
      //request has been started
      document.form1.showmsg.value='has started...';
      }
      if(xmlHttp.readyState==2)
      {
      //request has been sent
      document.form1.showmsg.value='has been sent...';
      }
      if(xmlHttp.readyState==3)
      {
      //request is pending
      document.form1.showmsg.value='is loading...';
      }
      if(xmlHttp.readyState==4)
      {
      //request is complete
      document.form1.showmsg.value=xmlHttp.responseText;
      }
    }
    
  xmlHttp.open("GET","delete.php",true);
  xmlHttp.send(null);
  
  
  /* END OF AJAX CALL */
  }

Code: Select all

<?php
 
//delete.php
 
require_once('database-connect.php');
$id=$_GET['id'];
 
//delete article from database - QUERY
$delete_article_query="DELETE FROM articles WHERE article_id=$id";
//update database
mysql_query($delete_article_query,$db_connection) or die(mysql_error());
 
//notification
echo "<div id='notice' ><div style='background-color:#FFFF66;width:690px;height:20px;text-indent:4px;padding:10px;'><img src='files/images/layout/alert.png' alt='alert' /><b> Article was deleted!</b></div></div>";
 
/* END OF deleteArticle */
 
?>
Also how can I display the response text as HTML in a div?

e.g. This won't work,

Code: Select all

var showmsgdiv=document.getElementById('showmsg');
 
      if(xmlHttp.readyState==0)
      {
      //request is not initialized
      document.showmsgdiv.value='not started...';
      }
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: Send over values using AJAX?

Post by papa »

The last part you can try:

document.getElementById('showmsg').innerHTML= 'Papa is tired...';
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

Re: Send over values using AJAX?

Post by Sindarin »

Thanks that works, however still haven't found any js equivalent to GET variables and send them to the php script.
One workaround I have is to use function ajaxCall($get_id) where $get_id is the article id. This actually works but don't know if it's the only or correct way it can be done.
Post Reply