www.webdeveloper.com
Results 1 to 5 of 5

Thread: Php in Html

  1. #1
    Join Date
    Dec 2011
    Posts
    39

    Php in Html

    Sir I have following codes

    PHP Code:
    <?php
    if(isset($_POST['button1'])) {
        
    $aa $_POST['text1'];
            echo (
    "My name is " $aa);        
    }
    ?>

    <html>
    <head>
    <title>First form</title>
    </head>    
    <body>

    <form name="aa" action="mix.php" method="POST">
        Name<input type="text" name="text1" value="">
        <input  type="submit" value="display" name="button1">
    </form>
            
    </body>    
    </html>
    These codes have no error message.
    I am beginner so I request you to please check whether I am doing right.

    Second Request:

    Is it possible to use php codes in <html> tag.
    if yes then please adjust my php code in html tag.

    Thanks in advance

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,536
    You want the echo portion of the PHP to output its text exactly where you want it within the HTML. For example, it might be something like:
    PHP Code:
    <html>
    <head>
    <title>First form</title>
    </head>    
    <body>

    <form name="aa" action="mix.php" method="POST">
        Name<input type="text" name="text1" value="">
        <input  type="submit" value="display" name="button1">
    </form>

    <?php
    if(isset($_POST['button1'])) {
        echo (
    "<p>My name is " $_POST['text1'] . "</p>");        
    }
    ?>
    </body>    
    </html>
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Dec 2011
    Posts
    39
    Thanks sir, it works

  4. #4
    Join Date
    Jan 2013
    Posts
    4
    Even shorter:

    <? (isset($_POST['button1'])) ? echo "<p>My name is " . $_POST['text1'] . "</p>" : ''; ?>

    <html>
    <head>
    <title>First form</title>
    </head>
    <body>

    <form name="aa" action="mix.php" method="POST">
    Name<input type="text" name="text1" value="">
    <input type="submit" value="display" name="button1">
    </form>

    </body>
    </html>

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,536
    Quote Originally Posted by kamoiran View Post
    Even shorter:
    PHP Code:
    <? (isset($_POST['button1'])) ? echo "<p>My name is " $_POST['text1'] . "</p>" ''?>

    <html>
    <head>
    <title>First form</title>
    </head>
    <body>

    <form name="aa" action="mix.php" method="POST">
    Name<input type="text" name="text1" value="">
    <input type="submit" value="display" name="button1">
    </form>

    </body>
    </html>
    Except that you're still outputting the result before the opening <html> tag.

    Also, "shorter" doesn't win any awards with anyone who has to maintain your code a year from now.

    Oh, and it doesn't parse, anyway.

    If you really feel the need to use the ternary operator for some reason (doesn't really buy you anything here, IMO):
    PHP Code:
    <html>
    <head>
    <title>First form</title>
    </head>
    <body>
    <form name="aa" action="mix.php" method="POST">
    Name<input type="text" name="text1" value="">
    <input type="submit" value="display" name="button1">
    </form>
    <?php  // always use full tag to be safe
    echo (isset($_POST['button']))
        ? 
    htmlentities("<p>My name is " $_POST['text1'] . "</p>")
        : 
    '';
    ?>
    </body>
    </html>
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles