1

i have this code and when i press on insert after entering data it dosent seem on the database at phpmyadmin . iam using wamp localhost which support Mysqli ..

here i put the server.php and index.html codes

server.php

<?php 
 $db = new PDO('mysql:host=localhost;dbname=ajaxdata','root','');
  $page=isset($_GET['p'])?$_GET['p']:'';
   if($page=='add'){
      $name = $_POST['nm'];
      $email = $_POST['em'];
       $phone = $_POST['hp'];
       $address = $_POST['al'];
      $stmt = $db->prepare("insert into crud values('',?,?,?,?)");
     $stmt-> bindparam(1, $name);
     $stmt-> bindparam(2, $email);
     $stmt-> bindparam(3, $phone);
    $stmt-> bindparam(4, $address);
     $stmt-> execute();


    }
 else if ($page=='edit'){}
else if($page=='del'){}
else {}

 ?>

index.php

<!DOCTYPE html>
 <html lang="en">
   <head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head                    content must come *after* these tags -->
<title>BCRUD Ajax PHP</title>

<!-- Bootstrap -->
<link href="css/bootstrap.min.css" rel="stylesheet">

<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
  <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
    </head>
       <body>
<p><br/></p>
<div class="container">
        <p></p>
             <button class="btn btn-primary"  data-toggle="modal" data-target="#addData">Insert Data</button>
                <!-- Modal -->
                    <div class="modal fade" id="addData" tabindex="-1" role="dialog" aria-labelledby="addlabel">
                        <div class="modal-dialog" role="document">
                            <div class="modal-content">
                                 <div class="modal-header">
                                 <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span></button>
                         <h4 class="modal-title" id="addLabel">Insert Data</h4>
                         </div>
                         <form>
                  <div class="modal-body">

                                  <div class="form-group">
                                    <label for="nm">Full name</label>
                                    <input type="text" class="form-control" id="nm" placeholder="Name Lengkap">
                                  </div>
                                  <div class="form-group">
                                    <label for="em">Email</label>
                                    <input type="email" class="form-control" id="em" placeholder="surel">
                                  </div>
                                  <div class="form-group">
                                    <label for="hp">Phone number</label>
                                    <input type="number" class="form-control" id="hp" placeholder="Nomortelp / HP">
                                  </div>
                                  <div class="form-group">
                                    <label for="al">Address</label>
                                    <textarea type="password" class="form-control" id="al" placeholder="alamat"></textarea>
                                  </div>

                    </div>
                        <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                        <button type="submit" onclick="savedata()" class="btn btn-primary">Save</button>
                  </div>
                  </form>
                </div>
              </div>
            </div>
     <p></p>
 </div>

<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="js/jquery-3.1.1.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="js/bootstrap.min.js"></script>
<script>
function savedata(){
var name = $('#nm').val();
var email = $('#em').val();
var phone = $('#hp').val();
var address = $('#al').val();
$.ajax({
type:"POST",
url: "server.php?p=add",
data:"nm="+name+"&em="+email+"&hp="+phone+"&al="+adress,
success : function(msg){
alert('success insert data');}
});

}
</script>
    </body>
        </html>
Dharman
  • 21,838
  • 18
  • 57
  • 107
Jack math
  • 21
  • 4

0 Answers0