简体   繁体   中英

send text from a php file to a variable in html

Can i send text from a php file into a variable in an html file? I have an if statement in my php, that checks if a field is empty. If it is empty then i want to send to my html file a text saying 'please fill the field' and put it in a label in my html. Is there a way for that?

<?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";
}

?>

Yes, you'll just echo out the HTML like any other ordinary echo.

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

It can be embed right in your HTML (as long it's a PHP file, obviously) like this:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM