-2

I recently start the php programming. However, I encountered a problem about running a function on php file. My php file is listed as follows:

<html>
<head>
    <meta http-equiv="content-type" content="text/html" />
    <meta name="author" content="aaaaa" />

    <title>Untitled 3</title>
</head>

<body>

<form method="post" action="Test()" >
<input type="submit" name="submit" value="submit"/>

</form>

<?php
echo "Hello";
function Test()
{
    echo "Goodbye";
}
?>

</body>
</html>

After running the program, a webpage is observed with A button entitled submit and the word "Hello". However, After click on the button, the webpage "This page cannot be displayed" is observed. But I respect the word "Goodbye" is shown. I transferred the php code to another file, but the problem was not resolved.

  • 2
    another 132 points on the php tag @chris85 and you'll be able to close those off in one go ;-) bet you can't wait! *lol* – Funk Forty Niner Feb 23 '17 at 19:22
  • after seeing a comment you left in one of the answers being *"However, the webpage "This page can't be displayed" is again observed."*, I'm thinking you're trying to run this as `file:///` rather than a host. – Funk Forty Niner Feb 23 '17 at 19:31
  • I am writing the code and runing it the phpDesigner 8. I set the localhost in a folder in my computer. – Mohammad Tahaye Abadi Feb 23 '17 at 19:38

1 Answers1

1

actions in forms aren't functions like JavaScript. You are trying to run Test() on submit which isn't a thing. What you want is action to be action=" "

In reality you should be doing something like this:

<html>
<head>
    <meta http-equiv="content-type" content="text/html" />
    <meta name="author" content="aaaaa" />

    <title>Untitled 3</title>
</head>

<body>

<form method="post" action="" >
<input type="submit" name="submit" value="submit"/>
</form>
<?php
    echo "Hello";

if(isset($_POST['submit'])){
    echo "Goodbye";
}

?>

</body>
</html>

If you WANT to use a function....

<html>
<head>
    <meta http-equiv="content-type" content="text/html" />
    <meta name="author" content="aaaaa" />

    <title>Untitled 3</title>
</head>

<body>

<form method="post" action="" >
<input type="submit" name="submit" value="submit"/>
</form>
<?php
    echo "Hello";

function test(){
   echo "Goodbye";
}

if(isset($_POST['submit'])){
    test();
}

?>

</body>
</html>

Lets say he file name is test.php where you have put all your code so the action values should be test.php

<form method="post" action="test.php" >
KB5
  • 128
  • 3
  • 12
clearshot66
  • 2,292
  • 1
  • 8
  • 17