javascript - Radio button value not inserting into database -
i have problem radio button value doesn't inserted database. i'm using ajax in php file request values , 1 of them radio button selected.
in external javascript file, variable "gender" assigned correctly according selected radio button. however, query executed in php file not insert radio button's value database @ all. rest of values inserted except radio button appears blank once table displayed.
note: i'm not using form.
these codes php file:
. . other codes . . function insertrow($name, $address, $phone, $gender, $nation){ $table_info = "info"; $query_string = "insert $table_info(name, address, phone, gender, nation) values('$name', '$address', '$phone', '$gender', '$nation');"; $result = @mysql_query($query_string) or die (mysql_error()); } ?> <?php if(isset($_request['id'])){ $id = rtrim($_request['id']); } if(isset($_request['name'])){ $name = $_request['name']; } if(isset($_request['address'])){ $address = $_request['address']; } if(isset($_request['phone'])){ $phone = $_request['phone']; } if(isset($_request['gender'])){ $gender = $_request['gender']; } if(isset($_request['nation'])){ $nation = $_request['nation']; } $action = rtrim($_request['action']); if($action=="delete"){ echo deleterow($id); echo update_database(); } elseif($action=="insert"){ echo insertrow($name, $address, $phone, $gender, $nation); echo update_database(); } else{ echo update_database(); } ?>
these codes javascript file:
. . other functions . . function insertthis(){ var name = document.getelementbyid("name").value; var address = document.getelementbyid("address").value; var phone = document.getelementbyid("phone").value; var gender; var radiobuttons = document.getelementsbyname("gender"); var nation = document.getelementbyid("nation").value; var page = "database.php"; for(var i=0, length=radiobuttons.length; i<length; i++){ if(radiobuttons[i].checked){ gender = radiobuttons[i].value; break; } } var parameters = "name="+name+"&address="+address+"&phone="+phone+"&gender="+gender+"&nation="+nation+"&action=insert"; var xmlhttp = new xmlhttprequest(); if(xmlhttp==null){ alert("your browser not support ajax!"); return false; } xmlhttp.onreadystatechange=function(){ document.getelementbyid("divtable").innerhtml=xmlhttp.responsetext; }; xmlhttp.open("get", page+"?"+parameters, true); xmlhttp.send(null); }
and lastly, radio buttons in html file:
<td>gender:</td> <td><input checked type="radio" name="gender" id="male" value="male">male <input type="radio" name="gender" id="female" value="female">female</td>
you can change code this.
replace:
for(var i=0; length=radiobuttons.length; i<length; i++)
with:
for(var i=0; i< radiobuttons.length; i++)
rewrite:
for(var i=0; i< radiobuttons.length; i++){ if(radiobuttons[i].checked){ gender = radiobuttons[i].value; break; } }
Comments
Post a Comment