0

can anyone help me with a little problem, I have array with 1 value in it, and with every submit input I want to add +1 value to array, for example in array I have value [0] -> "Name1", when I press submit button I want to add another value so array would be [0] -> "Name1", [1] -> "Name2" and when I press again it adds Name3 and so on, I used array_push, but it only adds one value, and then just updates that one value, so, do you have any ideas how to make my code work properly? Thanks!

$users = array("Name1");
$_SESSION["add"] = $users;

if(isset($_GET["create"])) {
    $name = $_GET["name"];  

    array_push($_SESSION["add"], $name);

}

foreach($users as $acc) {
    echo $acc;
}
1
  • First of all you have to post your code to help you make it work properly Commented Feb 22, 2018 at 10:37

3 Answers 3

1

You can make multiline form, which will represent every value from array.

echo '<input type="text" name="create[]" value="">';
foreach($_GET['create'] as $name)
{
    echo '<input type="text" name="create[]" value="$name">';
}

So you can iterate over each field in array, show it to user and let him edit entries and add new. You can access names as array in php ($_GET['names'], $_POST['names']);

Sign up to request clarification or add additional context in comments.

Comments

0

Keep an array in which you store your data in a session/cookies/database. When your script stops executing, this array doesn't exist anymore, so when you submit a form, the new array is created.

2 Comments

I tried using sessions but when I use it, it only shows first value which is created by adding it directly to array, I will add code to my question,
Have you called a session_start() anywhere before the code you had posted?
0

Check here how its working i think it working as par you said : http://www.tutorialsscripts.com/php-tutorials/array-functions/php-array_push-function-demo.php

2 Comments

Yes, I want exactly as given here, but where can I find that code example?
The code in the link is: $arrays = array("one", "two", "three", "four", "five"); array_push($arrays,'six','sevena'); print_r($arrays); It's just an example, I thought it will be whole code, like in the page

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.