-1

I am new to php, learning php from w3 schools. I am making a simple form to accept and display the name and email address of the user. I want to accept and display the email and name using a single php file rather than making two different files, one html file to accept and one php file to display. The question is can I use html tags inside the php block? I have tried but i does not work-

<html>
<body>

  if(isset($_POST["email"],$_POST["name"]))
{
        echo "your name is".$_POST["name"]."<br>";
        echo "your email address is".$_POST["email"]."<br>";

}

    else
    {
        <form action="form.php" method="post">
    Name:<input type="text" name="name">
    Email:<input type="text" name="email">
    <input type="submit">
       </form>
    }

  ?>
</body>
</html>

This code gives an syntax error and the form tag does not get color coded. What is wrong in this? Please help I have seen a similar question on this site but there were answers but they were not useful as I cannot close he php block as I need the if else statements which cannot be used in the html block. Further I am making a form which needs php commands to print.

  • 1
    you have to use `echo` to output the html...you did it correctly in the `if`, but not in the `else`. Alternatively you can close the php tag using `?>`, write your HTML, and open it again afterwords using ` – ksbg Mar 01 '16 at 12:02
  • 6
    Did you really search about it even a little bit? – Deepanshu Goyal Mar 01 '16 at 12:05
  • I'd caution against using W3 Schools as a learning resource (see [W3Fools](http://www.w3fools.com/) for info). The [official PHP website has their own tutorial](http://php.net/manual/en/tutorial.php) which I found to be an excellent guide (although it was over a decade ago when I used it to learn PHP). – daiscog Mar 01 '16 at 12:14

2 Answers2

3

Just check below code

<html>
 <body>
  <?php
    if(isset($_POST["email"],$_POST["name"]))
    {
        echo "your name is".$_POST["name"]."<br>";
        echo "your email address is".$_POST["email"]."<br>";
    }
    else
    {
  ?>
    <form action="form.php" method="post">
      Name:<input type="text" name="name">
      Email:<input type="text" name="email">
      <input type="submit">
    </form>
  <?php  
     }
  ?>
</body>
</html>
Dhaval Patel
  • 1,076
  • 6
  • 19
0

You can echo/print it or

<html>
<body>
<?php

  if(isset($_POST["email"],$_POST["name"]))
{
        echo "your name is".$_POST["name"]."<br>";
        echo "your email address is".$_POST["email"]."<br>";

}

    else
    { ?>
        <form action="form.php" method="post">
    Name:<input type="text" name="name">
    Email:<input type="text" name="email">
    <input type="submit">
       </form> <?
    }

  ?>
</body>
</html>`

because you're not using PHP variables in any of the HTML you can come out of the php parser, and go back in. It can make it quicker to just cut/paste off various things, and the php parser doesnt chew on it (although this is a minor detail)

mario.van.zadel
  • 2,919
  • 14
  • 23
BugFinder
  • 17,474
  • 4
  • 36
  • 51