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"];
1
  • Check out AJAX tutorials Commented Aug 20, 2013 at 9:30

4 Answers 4

1

Do this..

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

to communicate a value from JavaScript to PHP do following

$var1 = <?=$_POST["a"]?>;
Sign up to request clarification or add additional context in comments.

Comments

0

What you tried is almost correct:

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

1 Comment

Seems to me OP wants to know how to get JS variables to PHP, not vice versa.
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.

Comments

-1

Replace:

 $var1 = $_GET["a"]; 

on:

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

1 Comment

The goal seems to be to communicate a value from JavaScript to PHP, not the other way around.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.