【问题标题】:HTML,PHP. Change text in textarea cause of checked(change) radio buttonHTML,PHP。更改已选中(更改)单选按钮的文本区域中的文本
【发布时间】:2015-03-09 12:09:04
【问题描述】:

您好,我想在单击(更改)单选按钮时更改文本区域中的文本。我有自己的数据库 clanok(article),其中属性是 nadpis(title) 和文本。谢谢。

$result = mysqli_query($con,"SELECT * FROM clanok ORDER BY nadpis");
while($row = mysqli_fetch_array($result))
{
?>
<input type="radio" name="nadpis" value="<?php echo $row['nadpis']; ?> "> 
<?php
echo $row['nadpis'];
echo "<br>";  
}
?>
<textarea name="text" cols="30" rows="5" >                       
<?php
echo $row['text'];
?>
</textarea> 
<br>                
<input type="submit"  value="Odoslať článok">

【问题讨论】:

  • 您的答案的解决方案涉及 jquery。看here

标签: javascript php html textarea radio


【解决方案1】:

首先,页面加载完成后,您无法从数据库中获取新内容并将其显示在您的页面上(除非您使用 ajax),因此这意味着您的所有文本行都应该被加载和存储事先某处。

或者,您可以使用 ajax 每次从数据库中获取一些新文本。这两种解决方案都会给您的资源带来两种不同类型的压力。

我会给你非 ajax 的解决方案,但如果你想听 ajax 的解决方案,请告诉我,我也会在这里添加。如果您希望您的网站在单击时更改,您可以使用 javascript 来完成此操作。还有一个基于单选按钮当前状态(选择或未选择)的纯 CSS 解决方案,但这个 javascript 对我来说更容易编写。 ……所以给你。没有ajax,有javascript的解决方案。

<?php

$radio_button_div_text = ""; //makes a variable to store all the text to be displayed in section 1
$textarea_div_text = ""; //makes a variable to store all the text to be displayed in section 1
$list_of_all_ids_for_js_function = ""; //makes a variable to store something that will later be part of the javascript function

$result = mysqli_query($con,"SELECT * FROM clanok ORDER BY nadpis");
while($row = mysqli_fetch_array($result)) { //while there are rows to get...
    //put the different buttons and textareas into two separate variables
    $radio_button_div_text += 'input type="radio" name="nadpis" value="'.$row['nadpis'].'" onclick="show_only_one_textarea('.$row['nadpis'].')">';
    $radio_button_div_text += $row['nadpis'];
    $radio_button_div_text += '<br>';
    $textarea_div_text += '<textarea id='.$row['nadpis'].' name="text" cols="30" rows="5" style="display:none;">';
    $textarea_div_text += $row['text'];
    $textarea_div_text += '</textarea>';
    $list_of_all_ids_for_js_function += 'document.getElementById(\''.$row['nadpis'].'\').style.display="none";'    }

//now, after you got all the info sorted, show the two areas!    
echo $radio_button_div_text;
echo '<br><br>';
echo $textarea_div_text;

?>

<!--now, what you need, is some javascript to make some textareas visible when you click on the corresponding radio button -->
<script>
function show_only_one_textarea(area_id) {
    <?php echo $list_of_all_ids_for_js_function; ?>
    document.getElementById(area_id).style.display="block";    }
</script>

<!--now, everything will display properly and the javascript will work. here is your submit button -->
<br>                
<input type="submit"  value="Odoslať článok">

对不起,我没有在我自己的网站上测试过这段代码......它可能有一些带有 '" 引号的错误。但如果你仔细阅读它,你就会明白这个想法在那里尝试做的事情。你对 javascript 有什么了解吗?如果你需要澄清,请随时问我!

祝你好运。

【讨论】:

    猜你喜欢
    • 2018-07-21
    • 1970-01-01
    • 2014-03-20
    • 2017-10-03
    • 2021-11-25
    • 1970-01-01
    • 1970-01-01
    • 2021-09-06
    • 1970-01-01
    相关资源
    最近更新 更多