Click here to Skip to main content
15,879,095 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to parse javascript variable into php variable but I couldn't to do tat. I dont know what's wrong in my code.

Here is my code

test.php

   <input id="r1" type="radio" value=Article name="radios" checked>
   <input id="r2" type="radio" value=Book name="radios">
<input id="r3" type="radio"value=InBook name="radios">
  <input id="r4" type="radio" value="Phd Thesis" name="radios">
  <input id="r5" type="radio" value="Master's Thesis" name="radios">
   <input id="r6" type="radio" value=Miscellaneous name="radios">
 <button type=button onclick="radio();">Submit</button>


<script>
var art;

function radio(){
   if (document.getElementById('r1').checked) {
  art = document.getElementById('r1').value;
}
if (document.getElementById('r2').checked) {
   art = document.getElementById('r2').value;
}
if (document.getElementById('r3').checked) {
  art = document.getElementById('r3').value;
}
if (document.getElementById('r4').checked) {
  art = document.getElementById('r4').value;
}
if (document.getElementById('r5').checked) {
  art = document.getElementById('r5').value;
}
if (document.getElementById('r6').checked) {
  art = document.getElementById('r6').value;}

     sendart(art);
  }
function sendart(t)
{
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;
      }
    }
  }

  xmlHttp.open("GET","test.php?art=" + t, true);
  xmlHttp.send();
}


   
   </script>
   <?php
   if(isset($_GET['art'])){
   $s=$_GET['art'];
   echo $s;
?>




I dont know what's wrong here. Am not getting javascript variable art in my php.Thanks in advance
Posted
Updated 31-Aug-14 23:48pm
v2

1 solution

In your code you have not written anything to get the response from the php page.
I have made the changes in your javascript part which will return the value from the php page.
Kindly have a look at the below code. Hope this helps.

HTML
<html>
<head>
<script>
var art;
 
function radio(){
   if (document.getElementById('r1').checked) {
  art = document.getElementById('r1').value;
}
if (document.getElementById('r2').checked) {
   art = document.getElementById('r2').value;
}
if (document.getElementById('r3').checked) {
  art = document.getElementById('r3').value;
}
if (document.getElementById('r4').checked) {
  art = document.getElementById('r4').value;
}
if (document.getElementById('r5').checked) {
  art = document.getElementById('r5').value;
}
if (document.getElementById('r6').checked) {
  art = document.getElementById('r6').value;}
 
     sendart(art);
  }
function sendart(t)
{
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;
      }
    }
  }
/* added the following lines to get the response from the php page */
  xmlHttp.onreadystatechange=function()
  {
	if (xmlHttp.readyState==4 && xmlHttp.status==200)
    {
		document.getElementById("myDiv").innerHTML=xmlHttp.responseText;
    }
  }
/*end of added the follwing lines */
  xmlHttp.open("GET","test.php?art=" + t, true);
  xmlHttp.send();
}
</head>
<body>
<form name = "form1">
<input id="r1" type="radio" value=Article name="radios" checked>
<input id="r2" type="radio" value=Book name="radios">
<input id="r3" type="radio"value=InBook name="radios">
   <input id="r4" type="radio" value="Phd Thesis" name="radios">
   <input id="r5" type="radio" value="Master's Thesis" name="radios">
    <input id="r6" type="radio" value=Miscellaneous name="radios">
  <button type=button onclick="radio();">Submit</button>
 
</form>
</body>
</html>
 
Share this answer
 
Comments
Vidhya Raju 2-Sep-14 2:19am    
Is form needed in html part?
ChauhanAjay 2-Sep-14 3:24am    
It is a good practice to have it so i have put in the form tag. As i have seen in some browsers where the controls dont get displayed if the form tag is not used. Sorry dont remember the browser names.
Vidhya Raju 2-Sep-14 3:30am    
Can i know why this document.getElementById("myDiv").innerHTML=xmlHttp.responseText;
code is needed? Instead of that can I use

xmlHttp.onreadystatechange=function()
{
if (xmlHttp.readyState==4 && xmlHttp.status==200)
{
xmlHttp.open("GET","test.php?art=" + t, true);
xmlHttp.send();
}
}
Vidhya Raju 2-Sep-14 3:31am    
Because I need only variable to be parsed into the php page
ChauhanAjay 2-Sep-14 3:37am    
xmlHttp.onreadystatechange=function()
{
if (xmlHttp.readyState==4 && xmlHttp.status==200)
{
//result from the php page if any
}
}

This part is only required if you have to get anything back from the php page.
It was just for checking that the value which you echoed in the php page is returned to your html page or not.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900