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

发布到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条)

报告相同问题?

悬赏问题

  • ¥50 adb连接不到手机是怎么回事?
  • ¥15 vs2022无法联网
  • ¥15 TCP的客户端和服务器的互联
  • ¥15 VB.NET操作免驱摄像头
  • ¥15 笔记本上移动热点开关状态查询
  • ¥85 类鸟群Boids——仿真鸟群避障的相关问题
  • ¥15 CFEDEM自带算例错误,如何解决?
  • ¥15 有没有会使用flac3d软件的家人
  • ¥20 360摄像头无法解绑使用,请教解绑当前账号绑定问题,
  • ¥15 docker实践项目