Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在html中格式化php?_Php_Html - Fatal编程技术网

如何在html中格式化php?

如何在html中格式化php?,php,html,Php,Html,我有一个简单的test.php脚本: <?php $name = $_GET['name']; $response = "Hi, ".$name."! How are you?"; echo $response; ?> 正如您所看到的,有一个带有模板的html页面,并且只需要在一个位置(页面中间)进行响应。我不想重复整个html脚本 如果我把php脚本放到html中,那么我的php脚本就会公开 示例链接:www.domain.com/test.php?n

我有一个简单的
test.php
脚本:

<?php

    $name = $_GET['name'];
    $response = "Hi, ".$name."! How are you?";
    echo $response;

?>
正如您所看到的,有一个带有模板的html页面,并且只需要在一个位置(页面中间)进行响应。我不想重复整个html脚本

如果我把php脚本放到html中,那么我的php脚本就会公开


示例链接:
www.domain.com/test.php?name=John

用which.php保存此页面

   <html>
   <title>Your name</title>
   <body>
   <center>Welcome to the coolest page ever</center>
   <br>
   <?php if(isset($_GET['name'])) { $response = "Hi, ".$_GET['name']."! How are you?"; 
   echo $response;}
   ?>
   <br>
   <u>This is the footer</u>
   </body></html>

你的名字
欢迎来到有史以来最酷的页面


这是页脚
您可以将html放入php页面test.php中

下面是我如何在html内部使用echo来显示值的示例

首先,使用php连接到数据库并执行所需的任何查询

我还使用includes使处理每个页面的页眉/导航和页脚变得简单。不必在test.php页面中包含这些代码,您可以创建单独的页面并只包含它们。这样,如果你想编辑你的页眉或页脚,你可以在一个地方做,它将改变你的所有页面。 正如您将在我的html中看到的,您可以将echo值放入html中,如so value=”
//php code begins here
<?php

include_once 'header.php';

$sql = "SELECT * FROM `person` WHERE person_id = " .$user_id." ";

$conn = mysqli_connect( $dbServername , $dbUsername , $dbPassword , $dbName );
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
    echo "user_id: " . $row["person_id"]. " - person_id: " . $row["person_first"]. " " . $row["person_last"]. "<br>";
    $person_id = $row["person_id"];
    $person_first = $row["person_first"];
}
} else {
echo $sql;
}
$conn->close();

?>
//first php section ends here


//html section starts here
<div class="form-group">
                                        <label class="col-md-12">First Name</label>
                                        <div class="col-md-12">
                                            <input type="text" required="" name="person_first" value="<?php echo $person_first;?>" class="form-control form-control-line"> </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-md-12">Last Name</label>
                                        <div class="col-md-12">
                                            <input type="text" required="" name="person_last" value="<?php echo $person_last;?>" class="form-control form-control-line"> </div>
                                    </div>
                                    <div class="form-group">
                                        <label for="example-email" class="col-md-12">Mobile</label>
                                        <div class="col-md-12">
                                            <input type="text" required="" name="person_mobile" value="<?php echo $person_mobile;?>" class="form-control form-control-line"> </div>
                                    </div>
                                    <div class="form-group">
                                        <label for="example-email" class="col-md-12">Address</label>
                                        <div class="col-md-12">
                                            <input type="address" required=""  name="person_address" value="<?php echo $person_address;?>" class="form-control form-control-line"> </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-md-12">Email</label>
                                        <div class="col-md-12">
                                            <input type="text" required="" name="person_email" value="<?php echo $person_email;?>" class="form-control form-control-line"> </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-md-12">SSN</label>
                                        <div class="col-md-12">
                                            <input type="text" required="" name="SSN" value="<?php echo $SSN;?>" class="form-control form-control-line"> </div>
                                    </div>
//html ends here

//php footer code starts here
<?php
include_once 'footer.php';
?>
// php footer ends here
//php代码从这里开始
//第一节到此结束
//html部分从这里开始
名字
//php页脚到此结束
开始新页眉的代码。php

<?php
include 'includes/dbh.php';

session_start();
if (!isset($_SESSION["u_id"]))
{
  header("location: login.php");
}


$user_id = $_SESSION['u_id'];
echo "UserID is '".$user_id."'";

$sql = "SELECT * FROM users WHERE user_id='$user_id'";
// $sql = "SELECT SINGLE `user_id` FROM `users` WHERE user_id = '".$user_id."'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
    echo "user_id: " . $row["user_id"]. " - Name: " . $row["user_first"]. " " . $row["user_last"]. "<br>";
    $displayname = $row["user_first"]. " " . $row["user_last"];
    $email = $row["user_email"];
    $uid = $row["user_uid"];
}
} else {
echo "0 results";
}
$sql = "SELECT * FROM players WHERE player_id=$user_id";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
    echo "player_id: " . $row["player_id"]. " - player_id: " . $row["player_first"]. " " . $row["player_last"]. "<br>";
    $player_id = $row["player_id"];
    $player_first = $row["player_first"];
    $player_last = $row["player_last"];
    $player_mobile = $row["mobile"];
    $player_address = $row["player_address"];
    $player_city = $row["city"];
    $player_state = $row["state"];
    $player_zip = $row["zip"];
    $player_dob = $row["dob"];
    $player_gender = $row["gender"];
}
}

$conn->close();
?>

是否要在指定的html位置打印响应变量@ⵍⵢⴻⵙ 是的,基本上是这样。你可以使用Ajax。如果你想使用PHP,你可以使用
“PHP就在这里”
。你可能需要添加一个PHP处理程序,使PHP能够在.html文件上执行,并且该方法将根据你的服务器架构(Apache/Nginx/IIS)而有所不同。您也可以使用javascript或jQuery在运行时打印变量。如果我这样做,人们可以从view source或inspect Element中查看我的php代码php代码在前端从未显示。浏览器中将只显示回显文本。如果我这样做,人们可以从view source或inspect Element中查看我的php代码,所以您可以使用include来执行此操作。例如,页面顶部包含的my header.php代码在此页面源代码中不可见
<?php
include 'includes/dbh.php';

session_start();
if (!isset($_SESSION["u_id"]))
{
  header("location: login.php");
}


$user_id = $_SESSION['u_id'];
echo "UserID is '".$user_id."'";

$sql = "SELECT * FROM users WHERE user_id='$user_id'";
// $sql = "SELECT SINGLE `user_id` FROM `users` WHERE user_id = '".$user_id."'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
    echo "user_id: " . $row["user_id"]. " - Name: " . $row["user_first"]. " " . $row["user_last"]. "<br>";
    $displayname = $row["user_first"]. " " . $row["user_last"];
    $email = $row["user_email"];
    $uid = $row["user_uid"];
}
} else {
echo "0 results";
}
$sql = "SELECT * FROM players WHERE player_id=$user_id";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
    echo "player_id: " . $row["player_id"]. " - player_id: " . $row["player_first"]. " " . $row["player_last"]. "<br>";
    $player_id = $row["player_id"];
    $player_first = $row["player_first"];
    $player_last = $row["player_last"];
    $player_mobile = $row["mobile"];
    $player_address = $row["player_address"];
    $player_city = $row["city"];
    $player_state = $row["state"];
    $player_zip = $row["zip"];
    $player_dob = $row["dob"];
    $player_gender = $row["gender"];
}
}

$conn->close();
?>