1

I'm working on simple app that add array items on button click and display the values on click.

My problem is after adding the items to array I cant display the values.

Hope you help me.

HTML

<form action="" method="POST">
    <button name="save">save</button>
    <button name="display">display</button>
</form>

PHP

$arr = array();

    if (isset($_POST['save'])) {

        $items = array('Mark', '12', 'Japan');

        array_push($arr, $items);

    }

    if (isset($_POST['display'])) {

        print_r($arr);

    }
5
  • 1
    And what does output print_r($arr) ? Commented Dec 17, 2018 at 7:32
  • the Items that was push on save Commented Dec 17, 2018 at 7:35
  • 1
    The reason this isn't working is because when clicking on the save button you filled the $arr variable with the items and never saved them, but than when you clicked on the display button it recreated the $arr array as an empty list. To do this, on the save button you need to store that data somewhere like a database or a file, and than on the display button retrieve that data first and than display it. Commented Dec 17, 2018 at 7:38
  • You don't actually "save" your array after adding values to it Commented Dec 17, 2018 at 7:51
  • use array merge function of php instead of array_push Commented Dec 17, 2018 at 8:22

3 Answers 3

1

You may try to use PHP sessions. This example is very basic, but may help you to understand how to preserve your data across subsequent accesses:

<?php
session_start();

if (isset($_POST['save'])) {
    echo 'Save'.'<br>';
    $items = array('Mark', '12', 'Japan');
    $_SESSION['SimpleArray'] = array();
    array_push($_SESSION['SimpleArray'], $items);
}

if (isset($_POST['display'])) {
    echo 'Display'.'<br>';
    print_r($_SESSION['SimpleArray']);
    echo '<br>';
}
?>
Sign up to request clarification or add additional context in comments.

Comments

1

Here is a way that you can use a hidden input element to keep updating your POST array with the added information.

Use this:

<?php

//Check to make sure there is a value set for the hidden field in the post array.
  if(isset($_POST['myArray']) && $_POST['myArray']){

  //If there is we are going to decode and unserialize it and pass it to the $arr varaible.
  $arr = unserialize(base64_decode($_POST['myArray'])); 

  }

  //Check to see if you hit the save button.
  if (isset($_POST['save'])) {

    //We did so we are adding an element to the $arr array.
    $arr[] = array('Mark', '12', 'Japan');

  }

  //Check to see if we want to display all the data from hitting the save
  //button multiple times.
  if (isset($_POST['display'])) {

    //We did so lets print out the array of what we have "saved so far."
    print_r($arr);

  }

  //Checking to make sure we have an array.  If we do we are going to 
  //serialize and encode the array so we can pass it to the hidden input element.
  if(isset($arr)){

    //serialize and encode.
    $arr = base64_encode(serialize($arr));

  } else {

    //Nothing happened.  Pass nothing to the hidden input element.
    $arr = '';

  }


?>


<form action="" method="POST">
    <button name="save">save</button>
    <button name="display">display</button>
    <input type="hidden" name="myArray" value="<?php echo $arr; ?>">
</form>

Hope it helps.

Comments

0

It is two different conditions.

So when you are clicking on the save button, everything is working correctly and $arr is contain needed value but now, when you are clicking to the display button, you are re-submitting this form again and the first condition isn't working correctly because server forgets about your $arr. It is completely different requests.

Use this code:

$arr = array();

if (isset($_POST['save'])) {

    $items = array('Mark', '12', 'Japan');

    array_push($arr, $items);
    echo 'one <br>';

}

if (isset($_POST['display'])) {
    echo 'two <br>';
    print_r($arr);

}

And you will see that one isn't showing once you click the display button.

You can use session as the simplest solution for this.

1 Comment

This is more of a debugging help than an actual answer. Maybe add a sample example using sessions. It seems OP is a newbie to PHP

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.