简体   繁体   English

php通过输入类型提交发送变量

[英]php send variable by input type submit

Part of 'index.php' 'index.php'的一部分

<body>
    <?php include('form.php') ?>

    <form action="index.php" method="post">
        <select name="id"><?php playerID($tournament); ?></select>
        <input type="text" name="name" value="<?php playerName($tournament); ?>">
        <input type="submit" value="Save Player">
    </form>
</body>

Part of 'form.php' 'form.php'的一部分

function playerName($tournament) {
    $player = getPlayer($tournament);
    echo isset($player) ? $player->Name : '';
}

This code works for me, but when I press on 'Save Player' to submit I can't send the $tournament to call function 'updatePlayer($tournament)' 这段代码对我有用,但是当我按“保存播放器”提交时我无法发送$ tournament来调用函数'updatePlayer($ tournament)'

Another part of 'form.php' 'form.php'的另一部分

updatePlayer($tournament);

function updatePlayer($tournament) {       
    if (isset($_POST['id'])) {
        $id = $_POST['id'];
        $player = $tournament->getPlayer($id);
        $player->Name = $_POST['name'];
        $player->Phone = $_POST['phone'];
        $player->Active = $_POST['active'];
    }
}

I need something like this: 我需要这样的东西:

<input type="submit" value="Save Player" <?php [SEND $tournament FROM HERE?] ?>>

Your form on index.php needs to instantiate the function you have defined. index.php上的表单需要实例化您定义的函数。 To do this a simple $_POST detection at the top of the page will do. 为此,可以在页面顶部进行简单的$ _POST检测。 From there you can validate and run the function as intended. 从那里,您可以按预期验证和运行该功能。

if(isset($_POST['name')){
    //validation here???
    updatePlayer($_POST['name']);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM