0

I am newbie to web programming. I would like to insert data into MySQL DB, for that I am using PHP and this data is coming from a form in a Bootstrap Modal. When I click the Savebutton, nothing is happening. I been following a this tutorial and I have done exactly that tutor is doing. What went wrong with my code?

HTML Code and JavaScript:

 <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="addlLabel">Insert Data</h4>
            </div>
            <form>
            <div class="modal-body">

            <div class="form-group">
                <label for="nm">Name</label>
                <input type="text" class="form-control" id="nm" placeholder="Name here">
              </div>
              <div class="form-group">
                <label for="em">Email</label>
                <input type="email" class="form-control" id="em" placeholder="Email">
              </div>
              <div class="form-group">
                <label for="hp">Hone Number</label>
                <input type="number" class="form-control" id="hp" placeholder="Your Phone">
              </div>
              <div class="form-group">
                <label for="al">Address</label>
                <textarea class="form-control" id="al" placeholder="Your address"></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>
</div>

<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="js/jquery-3.1.1.min.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();

    $.aja({
       type:"post",
       url:"server.php?p=add",
       data:"nm="+name+"&em="+email+"&hp="+phone+"&al="+address,
       success: function(msg){
        alert('Success Insert data');
       }
    });

  }
</script>

PHP Code:

<?php 
$db=new PDO('mysql:host=localhost;dbname=ajax','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 users values('',?,?,?,?)");
 $stmt->bindParam(1,$name);
 $stmt->bindParam(2,$email);
 $stmt->bindParam(3,$phone);
 $stmt->bindParam(4,$address);

 if ($stmt->execute()) {
    echo "Success";
 }else{
    echo "Fail".mysqli_error();
 }
}else if($page=='edit'){

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

}else{

 }
?>

I have used the development in Chrome, no error is being shown.

6
  • print_r($_GET); and print_r($_POST); please share...... Commented Feb 19, 2017 at 9:54
  • 2
    Not $.aja({ it must be $.ajax({. Commented Feb 19, 2017 at 9:54
  • let me resolve that Commented Feb 19, 2017 at 9:55
  • @Naincy I tried ` print_r($_POST);` it showed nothing that's why I realised the data was not being sent...but thanks for the tip. Commented Feb 19, 2017 at 10:02
  • @Giovanrich send data in object{} format.below in answer. Commented Feb 19, 2017 at 10:05

1 Answer 1

2

Typo error.You have $.ajx({}).It must be $.ajax({}).Also send your data in object format.like this..

 $.ajax({
       type:"post",
       url:"server.php?p=add",
       data:{nm:name,em:email,hp:phone,al:address},
       success: function(msg){
        alert('Success Insert data');
       }
    });
Sign up to request clarification or add additional context in comments.

3 Comments

Why does the dev tools not complain or show it? Thanks that was the error. It is now working, will accept your answer
See error on console tab.Sure there must be an error.
I agree it must show and error but when I putajx console shows nothing other than a a blue >

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.