简体   繁体   English

使用AJAX和PHP将数据发送到mysql

[英]Sending data to mysql using AJAX and PHP

I am newbie to web programming. 我是Web编程的新手。 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. 我想将数据插入MySQL DB,因为我正在使用PHP,而这些数据来自Bootstrap Modal中的表单。 When I click the Save button, nothing is happening. 当我单击“ Save按钮时,没有任何反应。 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: HTML代码和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代码:

<?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. 我已使用Chrome中的开发程序,未显示任何错误。

Typo error.You have $.ajx({}) .It must be $.ajax({}) .Also send your data in object format.like this.. 错字错误。您有$.ajx({})它必须是$.ajax({}) 。还以对象格式发送数据。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM