【问题标题】:Submit button doesn't fire提交按钮不触发
【发布时间】:2016-08-15 18:52:56
【问题描述】:

我的提交按钮突然停止工作。它将数据提交到 MySQL 数据库中。 在我进行一些设计更改时,它突然停止工作。

下面的代码中是否有任何明显的错误/错误? 我是一个菜鸟,目前正在尝试学习一些 PHP 等,所以任何帮助将不胜感激。 :)

<section id="moviesearch">
  <!-- Section -->
  <div class="subcribe2">
    <div class="container">
      <!-- Container -->
      <div class="row">
        <!-- Row -->
        <div class="col-md-4">

        </div>

        <body ng-app="myApp">
          <div ng-controller="MyCtrl" class="col-md-4">
            <form class="form-watch-list-create">
              <input required="required" placeholder="Movie title" type="text" class="form-control" typeahead="item.Title for item in getLocation($viewValue)" typeahead-loading="loadingLocations" typeahead-min-length="2" typeahead-wait-ms="1000" typeahead-on-select="onSelected($item)"
              typeahead-template-url="customTemplate.html" ng-model="asyncSelected">
              <i ng-show="loadingLocations" class="glyphicon glyphicon-refresh"></i>
          </div>


          <div class="col-md-4">
            <button type="submit" class="btn btn-block btn-sm btn-dark">Add to watchlist</button>
          </div>

        </body>
      </div>
      <!-- End Row -->
    </div>
    <!-- End Container -->
  </div>

  <script type="text/ng-template" id="customTemplate.html">
    <a>
      <span bind-html-unsafe="match.label | typeaheadHighlight:query" style="width: auto;"></span>
      ({{match.model.Year}})

      <!-- <img ng-src="{{match.model.Poster}}" width="120"> -->
    </a>
  </script>

  <div ng-controller="MyCtrl">
    <i ng-show="loadingLocations" class="glyphicon glyphicon-refresh"></i>
  </div>
</section>

编辑 下面是向 MYSQL 数据库发送数据的代码:

function addWatchList() {
    if (isset($_GET['addtowatchlist'])) {
        $name = $_GET['name'];
        $conn = dbmysql();
        $query ="INSERT INTO watch_list values(null,'{$name}','{$_SESSION['user_id']}','NOW()')";
        if (mysqli_query($conn,$query)) {
            $last = "SELECT * FROM watch_list WHERE created_by  = '{$_SESSION['user_id']}' order by id desc limit 1";
            $data = mysqli_query($conn,$last);
            while ($row = mysqli_fetch_assoc($data)) {
                include "_view.php";    
            }
        }else {
            echo "An error occurred. Please try again.";
        }
        exit;
    }
}

【问题讨论】:

  • 检查你的控制台和错误
  • 顺便说一句,这里没有php,那为什么要给它打标签呢?
  • 对不起,这是我的错误。我的头因试图解决这个问题而有点崩溃。控制台中没有发生任何事情。按钮只是没有做任何事情

标签: php forms submit


【解决方案1】:

我正在查看您的代码,缺少属性 action="somefile.php"method="get" 如果您打算使用表单提交它,您应该输入它,或者如果您打算使用 javascript 提交您的代码,您可以使用 @987654323 @我认为这就是您所缺少的。我没有从您的输入中看到您 addtowatchlist 的名称,以便 php 可以将其捕获到您的 isset。

【讨论】:

    【解决方案2】:

    提交按钮不在表单内。

    您缺少用于结束&lt;form&gt;&lt;/form&gt; 标记。但是你有 &lt;/div&gt; 匹配 &lt;div&gt; 就在 &lt;/form&gt; 之前,所以它也隐式地关闭了 &lt;form&gt; 标记。然后你有&lt;input type="submit"&gt; 。由于提交按钮不在表单内,并且没有form 标记将其与表单关联,因此当您单击它时它不知道应该提交哪个表单。

    试试这个:

    <form class="form-watch-list-create">
        <div ng-controller="MyCtrl" class="col-md-4">  
            <input 
                required="required"
                placeholder="Movie title"
                type="text"
                class="form-control"
                typeahead="item.Title for item in getLocation($viewValue)"
                typeahead-loading="loadingLocations"
                typeahead-min-length="2"
                typeahead-wait-ms="1000"
                typeahead-on-select="onSelected($item)"
                typeahead-template-url="customTemplate.html"
                ng-model="asyncSelected">
            <i ng-show="loadingLocations" class="glyphicon glyphicon-refresh"></i>
        </div> 
        <div class="col-md-4">
            <button type="submit" class="btn btn-block btn-sm btn-dark">Add to watchlist</button>
        </div>
    </form>
    

    这会将表单放在两列周围。

    【讨论】:

    • 您好 Barmar,感谢您的意见。不幸的是,这并没有奏效。提交按钮仍然不起作用。控制台或任何东西都没有错误。 :(
    • 您的 HTML 存在其他问题,例如 &lt;body&gt; 内的 &lt;section&gt;&lt;div&gt;
    • &lt;body&gt; 只能有一个,并且必须包含所有其他 HTML。
    • 你是对的。我现在已经更正了它,但是这些编辑并没有使提交按钮起作用。我用更多信息编辑了我的第一篇文章。
    • 也许还有一些 AngularJS 问题。我不是很熟悉。
    【解决方案3】:

    在form标签中使用method属性并正确结束form标签

    <form role="form" method="get" class="form-watch-list-create">
      <div ng-controller="MyCtrl" class="col-md-4">  
            <input 
                required="required"
                placeholder="Movie title"
                type="text"
                class="form-control"
                typeahead="item.Title for item in getLocation($viewValue)"
                typeahead-loading="loadingLocations"
                typeahead-min-length="2"
                typeahead-wait-ms="1000"
                typeahead-on-select="onSelected($item)"
                typeahead-template-url="customTemplate.html"
                ng-model="asyncSelected">
            <i ng-show="loadingLocations" class="glyphicon glyphicon-refresh"></i>
        </div> 
        <div class="col-md-4">
            <button type="submit" class="btn btn-block btn-sm btn-dark">Add to watchlist</button>
        </div>
    </form>

    【讨论】:

    • 嗨 Waseem,我尝试添加您的建议,但问题是一样的。什么都没发生。我用更多代码编辑了我的第一篇文章
    • 我看到你的修改,你可以在form标签中使用method="get"
    • 我不确定我是否理解。为什么要使用 method="get" 进行发布操作?
    • 因为你已经使用 $name = $_GET['name'];在你的函数中。
    猜你喜欢
    • 2021-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-17
    • 1970-01-01
    • 2016-05-12
    • 2017-02-15
    • 1970-01-01
    相关资源
    最近更新 更多