您如何用html格式化php?

时间:2018-12-09 01:59:38

标签: php html

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

<?php

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

?>

我知道我可以在php中回显html脚本,例如:echo "<b>".$response."</b>";,但是如何才能将$response返回到我的html页面,并在html页面中将{{1 }}字符串?

我有一个$response页面:

index.html

如您所见,有一个带有模板的html页面,仅在一个位置(页面中间)需要响应。我不想回显整个html脚本。

如果我将php脚本放入html中,那么我的php脚本就会公开...

示例链接:<title>Your name</title> <center>Welcome to the coolest page ever</center> <br> "The PHP $response goes here" <br> <u>This is the footer</u>

2 个答案:

答案 0 :(得分:0)

使用what.php保存此页面。

public IPagedList<RegionResources> GetRegionResources(
    string cultureName,
    string defaultCultureName,
    int page,
    int pageSize)
{
    var query = context.RegionResources
        .Where(r => r.Culture.CultureName == cultureName && r.IsActive ||
                    r.Culture.CultureName == defaultCultureName);
    query = query.OrderBy(r => r.Name);
    query = query.Include(r => r.Culture);
    return query.ToPagedList(page, pageSize);
}

答案 1 :(得分:0)

您可以将html放在您的php页面test.php中

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

首先执行您的php以连接到数据库并执行所需的任何查询

我还使用include来简化每个页面的页眉/导航和页脚处理。不必在您的test.php页面中包含该代码,您可以创建单独的页面并将其包括在内。这样,如果您要编辑页眉或页脚,则可以在一个位置进行编辑,它将对所有页面进行更改。 正如您将在我的html中看到的那样,然后可以将echo值放入html中,就像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

新页面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();
?>

结束header.php