1

i am trying to get the javascript variable value to php.

here is my javascript code:

function getResults()
{
var radios = document.getElementsByName("address");    
for (var i = 0; i < radios.length; i++)
{       
if (radios[i].checked) {
var a = radios[i].value
alert(a);
break;
}
}
}

from this javascript i want to get variable a's value inside php code onclick on submit button. how can i do this?

i tried this

$var1 = $_GET["a"];
Mithun Ds
  • 131
  • 1
  • 4
  • 12

4 Answers4

1

Do this..

   $var1 = <?=$_GET["a"]?>;

to communicate a value from JavaScript to PHP do following

$var1 = <?=$_POST["a"]?>;
Rakesh KR
  • 6,357
  • 5
  • 40
  • 55
0

What you tried is almost correct:

var a = <?=$_GET["a"]?>;
Gianpaolo Di Nino
  • 1,139
  • 5
  • 17
0

Since javascript code runs on client-side, but PHP is server-side code, you need to send your JS variables to the server. You can do this using AJAX.

Hint: AJAX calls usually use POST instead of GET.

ciruvan
  • 5,143
  • 1
  • 26
  • 32
-1

Replace:

 $var1 = $_GET["a"]; 

on:

 $var1 = <?=$_POST["a"]?>;
Michael
  • 15,386
  • 36
  • 94
  • 143