首页 > 解决方案 > 如何使用相同的名称从 POST 变量中获取多个值

问题描述

当用户在“ctext”字段中输入文本并按接受时,我想用用户输入填充 value="" 字段,我实现了这一点,但它填充了页面中所有同名的值字段,我该如何实现呢?不同ctext输入的不同值?任何人请给我一个例子的解决方案,非常感谢

<?php

$connect = mysqli_connect('localhost', 'root', 'root123', 'font');
$query = 'SELECT * FROM pens ORDER by id ASC';
$result = mysqli_query($connect, $query);

if($result):
    if(mysqli_num_rows($result)>0):
        $i=0;
        while( $pen = mysqli_fetch_assoc($result) ):
        ?>
        <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>?action=add&id=<?php echo $pen['id']; ?>">

            <div class="name pen-<?php echo $pen['id']; ?>">
                <input type="text" name="ctext[]" class="form-control" placeholder="Type your text here" value="<?php $ctext = false; if(isset($_POST['ctext'])){ $ctext = $_POST['ctext']; } echo $ctext[$i]; ?>"></input>
                <input type="hidden" name="id" value="<?php $pen['id']?>"></input>
            </div>

            <div class="btn-custom">
                <input type="submit" name="add_to_cart" class="btn btn-block" value="Accept"></input>
            </div>      
        </form>
        <?php
        $i++;
        endwhile;
    endif;
endif;

?>

标签: phppostinput

解决方案


我希望我明白你想要什么。您希望在打印相应的表格时访问ctext每个人的 。$pen

您只需要用<input>一个唯一的名称命名您,然后在打印时访问该值。一个可能的解决方案是:

<input type="text" name="ctext[<?php echo $pen['id']; ?>]" class="form-control" placeholder="Type your text here" value="<?php $ctext = ''; if(isset($_POST['ctext'][$pen['id']])){ $ctext = $_POST['ctext'][$pen['id']]; } echo $ctext; ?>"></input>

它有什么作用?

  • name="ctext[<?php echo $pen['id']; ?>]"确保每个$pen. 对于$pen带有id1 的 a,这将导致name="ctext[1]".
  • if(isset($_POST['ctext'][$pen['id']])){ $ctext = $_POST['ctext'][$pen['id']]; }用于$pen['id']在 中查找对应的值$_POST['ctext']

顺便说一句,在输出用户输入时,您应该始终对其进行转义,例如使用htmlspecialchars. 这看起来像这样:echo htmlspecialchars($ctext);这样恶意输入"><script>alert('Hello!')</script>就不会执行 javascript。

更新:根据要求使用会话存储数据的解决方案:

<?php

$connect = mysqli_connect('localhost', 'root', 'root123', 'font');
$query = 'SELECT * FROM pens ORDER by id ASC';
$result = mysqli_query($connect, $query);

if($result):
    if(mysqli_num_rows($result)>0):
        session_start();
        if (isset($_POST['ctext'])) {
            $_SESSION['ctext'][$_POST['id']] = $_POST['ctext'];
        }
        while( $pen = mysqli_fetch_assoc($result) ):
        ?>
        <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>?action=add&id=<?php echo $pen['id']; ?>">

            <div class="name pen-<?php echo $pen['id']; ?>">
                <input type="text" name="ctext" class="form-control" placeholder="Type your text here" value="<?php $ctext = ''; if(isset($_SESSION['ctext'][$pen['id']])){ $ctext = $_SESSION['ctext'][$pen['id']]; } echo htmlspecialchars($ctext); ?>"></input>
                <input type="hidden" name="id" value="<?php echo $pen['id']?>"></input>
            </div>

            <div class="btn-custom">
                <input type="submit" name="add_to_cart" class="btn btn-block" value="Accept"></input>
            </div>      
        </form>
        <?php
        endwhile;
    endif;
endif;

注意:我删除了现在不必要的 counter $i。会话处理主要在while循环之前完成(启动会话并存储 POST 数据)。在输出期间,使用来自会话的值。输入的名称不再是数组。


推荐阅读