alireza
alireza

Reputation: 29

show data with multi dynamic name in php

i want with this code add for example 3 textbox after add , i want to show the value in the 3 textbox in another page but i can see just last value in last textbox , im using dynamic name for my textbox but i cannot see the values in 3 textbox please help.

add textbox code:

<form action="add_text_box.php" method="post">
    <input type="text"  style="margin-left:50px;" name="add" placeholder="add_name" />
    <input type="submit"  value="add"/>
</form>


<form action="show_users.php" method="post">
<?php
if($_SESSION==null){
    echo "no data.".'<br/>';
}else{
    $d=$_SESSION['add'];
    for($i=1;$i<=$d;$i++){
    $names='add'.$i;
    echo "<input type='text' name='$names' style='margin-left:50px;'/>".'&nbsp;'.'<a href="#">edit</a>'.'<br/>'.'<br/>';
    }
}
?>
<input type="submit" name="add_name" />
        </form>

process add textbox:

<?php
session_start();
?>

<?php
if($_POST['add']==null){
    echo "nist";
}else{
$_SESSION['add']=$_POST['add'];
    header('location:register_group.php');
}
?>

show values of textbox:

<?php
echo $_POST['names'];
?>

Upvotes: 0

Views: 40

Answers (1)

Saeed M.
Saeed M.

Reputation: 2361

you can access to your inputs like this :

echo $_POST['add1'];
echo $_POST['add2'];
echo $_POST['add3'];

or you can do it in right way. just make an array of your inputs :

 for($i=1;$i<=$d;$i++){
    echo "<input type='text' name='add[]' style='margin-left:50px;'/>".'&nbsp;'.'<a href="#">edit</a>'.'<br/>'.'<br/>';
    }

so in this way you can access to your inputs like this :

$con=mysqli_connect('localhost','root','','mydb1'); 
mysqli_query($con,"select * from tbl1");

$values = [];
foreach($_POST['add'] as $add){
    $values[] = "(".mysqli_real_escape_string($add).")";
}
$values = implode(',',$values);
mysqli_query($con,"INSERT INTO tbl1 (fname) VALUES ($values)"); 

Upvotes: 2

Related Questions