dqhnp44220 2015-02-04 17:29
浏览 56
已采纳

发布到php脚本时,jQuery AJAX请求不会被触发。

I have a database table which I am trying to retrieve data from using JQUERY AJAX. When my first page loads it does a php call to a table and populates a select form element. - This works I then want to select one of the options submit the form and have the row returned via Ajax. Previously I had the script working with just PHP files but am having trouble getting it to work. When submitting the form my URL is changing:

http://localhost/FINTAN/testertester.php?name=Specifics.

I am not getting anything back. In addition when looking at my console I get a jquery not defined factory (jquery). I can find the line in question in my jquery ui.js. Not sure if this is the issue or my code has caused the issue. I have cleard the firefox cache and due to the fact I have not had a successful AJAX call via jquery method am guessing it my code.

To get the code below I have mixed and matched a book and an online tutorial and many other sources and this is not my first attempt. Ideally I would like to output table row. However just getting a request working and knowing its not a conflict or compatability issue would makeme feel better and not hindered before I start

   <script src="jquery/jquery-ui-1.11.2/jquery-ui.js"></script>
   <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

   <script>

   $(document).ready(function(){
       $("#btn").click(function(){
           var vname = $("#name").val;
       } 
   }
   $.post("addithandle1.php",
   {
       name:vname};

   function(response,status){   
       alert("recieved data-------*

Response : " + response 
               +"

Status : " + status);
   }
   }
   </script>
</head>
<body>  
    <?php
include "config.php";

if (mysqli_connect_errno($con))
{

}
else
{
  $result = mysqli_query($con, "SELECT * FROM script ");



     echo " <Form  method='post'> <label>Script :</label> <select id='name'  name='name' >";
}
    while($row = mysqli_fetch_array($result))
    {   

    echo "<option value = '".$row['scriptname']."'>".$row['scriptname']."</option>"; 
    } 
  echo "</select>";  
           echo "<button id='btn'  class='btn-search'>Load Script </button></form>";     
?>
</body></html>

This is my PHP file that I am trying to retrieve from

 <?php
include 'config.php';

$batchtype2 = $_POST['name'];


$batchtype2 = mysqli_real_escape_string($con,$batchtype2);
$sql = "SELECT * FROM script WHERE scriptname  = '".$batchtype2."' ";       


$result = mysqli_query($con,$sql);

$count=mysqli_num_rows($result);

if($count==0 ){
    echo "</br></br></br></br></br></br></br><p> No Matching results found</p>";
}

else{
    while($row = mysqli_fetch_array($result)) {

        echo '<tr><td>'.$row['scriptname'].'</td></tr>';
  echo '<tr><td>'.$row['scripthours'].'</td></tr>';
   echo '<tr><td>'.$row['scripttotal'].'</td></tr>';


 }
}
mysqli_close($con);
?>

Thanks in advance for any help

  • 写回答

2条回答 默认 最新

  • duanjiwang2927 2015-02-04 17:42
    关注

    By making the following corrections (you have some syntax issues as well as usage issues which should be revealed in your browser's console when you load this page) in your JavaScript/jQuery this will work like you expect -

    Make sure to change this line -

    var vname = $("#name").val;
    

    to this -

    var vname = $("#name").val(); // note the parentheses
    

    in your function -

    $(document).ready(function(){
        $("#btn").click(function(e){
           e.preventDefault(); // prevent the default action of the click
               var vname = $("#name").val();
               $.post("addithandle1.php", {name:vname}, function(response, status) { // POST instead of GET
                   // never use alert() for troubleshooting
                   // output for AJAX must be in the callback for the AJAX function
                   console.log("recieved data-------*
    
    Response : " + response +"
    
    Status : " + status);
                   $('#table').html(response); // put response in div
            });
        });
    });
    

    Now $_POST['name'] should get populated properly.

    To get the table to appear in your requesting page first make sure that your PHP forms the table completely.

    Add a div to your requesting page and modify the AJAX call above as shown.

    <div id="table"></div>
    

    Now, when you make a request the div on the requesting page will be updated with whatever comes back from the PHP script.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建