I'm trying have a array which store the text box values, but my problem is whenever I run my code the first text is always being replace when I add another text, this "another text" will remain until I add a third text. And so, this process will repeat and repeat annndd repeat.
Please tell me wath I'am missing.
<body>
<?php
$server_name="localhost";
$username="root";
$password="";
$database="mabangis";
$connection=mysqli_connect($server_name,$username,$password,$database) or die('not connect');
$final_value1="";
if(isset($_POST['insert'])) {
$arraytextbox_value=$_POST['array'];
foreach($arraytextbox_value as $final_value)
{
$final_value1="$final_value<br>";
}
echo "$final_value1";
}
?>
<form method="POST" action="Prac-1.php">
Name: <input type="text" name="array[]">
<button type="add" name="insert">Add</button>
<button type="save">Save</button>
</form>
</body>
You can add more form fields like this
<form method="POST" action="Prac-1.php">
First Name: <input type="text" name="array[]">
Second Name: <input type="text" name="array[]">
<button type="add" name="insert">Add</button>
<button type="save">Save</button>
</form>
And change the php code like this
$names = array();
if(isset($_POST['insert'])) {
$arraytextbox_value=$_POST['array'];
foreach($arraytextbox_value as $final_value)
{
$names[]="$final_value";
}
print_r($names);
}
You have missed concatenating :
<?php
$_POST['insert'] = 'save';
$_POST['array'] = ['apple','orange'];
$final_value1="";
if(isset($_POST['insert'])) {
$arraytextbox_value=$_POST['array'];
foreach($arraytextbox_value as $final_value)
{
// concatenating assignment operator to join the strings
$final_value1 .= "$final_value<br>";
}
echo "$final_value1";
}
Alternatively, you can use implode()
if(isset($_POST['insert'])) {
echo $final_value1 = implode('<br>', $_POST['array']);
}
OUTPUT: "apple<br>orange"
DEMO:
http://sandbox.onlinephpfunctions.com/code/985b7c68d5cdfe86d6cfe8d6768e05d051c958c2
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.