doupu5941 2012-01-27 15:28
浏览 89
已采纳

如何使用下拉列表中的JQuery使用Ajax显示.php文件?

Basically, I have populated a dropdown list using php from a database on a page called admin.php.

I'm now using JQuery with Ajax so that when a surname is clicked from the dropdown menu.

It will call employerProfile.php and show it in the <div id="showEmployerProfile"> but my code doesn't seem to be working.

There are no errors, just the function doesn't want to work.

Here's the JS:

//SHOW EMPLOYER PROFILE FOR ADMIN
$(".eNameData").click(function() {
var eNameData = $(this).val();
    var dataToSend = 'eName=' + eNameData;

    $.ajax({                
        url: "includes/employerProfile.php", 
        type: "POST",
        data: dataToSend,     
        cache: false,
        success: function(html)
        {
        $("#showEmployerProfile").show();
            }
    });
  }
);  

Here is the code for the admin.php:

 <script type="text/javascript" src="/js/jq.js">
 </script> 

  <div id="pMainDashBoard">

    <?php /*?>DROP DOWN MENU TO SELECT EMPLOYER<?php */?>

  <form id="employer" method="get">
    <select name = "selectEmployer" id = "selectEmployer">
          <?php

          include "database_conn.php";
          $sql = "SELECT surname FROM employer";
          mysql_query($sql) or die (mysql_error());
          $queryresult = mysql_query($sql) or die(mysql_error());

        while ($row = mysql_fetch_assoc($queryresult)) {
            $eName = $row['surname'];
            echo"<option value = \"$eName\">$eName</option>
";
                        }
            mysql_free_result($queryresult);
            mysql_close($conn);
        ?>
      </select>
      </form>
   </p>
<div id ="showEmployerProfile">

 </div>

employerProfile.php:

employerProfile has $userID = $_GET['userID']; then the database_conn.php. SQL query is stated as:

$query = SELECT * FROM employer WHERE userID = '$userID';

but has " " around the sql then every div following this statement is echo'ed and has " " around each tag.

        echo <div id=\"empName\">;
        echo <h2>;
        echo $row["forename"] .' ';
        echo $row["surname"];
        echo  - ;
        echo $row["position"];
        echo </h2>;
        echo </div>;




        echo <div id=\"employerHead\">;

        echo <div id=\"empCompName\">;
        echo <h2>Company Name</h2>;
        echo $row["companyName"];
        echo </div>;

                    ..blah blah

        echo "</div>";

}


?>

Any help is much appreciated, thank you. T.J

  • 写回答

4条回答 默认 最新

  • duanaozhong0696 2012-01-27 15:51
    关注

    It depends on what employerProfile.php looks like exactly, but assuming that it returns html (you are echoing html for example), you need to use that html to populate your showEmployerProfile div.

    So you would need to change:

    success: function(html)
        {
          $("#showEmployerProfile").show();
        }
    

    to something like:

    success: function(php_output)    // using a different name for clarity
        {
          $("#showEmployerProfile").html(php_output).show();
        }
    

    And you need to change the first part to something like:

    $("#selectEmployer").change(function() {
      var eNameData = $(this).val();
    

    That way you are reacting on changes in the dropdown selection.

    Edit: Based on your comment, you need to supply the userID to your ajax function and employerProfile.php. You would need to change how your select is built in admin.php:

    $sql = "SELECT userID, surname FROM employer";     // changed
    mysql_query($sql) or die (mysql_error());
    $queryresult = mysql_query($sql) or die(mysql_error());
    
    while ($row = mysql_fetch_assoc($queryresult)) {
        $eName = htmlspecialchars($row['surname']);    // changed
        $userID = intval($row['userID']);              // added, assuming userID is an integer
        echo"<option value = \"$userID \">$eName</option>
    ";    // changed
    }
    

    Now your select options will have a value that corresponds to a userID.

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

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?