Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't access form data in PHP

Tags:

post

forms

php

I'm not a novice, but I'm not an expert either; I'm a keen learner.

Problem (minimalised) - I have a basic form which posts a name to another page which is supposed to receive name and print name. Code of both forms is below.

Form:

<?php
    echo "Hello, World!";
    echo "
        <form action='CFAcomments.php' method='POST'>
            <table style='width: 50%;' border='0'>
                <tbody>
                    <tr>
                        <td><label for='name'>name: </label></td>
                        <td><input type='Text' name='name' value='anon' /></td>
                    </tr>
                    <tr>
                        <td><input type='submit' name='send' value='Send' /></td>
                    </tr>
                </tbody>
            </table>
        </form>
    ";
?>

Form Process:

<?php
    echo "Hello, World 1!";
    echo "<br/>";
    var_dump($_POST);
    $name = $_POST("name");
    echo "Hello $name!";
?>

Result:

Hello, World 1!
array(2) { ["name"]=> string(11) "anon" ["send"]=> string(4) "Send" }

Problem:

Even though var_dump($_POST) shows data being sent, echo $name print nothing. Changing echo $name to echo "test" prints nothing too. The code seems to stop executing at $name = $_post("name");. If I remove this line echo "anything" works.

I've used PHP and forms for the last two years and never come across this. Any help would be appreciated.

like image 638
clarent Avatar asked Jun 06 '26 03:06

clarent


2 Answers

Array keys are referenced with square brackets, not parentheses.

$name = $_POST("name");

// Should be
$name = $_POST["name"];
like image 176
Michael Berkowski Avatar answered Jun 07 '26 16:06

Michael Berkowski


That's because you're not using the right brackets. It has to be:

$_POST['name'];
like image 34
markus Avatar answered Jun 07 '26 16:06

markus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!