如何使用ajax jquery php mysqli编辑数据

时间:2017-01-19 16:28:17

标签: php jquery ajax mysqli

我很困惑这个程序中的错误,你能帮助我吗?

这是一个使用php的过程:

$nm_pel        = $_POST['nm_pelanggan'];
  $alamat        = $_POST['alamat'];
  $telepon       = $_POST['telepon'];
  $email         = $_POST['email'];
  $id_pelanggan  = $_POST['id_pelanggan'];
  $sql = "UPDATE pelanggan SET nm_pelanggan='$nm_pel',alamat='$alamat',telepon='$telepon',email='$email' WHERE id_pelanggan='$id_pelanggan' ";
  mysqli_query($link,$sql);

这是一个函数ajax:

 function updateData(str){
    var id_pelanggan = str;
    var nm_pelanggan = $('#nm-'+str).val();
    var alamat = $('#alt-'+str).val();
    var telepon = $('#tlp-'+str).val();
    var email  =$('#em-'+str).val();
    $.ajax({
      type : 'POST',
      url  : 'core/process.php?p=edit',
      data : "nm_pelanggan="+nm_pelanggan+"&alamat="+alamat+"&telepon="+telepon+"&email="+email+"&id_pelanggan="+id_pelanggan,
      success : function(data){
        viewData();
      } 
     });
  }

2 个答案:

答案 0 :(得分:0)

并且此代码表单编辑数据:

    <div class="modal fade" id="edit-<?php echo $row['id_pelanggan']; ?>" tabindex="-1" role="dialog" aria-labelledby="editLabel-<?php echo $row['id_pelanggan']; ?>">
      <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=editLabel-"<?php echo $row['id_pelanggan'];?>">Edit Data Pelanggan <?php echo $row['id_pelanggan']; ?> </h4>
          </div>
          <form>
          <div class="modal-body">
            <div class="form-group ">
                <label class="control-label requiredField" for="id_pelanggan">
                  Kode Pelanggan
                  <span class="asteriskField">
                    *
                  </span>
                </label>
                <input class="form-control" id="<?php echo $row['id_pelanggan']; ?>" value="<?php echo $row['id_pelanggan']; ?>"  type="text" disabled>
              </div>
              <div class="form-group ">
                <label class="control-label " for="nm_pelanggan">
                  Nama Pelanggan
                </label>
                <input class="form-control" id="nm-<?php echo $row['id_pelanggan']; ?>" value="<?php echo $row['nm_pelanggan']; ?>" type="text">
              </div>
              <div class="form-group ">
                <label class="control-label " for="alamat">
                  Alamat
                </label>
                <input class="form-control" id="alt-<?php echo $row['id_pelanggan'];?>" value="<?php echo $row['alamat'];?>" type="text">
              </div>
              <div class="form-group ">
                <label class="control-label " for="telepon">
                  Nomor HP
                </label>
                <input class="form-control" id="tlp-<?php echo $row['id_pelanggan'];?>" value="<?php echo $row['telepon'];?>"  type="text">
              </div>
              <div class="form-group ">
                <label class="control-label " for="email">
                  Email
                </label>
                <input class="form-control" id="em-<?php echo $row['id_pelanggan'];?>" value="<?php echo $row['email']; ?>" type="email">
              </div>
              <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Batal</button>
                <button type="submit" onclick="updateData(<?php echo $row['id_pelanggan']; ?>)"  class="btn btn-primary" >Edit Data</button>
              </div>
            </form>
          </div>
        </div>
      </div>
    </div>

答案 1 :(得分:0)

您的javascript ajax函数显示格式错误。我是否可以建议您更改发送数据的构造函数,如下所示?希望这会有所帮助。

$.ajax({
  type : 'POST',
  url  : 'core/process.php?p=edit',
  data : {nm_pelanggan:nm_pelanggan, alamat:alamat, telepon:telepon, email:email, id_pelanggan:id_pelanggan},
  success : function(data){
    viewData();
  } 
});