javascript - Ajax 提交按钮不起作用

标签 javascript php jquery ajax submit

我正在尝试将多步表单中的输入提交到数据库(临时表),但是当我单击“提交”按钮时没有任何反应。我无法弄清楚出了什么问题,因为它在不同浏览器上的控制台日志上没有显示任何错误。我需要它先将表单输入保存到临时表,然后再要求用户在表单的最后部分登录。

这是我的多步骤表单的代码。

索引.html

<!doctype HTML>
<html>
<head>
<!-- jQuery easing plugin -->
<script src="http://sandbox.lookingfour.com/forms/multistepform/jquery.easing.min.js" type="text/javascript"></script>
<script src="http://sandbox.lookingfour.com/forms/multistepform/script.js" type="text/javascript"></script>
<script src="http://www.lookingfour.com/js/temporary_buyinfo.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="http://sandbox.lookingfour.com/forms/multistepform/style.css">
</head>
<body>
<table>
  <tr class='popupbox-header'>      
    <td>
      <span id='buytitle'></span>
      <a href='' onclick='buyform_back(); return false;'>x</a>
    </td>
  </tr>
<!-- multistep form -->
<tr>
<td>
<div id="msform">
  <!-- progressbar -->
  <ul id="progressbar">
    <li class="active">Account Setup</li>
    <li>Social Profiles</li>
    <li>Personal Details</li>
  </ul>
  <!-- fieldsets -->
    <fieldset>
    <h2 class="fs-title">First Step</h2>
    <h3 class="fs-subtitle">This is step 1</h3>
    <label>Quantity</label>
    <?php echo input_textbox('buyqty', 1, "class='numeric'",'number'); ?><span class="formdata-required">*</span><br>
    <label>Condition </label>
    <select id = 'buycondition'>
      <option value = "New">New</option>
      <option value = "Used">Used</option>
    </select><br>
      <table>
      <tr>
      <td id='buyerexamples' ></td></br>
      </tr>
      </table>
    <input type="button" name="next" class="next action-button" value="Next" />
  </fieldset>
  <fieldset>
    <h2 class="fs-title">Second Step</h2>
    <h3 class="fs-subtitle">This is step 2</h3>
    <label>Deadline</label>
    <?php echo input_datebox('buydeadline', date('m/d/Y', strtotime("+7 days")), 1); ?>
    <span class="formdata-required">*</span><br>
    <label>Notes / Message</label>
    <textarea id='buynotes' style='width: 70%; height:90px;'></textarea>
    <input type="button" name="previous" class="previous action-button" value="Previous" />
    <input type="button" name="next" class="next action-button" value="Next" />
  </fieldset>
  <fieldset>

    <h2 class="fs-title">Third Step</h2>
    <h3 class="fs-subtitle">This is step 3</h3>
    <div id='response'></div>
    <label>Location/Area</label>
              <select id = 'buylocation'>
                <option value = "Metro Manila">Metro Manila</option>
                <option value = "Abra">Abra</option>
                <option value = "Agusan del Norte">Agusan del Norte</option>
                <option value = "Agusan del Sur">Agusan del Sur</option>
              </select>
              <span class="formdata-required">*</span><br>
              <label>Budget</label>
              <?php echo input_textbox('buybudget','','placeholder="i.e. 5000 or 3500" style="width: 65%;"', '','text'); ?><br>
      <input type="button" name="previous" class="previous action-button" value="Previous" />
      <input type='submit' value='Submit' />
            <?php
              if (util_getuserid() == 0) {
                echo input_button('Save-In', '', 'id="buyloginbtn"  class="popup-button" style="background-color: #ea6e38;"');
                echo input_button('Sign-In', 'buyform_login("buyuser_login","buyuser_password","buyloginmsg");', 'id="buyloginbtn"  class="popup-button" style="background-color: #ea6e38;"');
              } else {
                echo input_button('Look for Suppliers', 'buyerform_post();', ' class="popup-button" style="background-color: #ea6e38;"');
              }
            ?>
  </fieldset>
</div>
<div class='menu-loginform' id='buyloginform' style="display:none;">
        <table width=100%>
          <tr>
            <td>
              <label style='text-align: left;'>Sign-In to proceed</label>
            </td>
          </tr>
          <tr>
            <td>
              <label style='text-align: left;'>Username / e-mail</label>
              <?php echo input_textbox('buyuser_login', '', 'class="popup-inputbox"', 'buyloginbtn'); ?>
            </td>
          </tr>
          <tr>
            <td>
              <label style='text-align: left;'>Password</label>
              <?php echo input_textbox('buyuser_password', '', 'class="popup-inputbox"', 'buyloginbtn','password'); ?>
              <a href='' OnClick='user_createtoplogin(); return false;' style='float: left;' id='buyform-createaccount'>Create a new account</a>
            </td>
          </tr>
          <tr>
            <td align=center>
              <div id='buyloginmsg' class="formdata-required" style="display:none;"></div>
            </td>
          </tr>
          <tr>
            <td>
            <?php
              if (util_getuserid() == 0) {
                echo input_button('Sign-In', 'buyform_login("buyuser_login","buyuser_password","buyloginmsg") && buyerform_post();', 'id="buyloginbtn"  class="popup-button" style="background-color: #ea6e38;"');
                echo input_button('Sign-In FB', 'Login()', 'id="status"  class="popup-button" style="background-color: #ea6e38;"');
              } else {
                echo input_button('Look for Suppliers', 'buyerform_post();', ' class="popup-button" style="background-color: #ea6e38;"');
              }
            ?>
            </td>
        </tr>
        </table>
      </div>
</td>
</tr>
</table>
<script>
$(document).ready(function(){
    $('#msform').submit(function(){

        // show that something is loading
        $('#response').html("<b>Loading response...</b>");

        /*
         * 'post_receiver.php' - where you will pass the form data
         * $(this).serialize() - to easily read form data
         * function(data){... - data contains the response from post_receiver.php
         */
        $.ajax({
            type: 'POST',
            url: 'http://www.lookingfour.com/jquery/serialize.php', 
            data: $(this).serialize()
        })
        .done(function(data){

            // show the response
            $('#response').html(data);

        })
        .fail(function() {

            // just in case posting your form failed
            alert( "Posting failed." );

        });

        // to prevent refreshing the whole page page
        return false;

    });
});
</script>
</body>
</html>

序列化.php

<?php
require_once(dirname(dirname(__FILE__))."/templates/initialize.inc");
$global_connection = db_connect();

    if($_POST["buylocation"] != '') {
    $buyinfo_temp = db_saverecord("buyinfo_temp", array(
                                    'buyinfo_location'=>$_POST['buylocation'],
                                    'buyinfo_notes'=>$_POST['buynotes'],
                                    'buyinfo_condition'=>$_POST['buycondition'],
                                ), 0, $global_connection);
}
?>

最佳答案

您正在尝试提交 div。您只能提交表单。替换

<div id="msform">
...
</div>

<form id="msform">
....
</form>

关于javascript - Ajax 提交按钮不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34261995/

相关文章:

php - 使用Laravel与Nginx和docker-compose找不到图像

php - PHP中的变量持久化

jquery - 如何在 jQuery 中转义 this\character?

javascript - 开 Jest - ReferenceError : define is not defined

javascript - 获得简单的评论器和 Bootstrap 来使用相同版本的 jQuery?

javascript - 显示不同的产品变体

javascript - JS/JQUERY iframe.contents() 语法

jquery - 根据表单输入显示元素

javascript - Vue未加载数据

javascript - 从字符串创建一个 JS 多维数组