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 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序