How to add template in codeigniter |how to create template in codeigniter| template in codeigniter 4
Create a registration form and submit data into MySQL table using jQuery Ajax in your PHP web application. In PHP without page reload submit records into database and show a success message using jQuery.
Submit Process:
- Create a register form page.
- Insert a button in form and set button type button.
- Use onsubmit() function in the form tag.
- Call function name in script.
- Store all form value in jQuery variable.
- Set a URL link of another PHP page, where you use insert query.
- Use post method for form submits.
- Return true after from submit success.
I will show you with an example and source code in this tutorial and it’s helpful for you to submit data using Ajax in PHP. So follow the example code:
Form:
<form method="post" name="myform" onsubmit="return validateform()"> <div class="form-group"> <label for="name">Name:</label> <input type="text" class="form-control" name="name" id="name" > </div> <div class="form-group"> <label for="email">Email:</label> <input type="text" class="form-control" name="email" id="email" > </div> <div class="form-group"> <label for="phone">Phone:</label> <input type="text" class="form-control" name="phone" id="phone" > </div> <div class="form-group"> <label for="Description">Description:</label> <textarea name="description" id="description"></textarea> </div> <div class="col-md-12 col-sm-12"> <input type="submit" name="submit" class="btn btn-info" value="Submit"> </div> </form>
Script:
Example 1:
<script type="text/javascript"> function validateform(){ var variable1= $("#InputBoxId1").val(); var variable2= $("#InputBoxId2").val(); var variable3= $("#InputBoxId3").val(); $.ajax({ data: {key:variable1,key:variable2,key:variable3}, type: "post", url:"form_submit.php", success: function(data){ alert("Data Submit Success"); } }); } </script>
Example 2:
<script type="text/javascript"> function validateform(){ var name = $("#name").val(); var phone = $("#phone").val(); var email = $("#email").val(); var description = $("#description").val(); $.ajax({ data: {user_name:name,user_phone:phone,user_email:email,user_description:description}, type: "post", url:"form_submit.php", success: function(data){ alert("Data Submit Success"); } }); } </script>
PHP:
<?php require_once("dbconnect.php"); $name=$_POST['user_name']; $phone=$_POST['user_phone']; $email=$_POST['user_email']; $desc=$_POST['user_description']; $qry=mysqli_query($con,"insert into `Table Name` (`name`,`phone`,`email`,`description`) values ('".$name."','".$phone."','".$email."','".$desc."')"); if($qry){ return true; }else{ return false; } ?>
Related posts