weixin_33725272 2020-04-02 20:50 采纳率: 0%
浏览 38

N00b的AJAX表单示例

I will begin with an apology: I'm bad with AJAX. I'm trying to learn, but this particular project is giving me an absolute headache. I've read through a lot of examples, but can't find anything that helps with this issue.

PROBLEM: Using an "old-fashioned" PHP/MySQL/HTML environment, I'm trying to send data from a simple HTML form using AJAX; send the ajax request to php for updating a record in a MySQL db. Unfortunately, my code just does not work. It seems that I am effectively sending data, but somewhere in all of this I am coming up short. NOTE: I've confirmed that there is no problem with my Database connection, I use "$db" when inputting my name, password, db_name as indicated in my "show_client.php" file.

MY GOAL: I'm hoping to: (a) Update the existing variable using this AJAX request (no refresh of page); AND (b) Display output in the div titled "case_activity_id2" after the form database row is updated and my ajax request has been submitted.

I have two relevant files: (1) index.php; and (2) show_client.php.

index.php

<html>
  <td>
        <form id= "ajaxForm" action = ""  method = "POST" >
            <input type = "hidden" name='case_activity_id' id = 'case_activity_id' value = '<?php echo $case_activity_id?>'>
            <select name ='show_client_id' id = 'show_client_id' class="form-control">
                    <?php 
                        showClient();
                    ?> 
            </select>

  </td>
  <td><input type="submit" value="send" id = "btnClick" class="btn btn-primary" /> </form></td>
  <td><div id = "case_activity_id2"></div></td>

<script>

(function(){
    $("#btnClick").on("click", function(){ submitForm();});
})();
function submitForm(){
    $(document).ready(function() {
        $("#ajaxForm").submit(function(event){
            var case_activity_id = $("#case_activity_id").val();
            var show_client_id = $("#show_client_id").val();
           $.ajax( {
              type: "POST",
              url:'show_client.php',
              dataType: 'json',
              data: {
                    case_activity_id:case_activity_id, 
                    show_client_id:show_client_id
                    },
              success:function(data) {
                $('#case_activity_id2').html(data);
                console.log(data);
              }
           });
        });
    });
}
</script>
</html> 

(2) show_client.php

<?php
session_start(); 
require 'db/connect.php';

            $show_client_id = $_POST['show_client_id']; 
            $case_activity_id = $_POST['case_activity_id']; 

            $sql = "UPDATE case_activity
                    SET show_client_id = '$show_client_id'
                    WHERE case_activity_id = '$case_activity_id'";  
            mysqli_query($db, $sql);  

?>
  • 写回答

1条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 GEO下载数据的处理报错 :函数‘Meta’标签‘"data.frame"’找不到继承方法,如何解决?
      • ¥15 DLNM模型是否可以用二分类变量作为y变量
      • ¥15 android object box 一个实体多个表怎么写
      • ¥15 temux 启用docker 服务失败
      • ¥15 Flask 使用celery发送邮件出现‘目标计算机积极拒绝‘
      • ¥60 老人用的sd卡在手机里面不知道操作了什么,导致图片和视频变成了文件,取下sd卡连接电脑就是图中的样子,后缀改为.jpg才可以,需要用系统的画图软件才能打开,文件属性还是文件,有没有批量操作的解决办法
      • ¥15 超时跳出方法代码的返回值问题
      • ¥15 汇编语言程序设计设计,ascii码求数,再求数的BCD码
      • ¥30 Mask rcnn训练自己的数据集出现问题!
      • ¥20 研究人工智能时的几个问题