dongqiuge5435 2017-02-15 14:16
浏览 36

接收POST时,JQuery Ajax无法正常工作

I've been searching around the last few days looking for an answer but haven't found one. At this point I believe the problem to either be a result of the AJAX, or my actual form. Either way, any help would be appreciated.

So the Ajax I'm using has worked before, when using it on a much simpler form. For that reason, I simply brought it over and adapted it. The only things that I changed are the ID names and file it will load. With that said, it does work - to an extent. If the file being loaded has text, or a basic echo statement, then it will run fine and print as expected on the page.

When I start trying to have it set variables and what not, then it starts having problems. It simple spits out "Ajax failed to run". Ive tried various ways to pull an error message as well but those didn't seem to work either. There may be some small errors here and there because I've been greatly fiddling with the code for a day or two now.

HTML:

    <!DOCTYPE html>
<?php session_start(); ?>
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <!-- Standard Meta -->

  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">

  <!-- Site Properties -->
  <title>Apollo Project | Conjugate</title>

  <link rel="stylesheet" type="text/css" href="/css/semantic.css">
  <link rel="stylesheet" type="text/css" href="components/dropdown.css">

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  <script src="/components/dropdown.js"></script>
  <script src="/scripts/semantic.js"></script>    

  <style type="text/css">
  body {
    background-color: #FFFFFF;
  }
  .ui.menu .item img.logo {
    margin-right: 1.5em;
  }
  .main.container {
    margin-top: 7em;
  }
  </style>

