Jump to content

Pass radio value to php code?


shreyaskudav

Recommended Posts

I am writing a simple code to select a radio button and pass the value of the selected radio to php code, through ajax!

Heres my code for ajax:

function sendid(attack){var att;var xmlhttp;if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safarixmlhttp=new XMLHttpRequest();}else{// code for IE6, IE5xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}xmlhttp.onreadystatechange=function(){if (xmlhttp.readyState==4 && xmlhttp.status==200){document.getElementById("result").innerHTML=xmlhttp.responseText;}}xmlhttp.open("POST","battlephp.php",true);xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");xmlhttp.send("att="+attack+"");}</script></head><body><input type="radio" value="10" name="attack" onclick="sendid('this.value')"/> Attack 1<input type="radio" value="20" name="attack" onclick="sendid('this.value')"/> Attack 2

And my php code:

$attack = intval($_POST['att']);echo "Working ".$attack;

And the Error I am getting is:

( ! ) Notice: Undefined index: att in C:wampwwwbattlephp.php on line 5Call Stack# Time Memory Function Location1 0.0003 247432 {main}( ) ..battlephp.php:0Working 0

Surely its not identifing the att which is pass through the ajax..! But how should I handle it now?? I am newbie to ajax!

Link to comment
Share on other sites

One problem I see is that you've put 'this.value' between quotes, that means you're actually passing the string "this.value". But that's not the real problem.

 

In order to figure out your problem you're going to have to print_r($_POST) to see what values it's getting.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...