hey huys i have a javascript ajax function that gets the parameter "id". I want to send it via post or get to a php file that gets database info based on the "id" but i cant get the id to transfer over to the php file. here is my code

function predeployment(id){
           alert(id);
	   x = x + 1	 
		if (x%2)
		{		
		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 && xmlhttp.status==200)
			{
			document.getElementById("predeployment").innerHTML=xmlhttp.responseText;
			}
		}
		xmlhttp.open("POST","predeploymentdata.php",true);
		xmlhttp.send("q=id");
		}
		else
		{
			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 && xmlhttp.status==200)
				{
				document.getElementById("predeployment").innerHTML=xmlhttp.responseText;
				}
			}
			xmlhttp.open("GET","blank.txt",true);
			xmlhttp.send();
			}
	}

here is the php code but i think it is good

include "connect.php";
$id=$_POST["q"];
echo $id;

//$id = 37;

$query = "SELECT * from deployment_implementor WHERE emop_id = $id";
$result = mysql_query($query);

Any help is greatly appreciated.

Recommended Answers

All 2 Replies

At first view, the code looks alright. You should place alerts everywhere in your javascript code. Also I would recommend you use the GET method, as POST method is a bit more difficult. So try out this function:

function predeployment(id) {

var ajax = false;

// Create the object:

// Choose the objecttype, depending on what is supported:
if (window.XMLHttpRequest) {

    // IE 7, Mozilla, Safari, Firefox, Opera, most browsers:
    ajax = new XMLHttpRequest();

} else if (window.ActiveXObject) { // Old IE-browsers

    // Make the type Msxml2.XMLHTTP, if possible:
    try {
        ajax = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e1) { // Else use the other type:
        try {
            ajax = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e2) { }
    }

}

// Retrieving the data from the page

 if (ajax) {

   var url = "predeploymentdata.php?id=" + encodeURIComponent(id);
   alert("url: " + url);

   ajax.open('GET', url);

   // Sends request
   ajax.send(null);
   
   // Function that handles response
   ajax.onreadystatechange=function(){
    // If everything is OK:
     if ( (ajax.readyState == 4) && (ajax.status == 200) ) {
          // Returns the value to the document
          alert(ajax.responseText);
	 }
    }


 } else { // AJAX is not useable
     alert('It is not possible to connect, please update your browser.');
 }

}

~G

Thanks Graphix it worked!!

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.