如何使用相同的名称从POST变量中获取多个值

时间:2018-06-17 05:23:08

标签: php post input

当用户在' ctext'中输入文字时字段并按下接受,我想填写值=" "用户输入的字段,我实现了这个,但它填充了页面中同名的所有值字段,如何实现不同的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;

&GT;

3 个答案:

答案 0 :(得分:0)

将输入的名称更改为数组。就像这样。提交表单时,您将获得这些值作为数组。试一试

<input type="text" name="ctext[]" class="form-control" placeholder="Type your text here"></input>

答案 1 :(得分:0)

我猜您的代码会误导您,您的表单处于while循环中,因此一旦任何 ctext 输入填充了您的变量 $ _ POST [&#39; ctext&#39;] 在服务器端设置,根据您的代码,一旦按下接受,它就会设置 ctext 的所有值。

您可以在输入字段 name =“ctext []”中将不同的名称作为解决方案或数组索引来避免这种情况。

答案 2 :(得分:0)

我希望我理解你想要的东西。您希望在打印相应表单时为每个人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 id 1,这将导致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;

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

相关问题