Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 每次按“提交”时,我都希望输入文本框更改文本_Php_Forms_Submit - Fatal编程技术网

Php 每次按“提交”时,我都希望输入文本框更改文本

Php 每次按“提交”时,我都希望输入文本框更改文本,php,forms,submit,Php,Forms,Submit,因此,基本上我有一个简单的表单,我想在每次使用PHP按submit时更改输入框文本。我知道我的代码不起作用,但我不知道如何解决这个问题。也许阵列不是最好的方法 <body> <?php if(isset($_GET['submit'])) exit(); $msg= ['One', 'Two', 'Three', ''] ?> <form action="<?php echo $_SERVER['PH

因此,基本上我有一个简单的表单,我想在每次使用PHP按submit时更改输入框文本。我知道我的代码不起作用,但我不知道如何解决这个问题。也许阵列不是最好的方法

<body>
    <?php 
        if(isset($_GET['submit'])) exit();
        $msg= ['One', 'Two', 'Three', '']
    ?>

    <form action="<?php echo $_SERVER['PHP_SELF']?>">
    <input type="submit" name="submit" value="Paina nappi">
    <input type="text" name="msg" value="<?php echo (isset($msg)) ? $viesti : ''; ?>">
    </form>
</body>


你可以这样写:

<?php
    session_start();

    $msgArray= array('One', 'Two', 'Three', '');
    // if $_SESSION['msgIndex'] is not set, we initialize it, or it will take 0 value every loading page
    if (!isset($_SESSION['msgIndex'])) { $_SESSION['msgIndex'] = 0;  }

    if (isset($_GET['submit'])) {
        getMessage();
    }

    // We save msgIndex in a $_SESSION variable cause even if the user reload the page, we keep the value
    function getMessage() {
        if ($_SESSION['msgIndex'] >= 0) {
            $_SESSION['msgIndex'] += 1;
        } if ($_SESSION['msgIndex'] > 3) {
            $_SESSION['msgIndex'] = 0;
        }
    }
?>

<form action="<?php echo $_SERVER['PHP_SELF']?>">
    <input type="submit" name="submit" value="Paina nappi">
    <input type="text" name="msg" value="<?= $msgArray[$_SESSION['msgIndex']] ?>">
</form>


您在哪里定义了
$viesti
变量?哎呀,它应该是$msg变量。我是否需要在每次提交时对数组进行for循环?我走对了吗?谢谢!它工作得很好,正如我所希望的!我遇到的唯一问题是,我必须将
放在文档开头的所有html、head和body标记下,否则我会出错。是的,因为如果使用$\u SESSION,则必须使用SESSION\u start()启动会话。因此,您可以创建一个文件,将所有PHP代码放入其中,并在开始时使用session_start,然后将该文件包含在所有HTML文件中:)