douxian9010 2014-05-11 01:12
浏览 26
已采纳

当<form>'.submit'函数被覆盖(使用ajax)时获取数据到php?

I have overridden the .submit function of a form on this web-page, because the webpage is loaded inside the #mainContent in an "index.php", and I want the Submit button to replace only this #mainContent.

I am trying to get the data from this form to a .php file in order to make queries to a database (or simply echo populated variables, to indicate that data was passed).

I am very new to AJAX. Can someone explain to me how to pass the data to the .php file, or point me to a resource that will give me some clarification?

Right now I'm simply trying to pass a string to a variable and echo it back.

Could someone clarify what the first element in the "data: {thisElement: notThisOne}," refers to? Is it the "name" of an attribute in the form? The name of the variable it will be passing to the php script in the 'url:' ?

Thanks for clarify this.

Here is my 'search.html' file (which is embedded in another 'index.php' file):

  <!DOCTYPE html>
  <html>
  <head>

  </head>
  <body>
     <div class="main" data-role="content" id="main">
        <div id="holder">
           <h1>Search</h1>
            <div class="left">

              <form name="searchForm" id="searchForm" onsubmit="return false;">

              <fieldset data-role="controlgroup" data-type="horizontal" data-role="fieldcontain">
                 <legend><h3>Course</h3></legend>
                 <select name="courseSelect" id="courseSelect" name="courseSelect">
                    <option value="*">All</option>
                    <option value="COMM2216">COMM-2216</option>
                 </select>
              </fieldset>

                 <p>
              <fieldset data-role="controlgroup" data-type="horizontal" data-role="fieldcontain">
                       <legend><h4>Type:</h4></legend>
                       <input type="radio" name="lecLab" value="lec" id="lec"/>
                          <label for="lec">Lecture</label>
                       <input type="radio" name="lecLab" value="lab" id="lab">
                          <label for="lab">Lab</label>
                       <input type="radio" name="lecLab" value="*" id="both" checked="checked">
                          <label for="both">Both</label>

                    <p>
                    </fieldset>
                    <input type="submit" value="Go">

              </form>
            </div>
        </div>
     </div>
  <script src="./scripts/searchGo.js"></script>
  </body>
  </html>   

The 'searchGo.js':

  $(document).ready(function() {
     $("#searchForm").submit(function(e)
        {
        //e.preventDefault();
        $.ajax({
           type: "POST",
           url: "./scripts/php_test.php",
           data: {courseSelect: "Lecture, or Lab?"},
           success: function(){
              alert($lecOrLab);
              $("#holder").load("./scripts/php_test.php");              
           }
        }) ;
     });
  });

Finally, the 'php_test.php':

  <html>
  <head>
  </head>
  <body>
  <?php

     $courseSelect = $_POST['courseSelect'];

     echo ($courseSelect);
  ?>
  </body>
  </html>

Am I collecting the data in the php_test.php incorrectly? Or assigning it in the 'data: {},' (of searchGo.js) incorrectly?

Thanks for clarification.

  • 写回答

2条回答 默认 最新

  • doucigua0278 2014-05-11 02:13
    关注

    First you shouldn't perform a search with POST, you are not modifying states(Deleting or updating records). Use a GET request instead.

    The data in the ajax request is what you want to pass to the request. Use jQuery serialize that encodes a set of form elements as a string for submission.

    $.ajax({
           type: "GET",
           url: "./scripts/php_test.php",
           data: $("#searchForm").serialize(),
           success: function(data){
             $("#holder").html(data);              
           }
    });
    

    Now, in your php file you should be looking at the $_GET. Something like this:

    $courseSelect = $_GET['courseSelect'];
    
    //now query your db and return your results based on your form  fields. 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?