1

I'm really new new to php. I consider myself alright at Java, but wanted to get more into web stuff. I like HTML and CSS well enough, but I'm having a lot of trouble with php.

I'm writing a really basic php code. I want it to get info from a user (via form) and add it to an array in php (POST). I then would like to store the array as a session variable and write a for loop that prints out each index in an HTML list.

ISSUES:
1. I don't have a good handle on SESSION, so not sure how to store the array as a session variable. 2. I'm not sure how to reference a specific index of an array in php. I've sorta done it in a java way here, but that needs to change.

--CODE--

<html>
<body>

<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
<input type="submit">
</form>

</body>
</html>


<?php
$stack = array("");
array_push($stack, $_POST[name]);

for(i < $stack.length){
print_r($stack[i]);
}
?>
nbray
  • 81
  • 4

3 Answers3

1

To access session variables is easy:

  1. First, you need to call the method session_start() (Make sure, you call it, before sending any HTTP header).
  2. After calling the session_start() method, you will have access to the $_SESSION associative array. You will be able to append anything to this array.

The syntax of the for loop in PHP is as follows:

foreach (array_expression as $value)
    statement

or

foreach (array_expression as $key => $value)
    statement

I hope that it helps.

SaWo
  • 1,232
  • 1
  • 11
  • 26
1

Just to quickly update the code you have, to make it somewhat workable:

Html code

    <html>
        <body>
            <form action="welcome.php" method="post">
        Name: <input type="text" name="name"><br>
        <input type="submit">
        </form>
    </body>
   </html>

Php code

<?php
$stack = array("");
if(isset($_POST['name'])){
    array_push($stack, $_POST['name']);

    for($i=0; $i < count($stack); $i++){
    echo($stack[$i]);
    }
}
?>

Assuming this is all in welcome.php.

shafiq.rst
  • 1,246
  • 1
  • 12
  • 25
aldorr
  • 11
  • 3
1

First let's see the lines of code in PHP you have written:

I.

$stack = array("");

This creates an array called $stack with a single element of "". $stack[0] will have the value of "". You can name the elements of an associated array, like this:

$stack = array("name" => "value");

In this case $stack["name"] will be "value".

II.

array_push($stack, $_POST[name]);

This is incorrect, since name is not a variable, nor a string. You probably meant:

array_push($stack, $_POST["name"]);

this would have written $_POST["name"] at the end of your array having "", so $stack[1] would have been whatever the value of $_POST["name"]; was.

III.

for(i < $stack.length){

This is incorrect syntax. You have meant

for($i = 0; $i < count($stack); $i++){

Note how $ is put in front of all variables and how similar this for cycle is to a Java for.

IV.

print_r($stack[i]);

Incorrect, you need the the cash ($), otherwise your variables will not cooperate.

print_r($stack[$i]);

You, however, do not check whether this is a POST request or a GET. When the user loads the page, it will be a GET request and when he submits the form, it will be a POST request. The first (GET) request will not have $_POST members ($_POST will be empty), as the form was not submitted yet. And if you check whether it is a POST request, you need to check whether "name" is present in $_POST:

<html>
<body>

<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
<input type="submit">
</form>

</body>
</html>


<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') { //it is a post
    if (isset($_POST["name"])) { //name is found inside $_POST
        echo "Name is " . $_POST["name"];
    }
}
?>

Question1:

$_SESSION is an array, like $stack. You can do something like this:

$_SESSION["name"] = $_POST["name"];

This will create a new element of $_SESSION with the index of "name", however, before such an assignment, you need to make sure the session was started.

Question2:

You reference it by the name of the index, just like in Java, however, in PHP you can have textual indexes as well if you want, while in Java you can only use integers.

Community
  • 1
  • 1
Lajos Arpad
  • 45,912
  • 26
  • 82
  • 148