douxian9010 2014-05-10 17: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-10 18: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条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥100 冷钱包突然失效,急寻解决方案
  • ¥15 下载honeyd时报错 configure: error: you need to instal a more recent version of libdnet
  • ¥15 距离软磁铁一定距离的磁感应强度大小怎么求
  • ¥15 霍尔传感器hmc5883l的xyz轴输出和该点的磁感应强度大小的关系是什么
  • ¥15 vscode开发micropython,import模块出现异常
  • ¥20 Excel数据自动录入表单并提交
  • ¥30 silcavo仿真,30分钟,只需要代码
  • ¥15 FastReport 怎么实现打印后马上关闭打印预览窗口
  • ¥15 利用3支股票数据估计其均值和方差的95%置信区间。
  • ¥15 微信小程序运行一项功能时,弹出未知错误弹框,检查代码没有问题
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部