0

Can anyone help me make this work? I am very very new to PHP and I was just trying to create a single file thing which asks for a name and returns it. Can anyone help see where I am going wrong? I am testing it on wtools.io Thanks in advance for any help :)

<form method="POST" action="formFunction()" name="form1">
   Name: <input type="text" name="name">  
   <input name="s1" value="Submit LoL !" type="submit">

</form>
<?php
function formFunction() {
$name = $_POST['name'];
echo $name;
}```
sammysahrd
  • 19
  • 2

1 Answers1

0

You cannot call a PHP function as the action of your form. Instead, you should create a second file/script, something like post.php, and use that as the action. For example:

<form action="post.php" method="post" name="form1">

Then, in your post.php:

<?php
    if (!$_POST) {
        print "This should only be called on form submit.";
        exit();
    }
    // you should actually test to see if name is supplied before this next line
    $name = $_POST['name'];
    
    echo $name;
?>
<!-- do some other stuff... -->

A similar question for your review Calling a particular PHP function on form submit

Jesse Q
  • 1,451
  • 2
  • 13
  • 18