Save Radio Button Value Using PHP/MySQL
This Tutorial will teach you on how to save radio button value using PHP and MySQL.
To start this tutorial let's follow the steps below.
Creating Our Database
First we are going to create our database which stores our data.
To create a database:
1. Open PHPMyAdmin.
2. Click Databases.
3. In the "Create new database" box, type "radio".
4. The click Create button
5. Click SQL tab and paste the following code.
CREATE TABLE IF NOT EXISTS `gender` ( `id` int(11) NOT NULL AUTO_INCREMENT, `gender` varchar(10) NOT NULL, ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
Creating Our Form
Next step is to create a form and save it as index.php. To create a form, open your HTML code editor and paste the code below after body tag.
<?php $gender=$_GET['gender']; ?> <form action="save.php" method="post"> Select Gender:<br /> <label> <?php if ($gender=='male') { echo '<input name="gender" type="radio" value="male" checked="checked" />'; } if ($gender=='female') { echo '<input name="gender" type="radio" value="male" />'; } ?> male </label> <br /> <label> <?php if ($gender=='female') { echo '<input name="gender" type="radio" value="female" checked="checked" />'; } if ($gender=='male') { echo '<input name="gender" type="radio" value="female" />'; } ?> female </label><br /> <input name="Save" type="submit" value="save" /> </form>
Creating our Connection
Next step is to create a database connection and save it as "connection.php". This file is used to connect our form to our database.
<?php $mysql_hostname = "localhost"; $mysql_user = "root"; $mysql_password = ""; $mysql_database = "radio"; $prefix = ""; $bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database"); ?>
Writing Our Save Script
Next step is to create our save script and save it as save.php. This code is used to save the input data to our database.
<?php include('connection.php'); $gender=$_POST['gender']; ?>
That's it, you've been successfully created your code on how to save radio button value using php. Hope this code will help you, see you guys in my next tutorial.
- Add new comment
- 6896 reads
Comments
test
This is a great tutoral
Add new comment