I have been trying to post a value from a radiobutton selection into my database. My datatype in mysql is int. I want value to be 1 or 0. But I believe it takes the value from the radio input type as a string. I need help in posting a value from a radiobutton into my database.

Recommended Answers

All 8 Replies

Post your code.

The form

<form>

<input type="radio" value="1" name="ys">

</form>

<?php
$radiobutton=$_POST['ys'];

insert into tblvalues(op)values('{$radiobutton}')
?>

If you don't mention form posting method [POST/GET], it will take the default value, ie., GET. So, $_POST will be empty.

Apart from that, It should work.

Its a post method on the form tag.Sorry didn't put that in

Then it *should* work. Unless you post your complete code, we won't be able to pin-point where exactly the problem is.

Are there sample codes where I can use a radiobutton to submit and int value all the ones I have seen have been for string values.

:) I used your code to insert radio button value to column age with datatype int.

<html>
<body>
<form method='post'>
<input type="radio" value="1" name="ys">
<input type='submit' name='submit' value='submit'>
</form>
</body>
</html>
<?php
if(isset($_POST['submit'])) {
	$con = mysql_connect("localhost","root");
	mysql_select_db("test2");
	$radiobutton=$_POST['ys'];
	mysql_query("insert into a (age) values ('{$radiobutton}')");
}
?>

If this *isn't working*, you should explain what is not working and post errors if you are getting any.

:) I used your code to insert radio button value to column age with datatype int.

<html>
<body>
<form method='post'>
<input type="radio" value="1" name="ys">
<input type='submit' name='submit' value='submit'>
</form>
</body>
</html>
<?php
if(isset($_POST['submit'])) {
	$con = mysql_connect("localhost","root");
	mysql_select_db("test2");
	$radiobutton=$_POST['ys'];
	mysql_query("insert into a (age) values ('{$radiobutton}')");
}
?>

If this *isn't working*, you should explain what is not working and post errors if you are getting any.

you can try type casting your post
$string = (int) $string; this will ensure that it is an integer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.