开发者

embeding php in html

开发者 https://www.devze.com 2023-03-02 09:26 出处:网络
is it possible to embed this into html if (empty($_POST[\'extras\'])) { $message .=\"<br /> No extras selected <br />\";

is it possible to embed this into html

if (empty($_POST['extras'])) {
        $message .="<br /> No extras selected <br />";
        } else { 
        foreach($_POST['extras'] as $extra)
        {
          $message .="<br />Extras: ".$extra."";

        } 
        }

I would like to place the above php statement at the bottom of this html code.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Booking System</title>
<link rel="stylesheet" href="css/bs-admin.css" type="text/css" />
</head>
<body>
<noscript>
    <div class="js_error">Please enable JavaScript or upgrade to better <a href="http://www.mozilla.com/en-US/firefox/upgrade.html" target="_blank">browser</a></div>
</noscript>
<div id="index">
<h1>Thank you for your reservation!</h1>


<p>
<h3>Your Booking is as follows:</h3>
<p>Dear <b><?php echo $custInf[0] ?></b>,
<p>You have Booked: <?php echo $eventInf[0] ?>
<p>Booking Date: <?php echo $eventInf[2] ?>
<p>Booking descriptiong: <?php echo $eventInf[1] ?>
<p>Number of machines booked: <?php echo $qty ?>
<p>Street: <?php echo $comments ?> 
<p>Suburb: <?php echo $suburb ?>
<p>Postcode: <?php echo $postcode ?>
<p>Dropoff: <?php echo $dropoff ?>
<p>Duraton: <?php echo $duratio开发者_如何学运维n ?>


If it's got php code in it then it's no more HTML.

You have to call it .php or .phtml.

PHP generates, or outputs html.

You can have pure html in .php scripts (outside the <?php ?> tags), but not the other way around (i.e. no php code in regular .html files).

If you want to add some logic (the PHP code) within it, you need to have it parsed by a webserver which will, in turn, generate html.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title>Booking System</title>
        <link rel="stylesheet" href="css/bs-admin.css" type="text/css" />
    </head>
    <body>
        <noscript>
             <div class="js_error">Please enable JavaScript or upgrade to better <a href="http://www.mozilla.com/en-US/firefox/upgrade.html" target="_blank">browser</a></div>
        </noscript>
        <div id="index">
            <h1>Thank you for your reservation!</h1>
            <div>
                <h3>Your Booking is as follows:</h3>
                <p>Dear <b><?php echo $custInf[0]; ?></b>,</p>
                <p>You have Booked: <?php echo $eventInf[0]; ?></p>
                <p>Booking Date: <?php echo $eventInf[2]; ?></p>
                <p>Booking descriptiong: <?php echo $eventInf[1]; ?></p>
                <p>Number of machines booked: <?php echo $qty; ?></p>
                <p>Street: <?php echo $comments; ?> </p>
                <p>Suburb: <?php echo $suburb; ?></p>
                <p>Postcode: <?php echo $postcode ?></p>
                <p>Dropoff: <?php echo $dropoff; ?></p>
                <p>Duraton: <?php echo $duration; ?></p>
            </div>

            <?php
                $message = "";

                if (empty($_POST['extras'])) $message .="<br /> No extras selected <br />";
                else
                {
                    foreach($_POST['extras'] as $extra)
                    {
                        $message .="<br />Extras: ".$extra;
                    } 
                }

                echo $message;
            ?>
        </div>
    </body>
</html>

I think you're needing to create an empty variable $message before you could start appending "extras" to it. Then all you need to do is echo $message.


Yes you can, however that's not a good way to do it. You should go for the Model-View-Controller model. It separates the HTML code from the actual code that does processing. Many PHP Framework does this. (Though personally i find them too clunky and wrote my own)

Also, embedding HTML into PHP is bad, as again, code should be separated from the HTML by as much as possible.

Example of views and controllers (From my framework):

Controller:

class Controllers extends BaseController{   
    function index($args=array()){

        // process data
        $this->render('index', array('data1'=>$data));
    }
}

View:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>My site</title>
</head>
<body>
<h1><?php echo $page->data1; // echos out $data from the view ?></h1>
</body>
</html>

This is much cleaner than your model of embedding php into the HTML and/or vice versa.

Take a look at frameworks, they are usually pretty helpful, although PHP frameworks are generally very restrictive as to what you can do.


Yes, you can do what you are asking. Make sure the extension is recognizable by the php interpreter (usually .php)

If you need to hack something up quick, this is ok. But for anything else than that, look into using some sort of templating language. This becomes an important point because you want to seperate your logic from your display for the sanity of yourself and other developers that will work on your code in the future.

edit: oh, also very important. Don't use $_POST this way without sanitizing the data. It's ripe for XSS injections.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Booking System</title>
<link rel="stylesheet" href="css/bs-admin.css" type="text/css" />
</head>
<body>
<noscript>
    <div class="js_error">Please enable JavaScript or upgrade to better <a href="http://www.mozilla.com/en-US/firefox/upgrade.html" target="_blank">browser</a></div>
</noscript>
<div id="index">
<h1>Thank you for your reservation!</h1>

<!--- bunch of stuff omitted here -->

<?php
if (empty($_POST['extras'])) {
    echo "<br /> No extras selected <br />\n";
} else { 
    foreach($_POST['extras'] as $extra)
    {
        echo "<br />Extras: ".$extra."\n";
    } 
}
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消