Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
182 views
in Technique[技术] by (71.8m points)

putting variable from php into html

I've searched and i couldn't find my fault,
I want to display the $content inside my textarea. I've also tried to put it into the value attribute but that didn't work either.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<?php
$search = array('foo','bar','foobar');
$replace = array('f**','b**','f**b**');

if (isset($_POST['cencor']) &&! empty($_POST['cencor'])){
    $content = $_POST['cencor'];
}

?>
<form action="findstring.php" method="post">
    <textarea name="cencor" rows="10" cols="80"><?php echo $content ?></textarea><br><br>
    <input type="submit" value="submit">
</form>

</body>
</html>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should be getting the following error in your <textarea> as soon as the page is loaded:

<b>Notice</b>: Undefined variable: content in <b>/path/to/file.php</b> on line <b>x</b><br />

Yet you're not checking for errors.

What you need to do is to use a conditional statement inside it for the $content variable.

Sidenote: The missing semi-colon in there, is perfectly valid since there is no further (PHP) instructions being passed after it, so let's lose that misconception (in comments).

However, it would be needed if there was a conditional statement when using the following:

if(isset($content)){ echo $content }

and would fail because it's looking for an "end of statement" and throwing the following:

Parse error: syntax error, unexpected '}', expecting ',' or ';' in...

  • The semi-colon being an "end of statement" character.

It's unclear as to what you really want to do here with the arrays, so I won't be able to help you there.

Another sidenote:

&&! empty is valid, but for the sake of argument, I've made it more readable, && !empty.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);

$search = array('foo','bar','foobar');
$replace = array('f**','b**','f**b**');

if (isset($_POST['cencor']) && !empty($_POST['cencor'])){
    $content = $_POST['cencor'];
}

?>
<form action="findstring.php" method="post">
    <textarea name="cencor" rows="10" cols="80"><?php if(isset($content)){ echo $content; } ?></textarea><br><br>
    <input type="submit" value="submit">
</form>

</body>
</html>

Footnotes:

This conditional statement:

if (isset($_POST['cencor']) && !empty($_POST['cencor']))

can be cut down to merely:

if (!empty($_POST['cencor']))

There's no need to use isset(), as !empty() is quite enough.

On an added note:

In order to avoid an undefined variable notice, you could have done the following and assigning "nothing" to the $content variable, which is perfectly valid:

$search = array('foo','bar','foobar');
$replace = array('f**','b**','f**b**');

    if (isset($_POST['cencor']) &&! empty($_POST['cencor'])){

        $content = $_POST['cencor'];
    }

$content = "";

Closing notes:

If the goal here is to keep the value that was entered in the <textarea> for the duration of the browser's session and the user navigating throughout your site, then that is just what you need to use here, sessions.

For example:

PHP:

if(!empty($_POST['cencor'])){

    $_SESSION['var'] = $_POST['cencor'];
    $_POST['cencor'] = $_SESSION['var'];

}

HTML:

<textarea name="cencor" rows="10" cols="80"><?php if(isset($_SESSION['var'])){ echo $_SESSION['var']; } ?></textarea>

NOTA: If this answer does not solve what it is you want to achieve, you will need to elaborate on your question.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...