Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

258
Views
PHP Array does not add new elements from HTML form

New to PHP and web development. I feel stupid asking this but been browsing for answers for hours and can't find anything. If this has been answered before please link me... Thank you for any help!

Description: Using HTML form, pass values as an array to PHP array. Works first time, but when I go back to the form and try to add another set of values, it overrides the values at index 0.

What I've Tried: array_push($log, $_POST["info"]) and $log[] = $_POST["info"]; = same result, index at 0 is overwritten.

HTML Code

 <?php $date = date('d/m/Y h:i', time());?>
 <form class="contact-form" action="/server.php" method="post">
        <input type="text" name="info[]" placeholder="Full name" required>
        <textarea name="info[]" rows="5" cols ="80" placeholder="Message" required></textarea>
        <input type="hidden" name="info[]" id="date-time" value="<?php echo $date; ?>"> 
        <button type="submit" name="submit">Submit Message</button>      
 </form>

PHP Code

<?php
    $log = array();

    if(isset($_POST["info"])){
      array_push($log, $_POST["info"]);
    }

    var_dump($log);
?>  

Current Result


array(1) {
  [0]=>
  array(3) {
    [0]=>
    string(12) "Blunder King"
    [1]=>
    string(28) "Blundering all the day long."
    [2]=>
    string(16) "27/11/2021 02:48"
  }
}

When I try to add another value to the $log array, it gets overwritten instead of moving new index.

array(1) {
  [0] =>
  array(3) {
    [0]=>
    string(12) "Blunder King"
    [1]=>
    string(28) "Blundering all the day long."
    [2]=>
    string(16) "27/11/2021 02:48"
  }, // would expect new array values at index 1. But instead I get these values overwriting index 0 above.
  [1] =>
  array(3) {
    [0]=>
    string(12) "Blunder Queen"
    [1]=>
    string(30) "Blundering all the day long."
    [2]=>
    string(16) "27/11/2021 02:51"
  }
}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Simply slightly change your code to use SESSION:

<?php  session_start();
$date = date('d/m/Y h:i', time());?>

<form class="contact-form" action="server.php" method="post">
        <input type="text" name="info[]" placeholder="Full name" required>
        <textarea name="info[]" rows="5" cols ="80" placeholder="Message" required></textarea>
        <input type="hidden" name="info[]" id="date-time" value="<?php echo $date; ?>"> 
        <button type="submit" name="submit">Submit Message</button>      
 </form>

For server.php

<?php session_start();
    
    if ( ! isset($_SESSION["log"])){
       $_SESSION["log"] = array();
}

    if(isset($_POST["info"])){
         array_push($_SESSION["log"], $_POST["info"]);
    }

    var_dump($_SESSION["log"]);
?>  

Or simply merge the two together and run it and you will see the result in a single php:

<?php 
session_start();
?>

<?php $date = date('d/m/Y h:i', time());?>
 <form class="contact-form" action="#" method="post">
        <input type="text" name="info[]" placeholder="Full name" required>
        <textarea name="info[]" rows="5" cols ="80" placeholder="Message" required></textarea>
        <input type="hidden" name="info[]" id="date-time" value="<?php echo $date; ?>"> 
        <button type="submit" name="submit">Submit Message</button>      
 </form>


 <?php
    
    if ( ! isset($_SESSION["log"])){
      $_SESSION["log"] = array();
    }

    if(isset($_POST["info"])){
      array_push($_SESSION["log"], $_POST["info"]);
    }

    var_dump($_SESSION["log"]);
?>  
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!