Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!doctype html>
- <html>
- <head>
- <meta charset="UTF-8">
- <title>OnClick Insert Radio Button value into Database using PDO in
- Jquery Ajax
- PHP | SoftAOX Tutorial</title>
- <script
- src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js">
- </script>
- </head>
- <body>
- <h1> On Click Insert Radio Button Value into Database</h1>
- <input type="radio" name="gender" value="Male">Male<br/><br/>
- <input type="radio" name="gender" value="Female">Female<br/><br/>
- <input type="radio" name="gender" value="Others">Others<br/>
- <input type="submit"><br/>
- <h3 id="result"></h3>
- <br/>
- <script>
- $(document).ready(function(){
- $('input[type="submit"]').click(function(){
- var gender = $(this).val();
- $.ajax({
- url:"insert.php",
- method:"POST",
- data:{gender:gender},
- success: function(data){
- $('#result').html(data);
- }
- });
- });
- });
- </script>
- </body>
- </html>
- <?php
- //Insert Data
- $hostname = "localhost";
- $username = "root";
- $password = "";
- $databasename = "tut";
- try
- {
- $conn = new PDO("mysql:host=$hostname;dbname=$databasename",$username,
- $password);
- $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- if(isset($_POST["gender"]))
- {
- $query = "INSERT INTO gender(gender) VALUES (:gender)";
- $statement = $conn->prepare($query);
- $statement->execute(
- array(
- 'gender' => $_POST["gender"]
- )
- );
- $count = $statement->rowCount();
- if($count > 0)
- {
- echo "Data Inserted Successfully..!";
- }
- else
- {
- echo "Data Insertion Failed";
- }
- }
- }
- catch(PDOException $error)
- {
- echo $error->getMessage();
- }
- ?>
Add Comment
Please, Sign In to add comment