如何使用jquery ajax从php文件中获取一个变量到另一个php文件

时间:2017-05-08 05:35:41

标签: php jquery html ajax

我在使用jquery ajax将我的php文件中的变量传递给另一个php文件时遇到了麻烦。我不知道为什么它不读它,因为每次我回应变量它是未定义的或只是空白。

teams.php

<?php
    // Display members from database
    $connect = mysqli_connect('localhost', 'root', '', 'chansandbox');

    if(isset($_POST['display'])){
        $team = "SELECT * FROM team";
        $resultTeam = mysqli_query($connect, $team);
?>
        <ul class="teams">
                <li><a id="teamheader">LIST OF TEAMS</a></li>
        </ul>
<?php
        while($arrayTeam = mysqli_fetch_array($resultTeam)){
            $team_id = $arrayTeam['team_id'];
?>
            <ul class="teams" name="teamId" value="<?php echo $team_id ?>">
                <li><a href="#"><?php echo $arrayTeam['team_name']; ?></a>
                    <ul class="menu">
                        <li class="memberdivholder">
                            <div class="gradeview">
                                <h1 id='options'><i class="fa fa-cog"></i>
                                    Options:
                                    <button class="topbutton">Grade Team</button>
                                    <button class="topbutton">View Team</button>
                                    <button class="teamdelete">Delete Team</button>
                                </h1>
                            </div>
                            <?php
                                $member = "SELECT * FROM employee WHERE team_team_id = '$team_id' ORDER BY position_pos_id=17 desc";
                                $resultMember = mysqli_query($connect, $member);
                                while($arrayMember = mysqli_fetch_array($resultMember)){
                            ?>
                                <div class="memberholder">
                                    <h1 class="clearfix">
                                        <?php
                                            if($arrayMember['position_pos_id']==17){
                                                echo "<i class='fa fa-star-o' aria-hidden='true'></i>";
                                            }
                                        ?>
                                        <?php echo $arrayMember['emp_fname'] . " " . $arrayMember['emp_lname']; ?>
                                            <button class="viewprofile">View Profile</button>
                                    </h1>
                                </div>
                            <?php
                                }
                            ?>
                        </li>
                    </ul>
                </li>
            </ul>
        <?php
        }
        exit();
        mysqli_close($connect);
    }
        ?>

的script.js

$('.teams').on('click', '.teamdelete', function(){
    var teamId = $(.teams).attr("value");
    if(confirm("Are you sure?")){
        $.ajax({
            url: "http://localhost:81/chansandbox/wp-content/themes/Skeleton/teamdelete.php",
            method: "POST",
            type: "POST",
            data: {
                team_id: teamId
            },
            success: function(data)
            {
                alert(data);
                eval(data);
            }
        });
    }
});

teamdelete.php

<?php
    $connect = mysqli_connect('localhost', 'root', '', 'chansandbox');

    $team_id = $_POST['teamId'];

    $update = "UPDATE employee SET team_team_id = NULL WHERE team_team_id = '$team_id'";

    if(mysqli_query($connect, $update)){
        echo "Employees Updated";

        $deleteteam = "DELETE FROM team WHERE team_id = '$team_id'";

        if(mysqli_query($connect, $deleteteam)){
            echo "Team deleted '$team_id'";
        }
        else{
            echo "Team not deleted";
        }
    }
    else{
        echo "Employees not updated";
    }

    mysqli_close($connect);
?>

正如您所看到的,我正在尝试在teams.php中获取ul(class = teams)的值,​​并使用ajax和var teamId = $(this).attr("value");将其传递给js脚本文件并将其传递给teamdelete .php但它似乎不起作用。非常感谢任何帮助,谢谢。

2 个答案:

答案 0 :(得分:0)

因为$('。teamdelete')没有attr值.. 在您的HTML中,你应该把

<button class="teamdelete" value="<?php echo $arrayTeam['team_id']; ?>">Delete Team</button>

并在你的javascript中执行此操作

var teamId = $(this).attr("value");

并在你的php ..因为你应该做的帖子数据= team_id的名称

$team_id = $_POST['team_id'];

答案 1 :(得分:0)

您的数据变量不正确......

ajax数据以data: { NAME : VALUE }发送。 PHP本质上是调用$_POST['VALUE']所以php不知道该值是什么

PHP调用:

$_POST['teamId'];

但是ajax发送

team_id:

尝试将teamdelete.php上的PHP更改为:

$team_id = $_POST['team_id'];

还有其他问题......

HTML:

<button class="teamdelete" value="<?php echo $arrayTeam['team_id']; ?>" >Delete Team</button>

JS:

$('.teams').on('click', '.teamdelete', function(){
    var teamId = $(this).attr("value"); // ------------ CHANGED 
    console.log(teamId); // ----------- CHANGED should show the right ID in console
    if(confirm("Are you sure?")){
        $.ajax({
            url: ".....teamdelete.php",
            method: "POST",
            type: "POST",
            data: {
                team_id: teamId
            },
            success: function(data)
            {
                alert(data);
                eval(data);
            }
        });
    }
});

PHP:

<?php
    $connect = mysqli_connect('localhost', 'root', '', 'chansandbox');

    $team_id = $_POST['team_id']; // ----------- CHANGED

    $update = "UPDATE employee SET team_team_id = NULL WHERE team_team_id = '$team_id'";

    if(mysqli_query($connect, $update)){
        echo "Employees Updated";

        $deleteteam = "DELETE FROM team WHERE team_id = '$team_id'";

        if(mysqli_query($connect, $deleteteam)){
            echo "Team deleted '$team_id'";
        }
        else{
            echo "Team not deleted";
        }
    }
    else{
        echo "Employees not updated";
    }

    mysqli_close($connect);
?>

您可以使用开发人员面板和检查器确认删除按钮具有正确的团队价值。