使用ajax从函数重新加载php数据

时间:2016-12-04 07:01:51

标签: javascript php jquery mysql ajax

尝试我的第一个php程序。它从mySQL数据库中提取数据加载html。我希望能够根据菜单选择更改加载的数据。我不确定自己需要做什么。但这是我到目前为止所尝试过的。

function load_comments($UsersID,$username,$ProjectID) {  
    global $connection;

    $sql = "SELECT * FROM UserComments JOIN Projects ON UserComments.ProjectID = Projects.ProjectID WHERE UserID = '$UsersID' AND Projects.ProjectID = '$ProjectID'"; 
    $result = mysqli_query($connection, $sql) or die(mysqli_error($connection));

    while ($row = mysqli_fetch_array($result, MYSQL_ASSOC)) {
        echo '<p class="note"><span></span>' . nl2br($row['UserComment']) . ' ';
        if ( empty($row['FileName']) ) {
            echo '<br><br><span id="rightfloat"><font color="#5bc0de">' . $username . '   at   ' . $row['DateSubmitted'] . '</font></span></p>';
        }else {
            $echo_file = '<a href="' . $row['UserFile'] . '"><img src="images/Attach-icon.png">' . $row['FileName'] . '  </a>';
            echo '<br><br><span id="rightfloat"><font color="#5bc0de">' . $echo_file . ' ' . $username . '   at   ' . $row['DateSubmitted'] . '</font></span><br></p>'; 
        }
    }  
    return;
}

function load_projects($UsersID) {
    global $connection;
    global $ProjectID;

    $sql = "SELECT Projects.ProjectID, Projects.ProjectName, Users.UserID FROM Projects JOIN CustomerProject ON CustomerProject.ProjectID = Projects.ProjectID JOIN Users ON CustomerProject.UserID = Users.UserID WHERE CustomerProject.UserID = '$UsersID'";
    $result = mysqli_query($connection, $sql);

    while ($row = mysqli_fetch_array($result, MYSQL_ASSOC)) {
        $idname = "ProjID-" . $row['ProjectID'];
        echo '<a id="' . $idname . '" href="#" onclick="FindID(\'' . $idname . '\')">' . nl2br($row['ProjectName']) . '</a>';
    }   

    echo "
        <script type=\"text/javascript\">
            function FindID(id_name) {
                var sel_name = document.getElementById(id_name);
                var sel_project_name = sel_name.textContent;

                $jQuery.ajax({
                    type: 'POST',
                    url: \"comment-data.php\",
                    data: { 'proj_name': sel_project_name },
                    success: function(){ alert('success'); }, 
                    error: function(){ alert('failure'); }
                });
            }
        </script>
    ";
}

PHP代码(与上面HTML相同的页面)

require("DBConnection.php");
require_once("session.php");

$proj_name = $_POST['proj_name'];

$sql = "SELECT * Projects WHERE ProjectName = " . $proj_name;

$result = mysqli_query($connection, $sql) or die(mysqli_error($connection));
$count = mysqli_num_rows($result);

if (!empty($result)){
     $row=mysqli_fetch_row($result);
     $ProjectID = $row['ProjectID'];

   // here was my feeble attempt to reload my comment data after the user selected a project from the db. This was not working, so I've commented out

   //  load_comments($UsersID, $login_session, $ProjectID);
} else {
    echo "Project not found in database!";
}

最终,我想使用success:部分根据用户菜单选择(项目)重新加载我的评论数据。我真的不知道该怎么做;但是现在我一直试图看看是否有成功或错误。这对我来说也很困难:/

这是comment-data.php:

{{1}}

1 个答案:

答案 0 :(得分:0)

好的,你的项目很好但是你需要知道:

当你在php中创建一个类时,最好将它们收集在一个没有任何html的php类文件中,这样当你需要的时候就像那样调用它们,例如:

your.php

<?php
include ("class.php")
?>
<html>
//some html code for your website
 <section id="UserComments" style="margin-left: 3em;">
    <?php load_comments($UsersID, $login_session, $ProjectID); echo "<br />" ?>
  </section>
</html>

现在你已经准备就绪,你可以在comment-data.php打电话给你:

include ("class.php");
require("DBConnection.php");
require_once("session.php");

$proj_name = $_POST['proj_name'];

$sql = "SELECT * Projects WHERE ProjectName = " . $proj_name;

$result = mysqli_query($connection, $sql) or die(mysqli_error($connection));
$count = mysqli_num_rows($result);

if (!empty($result)){
     $row=mysqli_fetch_row($result);
     $ProjectID = $row['ProjectID'];
     load_comments($UsersID, $login_session, $ProjectID);
     // it should work now
} else {
    echo "Project not found in database!";
}

现在对于ajax部分,我发现了一种方法来获取html并将其放在页面中的某个位置,但我不确定,所以在这里。

class.php

function load_comments($UsersID,$username,$ProjectID) {  
    global $connection;

    $sql = "SELECT * FROM UserComments JOIN Projects ON UserComments.ProjectID = Projects.ProjectID WHERE UserID = '$UsersID' AND Projects.ProjectID = '$ProjectID'"; 
    $result = mysqli_query($connection, $sql) or die(mysqli_error($connection));

    while ($row = mysqli_fetch_array($result, MYSQL_ASSOC)) {
        echo '<p class="note"><span></span>' . nl2br($row['UserComment']) . ' ';
        if ( empty($row['FileName']) ) {
            echo '<br><br><span id="rightfloat"><font color="#5bc0de">' . $username . '   at   ' . $row['DateSubmitted'] . '</font></span></p>';
        }else {
            $echo_file = '<a href="' . $row['UserFile'] . '"><img src="images/Attach-icon.png">' . $row['FileName'] . '  </a>';
            echo '<br><br><span id="rightfloat"><font color="#5bc0de">' . $echo_file . ' ' . $username . '   at   ' . $row['DateSubmitted'] . '</font></span><br></p>'; 
        }
    }  
    return;
}

function load_projects($UsersID) {
    global $connection;
    global $ProjectID;

    $sql = "SELECT Projects.ProjectID, Projects.ProjectName, Users.UserID FROM Projects JOIN CustomerProject ON CustomerProject.ProjectID = Projects.ProjectID JOIN Users ON CustomerProject.UserID = Users.UserID WHERE CustomerProject.UserID = '$UsersID'";
    $result = mysqli_query($connection, $sql);

    while ($row = mysqli_fetch_array($result, MYSQL_ASSOC)) {
        $idname = "ProjID-" . $row['ProjectID'];
        echo '<a id="' . $idname . '" href="#" onclick="FindID(\'' . $idname . '\')">' . nl2br($row['ProjectName']) . '</a>';
    }   

    echo "
        <script type=\"text/javascript\">
            function FindID(id_name) {
                var sel_name = document.getElementById(id_name);
                var sel_project_name = sel_name.textContent;

                $jQuery.ajax({
                    type: 'POST',
                    url: \"comment-data.php\",
                    data: 'proj_name='+ sel_project_name,
                    success : function(code_html, status){
                      $(code_html).appendTo("#somewhereinthehtml");
                      alert('success');
                    }, 
                    error: function(){ alert('failure'); }
                });
            }
        </script>
    ";
}