简体   繁体   English

在HTML textarea中打印PHP变量

[英]Printing a PHP variable in HTML textarea

I have a PHP snippet which generates the required output in a variable $ans1 . 我有一个PHP代码段,它在变量$ans1生成所需的输出。 What I want to do is print this variable $ans1 in a <textarea> . 我想做的是在<textarea>打印此变量$ans1 I tried to write the following code but it generates the output as usual and not in the textbox. 我试图编写以下代码,但是它照常生成输出,而不是在文本框中。 The following is my PHP code: 以下是我的PHP代码:

while($row = mysqli_fetch_array($result)) {
    if($submit3 == "Positive") {
        $ans1 = $row['reply_yes'];
        echo $ans1;
    } else if($submit3 == "Negative") {
        $ans1 =  $row['reply_no'];
        echo $ans1;
    }
    echo "<br/>";
    break;
}

And following is my HTML code: 以下是我的HTML代码:

<form method="post" action="fetch_page.php">
    <input type="submit" name="submit1" value="Positive" onclick="enter()"/>
    <input type="submit" name="submit2" value="Negative" onclick="enter()"/>
    <textarea name="txt1" cols="66" rows="10" id="txt1"> </textarea>
    <script>
        function enter()
        {
           document.getElementById('txt1').value= <?php echo htmlspecialchars($ans1);?>;
        }
    </script>
</form>

Please tell me where am I going wrong. 请告诉我我要去哪里错了。

Adding quotes like this isnt working either 像这样添加引号也不起作用

 document.getElementById('txt1').value= "<?php echo htmlspecialchars($ans1);?>";

As you can see in the following image, the answer(the not bold part) should get printed in the textbox also according to my html code 如下图所示,答案(非粗体部分)也应根据我的html代码打印在文本框中 在此处输入图片说明

You need to add quotes around the echoed value: 您需要在回显值周围添加quotes

document.getElementById('txt1').value = "<?php echo htmlspecialchars($ans1);?>";

And your script should be situated in <head> 并且您的脚本应位于<head>

Edit 编辑

What about using this: 怎么使用这个:

document.getElementById('txt1').value = "<?php Print($ans1); ?>";

You can add the text you want to be displayed in the textarea between the <textarea> tag. 您可以在<textarea>标签之间的<textarea>添加要显示的文本。

<textarea name="txt1" cols="66" rows="10" id="txt1">
    <?php echo $ans1; ?>
</textarea>

If the text still doesn't appear or you get an error then make sure you access variables from the global scope. 如果文本仍然没有出现或出现错误,请确保您从全局范围访问变量。 Like below. 像下面。

<textarea name="txt1" cols="66" rows="10" id="txt1">
    <?php echo $GLOBALS['ans1']; ?>
</textarea>

You didn't surround the php with quotes. 您没有用引号将php引起来。 The following works: 以下作品:

    <form method="post" action="fetch_page.php">
        <input type="submit" name="submit1" value="Positive" onclick="enter()"/>
        <input type="submit" name="submit2" value="Negative" onclick="enter()"/>
        <textarea name="txt1" cols="66" rows="10" id="txt1"> </textarea>
        <script>
            function enter()
            {
                document.getElementById('txt1').value = "<?php echo htmlspecialchars($ans1); ?>";
            }
     </script>
 </form>

Your question is not a PHP problem. 您的问题不是PHP问题。 You can't see any output from the function because your script is halted upon submission! 您看不到函数的任何输出,因为您的脚本在提交后就暂停了! So change the submit buttons to type="button" and add a ID, then use the script below. 因此,将提交按钮更改为type="button"并添加一个ID,然后使用以下脚本。 Using jQuery (to minimize t he code you need to write) and use a timeout to actually have time for the function be able to display the results. 使用jQuery(以最大程度地减少需要编写的代码),并使用超时来实际让函数有时间显示结果。

 $(".button").click(function() {
      var buttonName = $(this).attr('name'),
      elm = $(this);
      $('#txt1').val( buttonName + ' was clicked.' ); // Add response
      setTimeout(function(){               
           elm.get(0).form.submit(); // Submit form           
      }, 5000); // After 5 seconds
 });

JSFIDDLE JSFIDDLE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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