Insert And Retrieve Data in MySQL with $.post Noob Question



PHP Snippet 1:

<!DOCTYPE html>
<html>
<head>
    <title>Insert data in MySQL database using Ajax</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<div style="margin: auto;width: 60%;">
    <div class="alert alert-success alert-dismissible" id="success" style="display:none;">
      <a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
    </div>
    <form id="fupForm" name="form1" method="post">
        <div class="form-group">
            <label for="email">Name:</label>
            <input type="text" class="form-control" id="name" placeholder="Name" name="name">
        </div>
        <div class="form-group">
            <label for="pwd">Email:</label>
            <input type="email" class="form-control" id="email" placeholder="Email" name="email">
        </div>
        <div class="form-group">
            <label for="pwd">Phone:</label>
            <input type="text" class="form-control" id="phone" placeholder="Phone" name="phone">
        </div>
        <div class="form-group" >
            <label for="pwd">City:</label>
            <select name="city" id="city" class="form-control">
                <option value="">Select</option>
                <option value="Delhi">Delhi</option>
                <option value="Mumbai">Mumbai</option>
                <option value="Pune">Pune</option>
            </select>
        </div>
        <input type="button" name="save" class="btn btn-primary" value="Save to database" id="butsave">
    </form>
</div>
<p id="target">
</p>

<script>
$(document).ready(function() {
$('#butsave').on('click', function() {
var name = $('#name').val();
var email = $('#email').val();
var phone = $('#phone').val();
var city = $('#city').val();
if(name!="" && email!="" && phone!="" && city!=""){
    $.ajax({
        url: "save.php",
        type: "POST",
        data: {
            name: name,
            email: email,
            phone: phone,
            city: city              
        },
        cache: false,
        success: function(dataResult){
            var dataResult = JSON.parse(dataResult);
            if(dataResult.statusCode==200){
                $("#butsave").removeAttr("disabled");
                $('#fupForm').find('input:text').val('');
                $("#success").show();
                $('#success').html('Data added successfully !');    
$('#target').load('show.php');                  
            }
            else if(dataResult.statusCode==201){
                alert("Error occured !");
            }
            
        }
    });

    }
    else{
alert('Please fill all the fields !');
}
});
});
</script>
</body>
</html>

PHP Snippet 2:

<?php
    include 'database.php';
    $name=$_POST['name'];
    $email=$_POST['email'];
    $phone=$_POST['phone'];
    $city=$_POST['city'];
  $sql = $con->prepare("INSERT INTO `crud`( `name`, `email`, `phone`, `city`) VALUES (?,?,?,?)");
$sql->bind_param("ssss", $name, $email, $phone, $city);
$rc = $sql->execute();

    if (true===$rc) {
        echo json_encode(array("statusCode"=>200));
    } 
    else {
        echo json_encode(array("statusCode"=>201));
    }
      //connection closed.
$sql->close();
 $con->close();
?>

PHP Snippet 3:

<?php
include 'database.php';
// Check connection
if ($con->connect_error) {
  die("Connection failed: " . $con->connect_error);
} 

$query = "SELECT name, email, phone, city FROM crud";

$result = $con->query($query);

if ($result->num_rows > 0) {
  // output data of each row

/* fetch associative array */
while ($row = $result->fetch_assoc()) {
    echo $row["name"]. " " . $row["email"]. " " . $row["phone"]. " " . $row["city"]."<br>";
}
} else {
  echo "0 results";
}
  

$result -> free_result();
$con->close();
?>

PHP Snippet 4:

<?php
    $servername = "localhost";
    $username = "user";
    $password = "secret";
    $db="school";
    $con = mysqli_connect($servername, $username, $password,$db);
?>