如何修复此问题,以便在php调用$ fontcolor时 并且用户选择选项value =“fblue”,字体颜色将为蓝色?
同样,如果用户为字体大小1选择值=“size1”,则如何 根据用户选择更改字体,因为这是一个单选按钮 我想只有值会被发送到php脚本,在这种情况下是size1?
带有php脚本的部分将在它自己单独的.php文件中,但不会 包含在html中,这只是为了显示脚本的.php部分是什么样的。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>
Webpagegenerator
</title>
</head>
<body>
<center>
<h1>Web Page Generator</h1>
<h3>Generates a web page based on user choice:</h3>
<form method = "post"
action = "webpagegene.php">
<h3>User inputs text body: </h3>
<textarea name = "textbody"
rows = "10"
cols = "40">
</textarea>
<br/>
<br/>
<table border = "2" >
<tr>
<td><h3>Font Color</h3></td>
<td colspan = "2"><h3>Background Color</h3></td>
</tr>
<tr>
<td>
<!--How do I fix this so that when php calls $fontcolor
and the user selects the option value = "fblue" the font color will be blue?-->
<select name = "fontcolor">
<option value = "fblue">Blue</option>
<option value = "fred">Red </option>
<option value = "fgreen">White</option>
</select>
</td>
<td>
<select size = "5"
name = "backgroundcolor">
<option value = "byellow">Yellow</option>
<option value = "bsilver">Silver</option>
<option value = "bmaroon">Maroon</option>
<option value = "bpink">Pink</option>
</select>
</td>
<!--Likewise, if the user selects the value = "size1" for Font Size 1, how
do I change the font based on the user selection, since this is a radio button
I think only the value will be sent to the php script, in this case size1?-->
<td>
<input type = "radio"
name = "sizeType"
value = "size1"/>Font Size 1<br/>
<input type = "radio"
name = "sizeType"
value = "size2"/>Font Size 2<br/>
<input type = "radio"
name = "sizeType"
value = "size3"/>Font Size 3<br/>
</td>
</tr>
</table>
<br/>
<input type = "submit" value = "show me"/>
<br/>
<br/>
</form>
<?php
// The php script seems to work; however, in terms of
// the script manipulating attributes such as changing
// the font, color or background when requested by the user, it
// does not seem to yield any results :-(.
// I can however have the user type text into the text field, but this is not
// enough, I want to be able to manipulate it-change it's font size, font color
// and the background color for the text.
$backgroundcolor = $_REQUEST["backgroundcolor"];
$fontcolor = $_REQUEST["fontcolor"];
$textbody = $_REQUEST["textbody"];
$sizeType = $_REQUEST["sizeType"];
$theStyle = <<< HERE
"$textbody$fontcolor$sizeType"
HERE;
print $textbody;
?>
</center>
</body>
</html>
答案 0 :(得分:0)
您需要使用表单进行回发。
类似的东西:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
<select size="5" name="backgroundcolor">
<option value="byellow">Yellow</option>
<option value="bsilver">Silver</option>
<option value="bmaroon">Maroon</option>
<option value="bpink">Pink</option>
</select>
<input type="submit" value="Change" />
</form>
回发后,您将收到$ _GET ['backgroundcolor']的输入。
我建议使用Javascript,Cookies和Sessions。
使用jQuery(Javascript框架)和jQuery Cookie Plugin:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
<select size="5" name="backgroundcolor" id="bgcolor">
<option value="yellow">Yellow</option>
<option value="silver">Silver</option>
<option value="maroon">Maroon</option>
<option value="pink">Pink</option>
</select>
<input type="submit" value="Change" id="sbtbtn" />
</form>
<script type="text/javascript">
<![CDATA[
$("#sbtbtn").hide(); // hide the Change button so that
// you can dynamically do changes
// without a post back
$("#bgcolor").change(function(){
var val = $("#bgcolor").val();
$("body").css({backgroundColor: val})
// changes the background color
$.cookie('changebgcolor', val, { expires: 10 });
// sets the cookie and value, then expire in 10 days.
});
]]>
</script>
在后端(PHP):
<?php
if(isset($_COOKIE['changebgcolor'])){
$_SESSION['bgcolor'] = $_COOKIE['changebgcolor'];
setcookie ('changebgcolor', '', time() - 3600);
// unset the cookie
}
$_SESSION['bgcolor'] = $_SESSION['bgcolor']?$_SESSION['bgcolor']:'white';
echo '<style type="text/css"><!--
body{background-color:'.$_SESSION['bgcolor'].'}
--></style>';
?>
答案 1 :(得分:0)
我认为你误解了PHP的作用。
服务器在将脚本发送到用户的浏览器之前处理该脚本。
服务器处理完PHP脚本后,它将采用纯HTML格式,您需要采用不同的技术来获取所需的更改。
Mauris提出了一些方法。只是谷歌他们,你应该找到很多例子。
其他一些好的搜索术语是DOM,AJAX和DHTML。
答案 2 :(得分:0)
或者你可以使用jQuery(javascript)来改变颜色。 如果你使用javascript,你也不必重新加载页面。
以下是http://www.smallsharptools.com/SmallSharpTools.EmbeddedScripts/JQuery.aspx
的示例查看源代码。
答案 3 :(得分:0)
您想要在新屏幕上使用新样式打印textarea中的文本,还是显示更改textarea内容的同一页面?