简体   繁体   中英

How can i get multiple value from POST variable using same name

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;

?>

Change name of an input to an array.like this . When you submit the form you will get these values as an array. Give it a try

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

I guess your code is misleading you, your form is in while loop so once any of the ctext input is filled your variable $_POST['ctext'] is set on server side and according to your code it sets all the value of ctext once accept is pressed.

You can have different names as a solution or an array indexing in input field name=“ctext[]” to avoid this.

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']]; } 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, eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM