将文本从php文件发送到html中的变量

时间:2012-12-28 07:51:54

标签: php html

我可以将php文件中的文本发送到html文件中的变量吗?我的php中有一个if语句,用于检查字段是否为空。如果它是空的,那么我想向我的html文件发送一个文本,说“请填写字段”并将其放在我的HTML中的标签中。有办法吗?

<?php 

$name = $_REQUEST['author'];
$from = $_REQUEST['email'];
$subj = $_REQUEST['sub'];
$message = $_REQUEST['msg'];
$subject = $subj;
$body = $message;
$headers .= "From: ".$from;

$to = "mail@mail.com";

if ($name = ''){
     HERE i want to send a text to a variable named 'reason' in my html in case $name is empty.
    }

if(mail($to,$subject,$body,$headers)) {
    header( "Location: http://www.mysite.com/contactForm-english" );
    } else {
echo "There was a problem sending the mail. Check your code and make sure that the e-mail address ".$to." is valid";
}

?>

2 个答案:

答案 0 :(得分:0)

是的,你就像其他任何普通的回声一样回应HTML。

echo '<label>Test</label>';

它可以嵌入你的HTML中(只要它是一个PHP文件,显然)就像这样:

<!DOCTYPE html>
<html>
    <head>
        <title>Test</title>
    </head>

    <body>
        <?php echo $something === true ? '<label>test</label>' : ''; ?>
    </body>
</html>

答案 1 :(得分:0)

<?php echo $variable; ?>