dongtaidai0492 2018-08-11 17:24
浏览 36

在用AJAX加载的内容中提交用于php的表单

I've been search for a solution to this for a while and haven't been able to find one.

My company has quite a few webtools that we use for our jobs and I have been tasked with combining them all into a dashboard page. One of my coworkers started this project but never finished it before he left, so now I have to figure it out and there are quite a few problems.

The dashboard page is composed of a couple of sidebars, plus a "main" element. The sidebar includes a bunch of links that when clicked load specific php pages into the "main" via a call to jQuery AJAX. Here's a simplified example:

<html>
<head>
<script>
  $(function() {
    $("#pageToLoad").click(function() {
      $.ajax({
        url: "some_directory/pageToLoad.php",
        success: function(result) {
          $("#main").html(result);
        }
      });
    });
  });
</script>
</head>
<body>
  <a href="#pageToLoad" id="pageToLoad">Load Page</a>
  <main id="main"></main>
</body>
</html>

The problem here is that most of our tools use forms that are submitted via POST and then parsed with PHP (has to be php because it connects to our database to get information needed). Here's a simplified example of a tool page:

<?php
  $action = htmlspecialchars($_SRVER["PHP_SELF"]);
  function getResults() {
    if(isset($_POST["infoForQuery"])) {
      //Do some sort of query to database and print out results
    }
  }
?>
<body>
  <form action="<?php echo $action;?>" method="post">
    <input type="text" name="infoForQuery">
    <input type="submit" value="submit" />
  </form>
  <div>
    <?php getResults();?>
  </div>
</body>

I need to be able to use these tools inside the dashboard page, but whenever I try to submit a form, it just reloads the entire dashboard page, resulting in the POST data not existing (maybe this is because the PHP code is run before ajax loads the content into the dashboard page? not sure).

I know I can use AJAX to send the POST like so:

$(function() {
  $('form').on('submit', function (e) {
    e.preventDefault();
    $.ajax({
      type: 'post',
      url: 'some_directory/pageToLoad.php',
      data: $('form').serialize(),
      success: function() {
        alert('form submitted');
      }
    });
  });
});

However, this does not allow me to use the data collected in the form to query a database and print results out on the page.

Is it possible to accomplish this without having to leave the dashboard page, and if so, how?

Edit: I believe I've figured it out! What I wasn't realizing was that when I overrode the submit function and used ajax to send the post instead, I could actually retrieve the result of that post and put it into my page. Here's what I did:

<script>
$('form').on('submit', function (e) {
    e.preventDefault();
    $.ajax({
        url: 'some_directory/pageToLoad.php',
        type: 'post',
        data: $('form').serialize(),
        success: function(result) {
            $("#main").html(result);
        }
    });
});
</script>
  • 写回答

1条回答 默认 最新

  • 普通网友 2018-08-11 17:45
    关注

    Could be because the submit event is not firing. Since your forms are loaded dynamically do it like this:

    $(function() {
      $(document).on('submit', 'form', function (e) {
        e.preventDefault();
        $.ajax({
          type: 'post',
          url: 'some_directory/pageToLoad.php',
          data: $('form').serialize(),
          success: function() {
            alert('form submitted');
          }
        });
      });
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示