使用提交按钮依赖于另一个提交按钮

时间:2012-07-10 04:19:51

标签: php html forms

我对PHP比较陌生,我在一个不相关的领域工作了很多年后回到了学校。我正在为爸爸做一个外部项目,并且遇到表面上看起来很容易解决的问题,我似乎无法解决这个问题。

在相关页面上,它会显示联盟中的球员名单及其得分。它下面的表格有一个文本框,允许您输入名称和三个按钮;一个用于添加名称作为玩家(默认分数为0),从玩家列表中删除该玩家,第三个用于调整该玩家的分数。我得到前两个(“添加”和“删除”)按照我的意愿工作,但是我遇到的问题是分数调整问题。

如果单击“调整分数”按钮,则会显示一个新文本框以输入分数以及另一个实际提交分数的按钮。当你按下新按钮来调整分数时,问题是没有发生了,此时我刚刚在那里得到了一个测试“echo”语句,应该在提交时显示,但是这没有发生。以下是有问题的代码。有关我如何解决的任何想法?感谢您的任何建议。

<html>
<head>
    <LINK REL="stylesheet" type="text/css" href="styles/footballTest.css" />
    <title>VFW Football testing</title>
</head>
<body>
    <?php
        $dbConnect = mysql_connect("localhost", "root","");
        $dbName = "vfwleaguetest";
        $getPlayers = "SELECT * FROM `players`";
        if(!$dbConnect)
        {
            echo "<p>Connection failed</p>";
        }   
        if(mysql_select_db($dbName, $dbConnect) === false)
        {
            echo "<p>Could not select the database ".$dbName ."<br/>".mysql_error($dbConnect)." </p>";
        }
        if(isset($_POST['submit']))
        {
            $name=@$_POST['nameAdd'];
            $playerAdd = "INSERT INTO `vfwleaguetest`.`players` (`playerName`, `seasonTotal`) VALUES ('".$name."', '0')";
            if(mysql_query($playerAdd, $dbConnect) === false)
            {
                echo "<p>Error adding player to database: ".mysql_error($dbConnect)."</p>";
            }
        }
        unset($_POST['submit']);
        if(isset($_POST['delete']))
        {
            $name= @$_POST['nameAdd'];
            $playerDelete = "DELETE FROM `vfwleaguetest`.`players` WHERE `players`.`playerName` = '".$name."'";
            if(mysql_query($playerDelete, $dbConnect) ===false)
            {
                echo "<p>Error deleting player to database: ".mysql_error($dbConnect)."</p>";
            }
            else
            {
                echo $name." successfully removed from player list";
            }
        }
        if(isset($_POST['adjust']))
        {
            ?>
            <br/>
            <input type='text' name='scoreAdjust' size=5 />&nbsp;
            <input type='submit' name='fixScore' value='new season total for <?php echo $_POST['nameAdd'];?>' />
            <?php
            if(isset($_POST['fixScore'])) //THIS IS WHERE THE PROBLEM SEEMS TO LIE
            {
                echo "after fixScore click";
                //add sql below
            }
        }   
    ?>
    <form name="players" action="" method="post">
    <?php
        $playerList = mysql_query($getPlayers, $dbConnect);
        echo "<table><th><tr>";
        echo "<td>Player name</td><td>Season total</td></tr></th>";
        while(($row = mysql_fetch_row($playerList)) != false)
        {
            echo "<tr><td>$row[0]</td><td>$row[1]</td></tr>";
        }
        echo "</table>";
    ?>
    Manage players&nbsp;<input type="text" name="nameAdd" />&nbsp;
<input type="submit" name="submit" value="add" />&nbsp;
<input type="submit" name="delete" value="delete" />&nbsp;
<input type="submit" name="adjust" value="adjust score" />
    </form>
</body>
</html>    

3 个答案:

答案 0 :(得分:0)

<input>的{​​{1}}元素必须位于正在提交的scoreAdjust元素中。

答案 1 :(得分:0)

使用此代码:

            <br/>
            <form method="post">
                <input type='text' name='scoreAdjust' size=5 />&nbsp;
                <input type='submit' name='fixScore' value='new season total for <?php echo $_POST['nameAdd'];?>' />
            </form>
            <?php
            if(isset($_POST['fixScore'])) //THIS IS WHERE THE PROBLEM SEEMS TO LIE
            {
                echo "after fixScore click";
                //add sql below
            }

要将字段提交到$_POST,它必须位于form内。

答案 2 :(得分:0)

问题是分数调整输入超出了形式:

<input type="text" size="5" name="scoreAdjust">
<input type="submit" value="new season total for aaa" name="fixScore">

你需要:

<form method="post" action=''>
  <input type="text" size="5" name="scoreAdjust">
  <input type="submit" value="new season total for aaa" name="fixScore">
</form>