doumor942473 2018-06-17 05:23
浏览 97
已采纳

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

When user inputs text in 'ctext' field and press accept, I want to fill the value=" " field with user input, i achieved this but it fills all the value fields of same name in the page, how can i achieve it for different value of different ctext input? Anyone please give me solution with example, Many thanks

<?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;

?>

  • 写回答

3条回答 默认 最新

  • dtng5978 2018-06-17 07:18
    关注

    I hope I understand what you want. You want to access the ctext for each individual $pen when printing the corresponding form.

    You just need to name your <input> with a unique name and then access that value when printing. A possible solution is this:

    <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>
    

    What does it do?

    • name="ctext[<?php echo $pen['id']; ?>]" ensures a unique name for each $pen. For a $pen with id 1 this will result in name="ctext[1]".
    • if(isset($_POST['ctext'][$pen['id']])){ $ctext = $_POST['ctext'][$pen['id']]; } uses $pen['id'] to look up the corresponding value in $_POST['ctext'].

    By the way, when outputting user input you should always escape it, e.g. with htmlspecialchars. This will look like this: echo htmlspecialchars($ctext); That way malicious input like "><script>alert('Hello!')</script> won't execute the javascript.

    Update: as requested a solution using session to store data:

    <?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;
    

    Note: I removed the now unnecessary counter $i. The session handling is mainly done before the while loop (start a session and store POST data). During output the values from the session are used. The name of the input is not an array anymore.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 plotBAPC画图出错
  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理