# Sending variables from javasrcipt/ajax

**URL:** <https://forum.kirupa.com/t/sending-variables-from-javasrcipt-ajax/265573>\
**Category:** Uncategorized\
**Created:** [July 10, 2008, 11:24am UTC](https://forum.kirupa.com/t/sending-variables-from-javasrcipt-ajax/265573 "2008-07-10T11:24:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![brndn](https://avatars.discourse-cdn.com/v4/letter/b/dec6dc/32.png) [@brndn](https://forum.kirupa.com/u/brndn)\
**Post date:** [July 10, 2008, 11:24am UTC](https://forum.kirupa.com/t/sending-variables-from-javasrcipt-ajax/265573/1 "2008-07-10T11:24:20Z")

</div>

what I am trying to do is send $id via a javascript/ajax method  
At present $id is blank or not being sent to the php page.

here is the php

```php
<?php 
$id;
$RETURN = "<div style='display:block; width:200px; height:300px; background:white;'>".$id."</DIV>";
echo $RETURN;
?>

```

this is the function I have been trying.  
I have made the variable static for now. but this will be (parameters)

f

```auto
unction getAjaxRequest(parameters)
    {
    
    
var xmlHttp;
try
  {
  // Firefox, Opera 8.0+, Safari
  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;
      }
    }
  }
  xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4)
      {
          tabselector.expandit(2)
          document.getElementById("TAB3").innerHTML= xmlHttp.responseText;
      //document.myForm.time.value=xmlHttp.responseText;
      }
    }
    
  xmlHttp.open("GET","php/selection-details.php?id=12" ,true);
  xmlHttp.send(null);
  
  
}

```

If anyone can share their wisdom I would much appriciate it.

---

<div class="post-metadata">

**Author:** ![brianmanden](https://avatars.discourse-cdn.com/v4/letter/b/3ec8ea/32.png) [@brianmanden](https://forum.kirupa.com/u/brianmanden)\
**Post date:** [July 10, 2008, 12:28pm UTC](https://forum.kirupa.com/t/sending-variables-from-javasrcipt-ajax/265573/2 "2008-07-10T12:28:06Z")

</div>

I´m not sure I understand your problem but the following code might be of some help to you otherwise please specify your problem.

Just click the div and the AJAX request will fire.

```php

 <?php 
 // 
$id = 1234;
$RETURN = "<div id='theDiv' style='border:1px solid red; display:block; width:200px; height:300px; background:white;' onclick='getAjaxRequest(".$id.")'>".$id."</div>";
echo $RETURN."<br/>
\r";
?>
<script>
	function getAjaxRequest(parameters){
		var xmlHttp;
		try{
			// Firefox, Opera 8.0+, Safari
			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;
				}
			}
		}
		xmlHttp.onreadystatechange=function(){
			if(xmlHttp.readyState==4){
				//tabselector.expandit(2)
				document.getElementById("TAB3").innerHTML= xmlHttp.responseText;
				//document.myForm.time.value=xmlHttp.responseText;
			}
		}
		theId = document.getElementById("theDiv").innerHTML;
		xmlHttp.open("GET","selection-details.php?id="+theId ,true);
		xmlHttp.send(null);
	}
</script>
<br/>
<div style="border:1px solid red;" id="TAB3">some text</div>

```

and the selection-details.php…

```php

<?php
	echo 'id = '.$_GET['id'];
?>

```

---

<div class="post-metadata">

**Author:** ![brndn](https://avatars.discourse-cdn.com/v4/letter/b/dec6dc/32.png) [@brndn](https://forum.kirupa.com/u/brndn)\
**Post date:** [July 10, 2008, 12:59pm UTC](https://forum.kirupa.com/t/sending-variables-from-javasrcipt-ajax/265573/3 "2008-07-10T12:59:48Z")

</div>

yeh it was the global vars on the server

$id = $\_GET[“id”];

hanks for the respose