</head>
<body>

  <div class="ui fixed inverted menu">
    <div class="ui container">
      <a href="http://semantic-ui.com/examples/fixed.html#" class="header item">
        Apollo Project
      </a>
      <a href="http://semantic-ui.com/examples/fixed.html#" class="item">Home</a>
      <div class="ui simple dropdown item">
        Dropdown <i class="dropdown icon"></i>
        <div class="menu">
          <a class="item" href="http://semantic-ui.com/examples/fixed.html#">Link Item</a>
          <a class="item" href="http://semantic-ui.com/examples/fixed.html#">Link Item</a>
          <div class="divider"></div>
          <div class="header">Header Item</div>
          <a class="item" href="http://semantic-ui.com/examples/fixed.html#">Link Item</a>
        </div>
      </div>
    </div>
  </div>


  <div class="ui main text container">

     <h3 class="ui center aligned header">Verb Conjugating</h3>
    <div id="form-content">
    <form method="post" action="" id="mainForm" >
      <!-- Selector for Chapter -->
      Chapter:
    <select name="chapter" class="ui fluid search dropdown" id="chapter">
      <option value="chAll">Any Chapter</option>
      <option disabled role="separator">Select a chapter:</option>
      <option value="ch1">Chapter 1</option>
      <option value="ch2">Chapter 2</option>
      <option value="ch3">Chapter 3</option>
      <option value="ch4">Chapter 4</option>
      <option value="ch5">Chapter 5</option>
      <option value="ch6">Chapter 6</option>
      <option value="ch7">Chapter 7</option>
      <option value="ch8">Chapter 8</option>
      <option value="ch9">Chapter 9</option>
      <option value="ch10">Chapter 10</option>
      <option value="ch11">Chapter 11</option>
      <option value="ch12">Chapter 12</option>
      <option value="ch13">Chapter 13</option>
      <option value="ch14">Chapter 14</option>
      <option value="ch15">Chapter 15</option>
      <option value="ch16">Chapter 16</option>
      <option value="ch17">Chapter 17</option>
      <option value="ch18">Chapter 18</option>
      <option value="ch19">Chapter 19</option>
      <option value="ch20">Chapter 20</option>
      <option value="ch21">Chapter 21</option>
      <option value="ch22">Chapter 22</option>
      <option value="ch23">Chapter 23</option>
      <option value="ch24">Chapter 24</option>
      <option value="ch25">Chapter 25</option>
      <option value="ch26">Chapter 26</option>
      <option value="ch27">Chapter 27</option>
      <option value="ch28">Chapter 28</option>
      <option value="ch29">Chapter 29</option>
      <option value="ch30">Chapter 30</option>
      <option value="ch31">Chapter 31</option>
      <option value="ch32">Chapter 32</option>
      <option value="ch33">Chapter 33</option>
      <option value="ch34">Chapter 34</option>
      <option value="ch35">Chapter 35</option>
    </select>   
    <br>
    <div class="ui two column doubling stackable grid">
        <!-- Selectors for Voice, Tense, Conjugation, and Mood -->
      <div class="column"> Voice:
        <select name="voice" class="ui fluid search dropdown" id="voice">
            <option value="vocAll">Any Voice</option>
            <option disabled role="separator">Select a voice:</option>
            <option value="usrAct">Active</option>
            <option value="usrPas">Passive</option>
        </select>
     </div>
      <div class="column"> Tense:
        <select name="tense" class="ui fluid search dropdown" id="tense">
            <option value="tenAll">Any Tense</option>
            <option disabled role="separator">Select a tense:</option>
            <option value="usrPres">Present</option>
            <option value="usrImp">Imperfect</option>
            <option value="usrPerf">Perfect</option>
            <option value="usrPluPerf">PluPerfect</option>
            <option value="usrFut">Future</option>
            <option value="usrFutPerf">Future Perfect</option>
        </select>
     </div>
     <div class="column"> Conjugation:
        <select name="conj" class="ui fluid search dropdown" id="conj">
            <option value="conjAll">Any Conjugation</option>
            <option disabled role="separator">Select a conjugation:</option>
            <option value="usr1st">1st</option>
            <option value="usr2nd">2nd</option>
            <option value="usr3rd">3rd</option>
            <option value="usr4th">4th</option>
        </select>
     </div>
     <div class="column"> Mood:
        <select name="mood" class="ui fluid search dropdown" id="mood">
            <option value="moodAll">Any Mood</option>
            <option disabled role="separator">Select a mood:</option>
            <option value="usrInd">Indicative</option>
            <option value="usrInf">Infinitive</option>
            <option value="usrImp">Imperative</option>
        </select>
     </div>
    </div> 
    <br>
    <input type="submit" class="ui fluid button" name="submitBtn">
    </form>
    </div>

 <!-- HTML to submit Ajax to another page -->
    <script type="text/javascript">

        $(document).ready(function() {  

            // submit form using $.ajax() method

            $('#mainForm').submit(function(e){

                e.preventDefault(); // Prevent Default Submission

                $.ajax({
                    url: 'submitDecline.php',
                    type: 'POST',
                    data: $(this).serialize() // it will serialize the form data
                })
                .done(function(data){
                    $('#form-content').fadeOut('slow', function(){
                        $('#form-content').fadeIn('slow').html(data);
                    });
                })
                .fail(function(){
                    alert('Ajax Submit Failed ...');    
                });
            }); 
        });
    </script>



<!--    
    <h3 class="ui center aligned header">Doubling Stackable Grid</h3>
    <div class="ui stackable grid">

    </div>
    <div class="ui two column doubling stackable grid">
      <div class="column">
        <div class="ui segment">Content</div>
      </div>
      <div class="column">
        <div class="ui segment">Content</div>
      </div>
      <div class="column">
        <div class="ui segment">Content</div>
      </div>
      <div class="column">
        <div class="ui segment">Content</div>
      </div>
      <div class="column">
        <div class="ui segment">Content</div>
      </div>
      <div class="column">
        <div class="ui segment">Content</div>
      </div>
    </div>
     -->
  </div>
<?php $conn->close();    
?>
  <script src="scripts/script.js"></script>

</body>
</html>

PHP file being loaded:

    <p> This is a test </p>
<?php
session_start();

if( $_POST ){   
    $chaptSelect = $_POST['chapter'];   
    $tenseSelect = $_POST['tense']; 
    $voiseSelect = $_POST['voice']; 
    $moodSelect = $_POST['mood'];   
    $conjSelect = $_POST['conj'];

    if( $chaptSelect == "Any Chapter" ) {
        echo("You selected any chapter")    
    }

?>

<p> This is a test </p>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 python的qt5界面
    • ¥15 无线电能传输系统MATLAB仿真问题
    • ¥50 如何用脚本实现输入法的热键设置
    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100