0

What I'm trying to do is get simple input from the user an print it out using echo within my php tags but not working for some reason. I'm still a beginner to php so if anyone could assist me, that be dope.

Here's my code :

<p>
 <form method="GET">
    <input type="text" name="firstname">
    <input type="submit" value="sub">
 </form>
</p>


<?php
  echo $_GET['firstname'];
?>

The file is saved with a .php extension and at the moment, I'm running it locally on my computer using apache. The HTML for the form appears but when I click the submit button, it does not echo what was inputted. I know its not going to the code within the php tag but I'm unsure on how to make it go there.

halapgos1
  • 1,130
  • 4
  • 16
  • 34
  • Are you accessing the page with `http://`? If you view the source is the PHP present? – chris85 Oct 02 '16 at 21:22
  • Try `action=""` on the `
    ` tag.
    – Sebastian Simon Oct 02 '16 at 21:22
  • Is the PHP file being parsed as one? Make sure that you're viewing the file through a server and not as a static file. – Terry Oct 02 '16 at 21:26
  • Possible duplicate of [PHP code is not being executed, instead code shows on the page](http://stackoverflow.com/questions/5121495/php-code-is-not-being-executed-instead-code-shows-on-the-page) – chris85 Oct 02 '16 at 21:29
  • @chris85 Nah, I'm writing `localhost/test.php` @Xufox what do I write in the action attribute for the form tag? I was thinking about that too but I was unsure. And the PHP is being shown when I do view source on the file – halapgos1 Oct 02 '16 at 21:36
  • And @chris85 I looked at the link you suggested and it didn't help so I thought I'd ask – halapgos1 Oct 02 '16 at 21:40
  • Which protocol are you using when you input that address? `file:///`, `ftp://`, `http`, `https`, other? Since the PHP is being shown your PHP is not processing; or your server is misconfigured. – chris85 Oct 02 '16 at 23:17

2 Answers2

0

There are two things that you might wanna do:

  1. In the form action write <?php echo $_SERVER['PHP_SELF']; ?> The PHP_SELF redirect you to the same .php file with the form data

  2. To avoid any exception, use isset function in the last part of your code, which would check if the POST variable exist or not.

    <?php if(isset($_GET['firstname'])){ echo $_GET['firstname']; } ?>

Kushagra
  • 626
  • 6
  • 20
  • sorry my bad, please check the edited post. Thanks, and don't forget to up vote if it works for you. – Kushagra Oct 02 '16 at 22:08
0

To finish this you will need a action which means this:

<p>
  <form method="GET" action="index.php">
    <input type="text" name="firstname">
    <input type="submit" name="submit" value="sub">
 </form>
</p>

Also if you would like to get the result from the form whenever you hit that submit button, you will have to "tell" php that you would like whenever you hit that submit button to print the result. The simpliest way is this:

<?php
  if(isset($_GET['submit']))
   {
   echo $_GET['firstname'];
   }
?>
h3k
  • 178
  • 3
  • 13
  • Not working still. I see it if I click `view source` on the local page but it doesn't execute the php code IDK WHY omg...I'm literally going crazy rn smh – halapgos1 Oct 02 '16 at 22:05
  • My suggestion will be to copy and paste the exact code from here and try it out. Also do not to forget to change the name of the file you are pasting this code to index.php – h3k Oct 02 '16 at 22:18