开发者

How to format text in php when we have 2 or more options

I'm trying to make a simple script that allow formatting text and submit it.

Here is the form:

<html>
<head>
<title>
</title>
</head>
<body>
<form method="post" action="step2.php">
<input type="text" name="text"  /><br>
Red<input type="radio" name="Red" /> <br>
15px<input type="radio" name="15" /> <br>
<input type="submi开发者_运维知识库t" name="submit"/>
</form>
</body>
</html>

and in the step2.php at this stage i'm showing results when 2 options are selected. I'm trying to show results when only "Red" is select, when only "15px" is selecet, when both are selected and when nothing is selected. Here is my script for the moment:

<?php
if (isset($_POST['Red']) && isset($_POST['15']) ) {
echo "<font size='15px' color=red>";
echo $_POST['text'];
echo "</font>";
}

?>

I succeeded Thanks for answers! the secret was in empty($varname), here's the code

<?php
if (isset($_POST['Red']) && isset($_POST['15']) ) {
echo "<font size='15px' color=red>";
echo $_POST['text'];
echo "</font>";
}

if (empty($_POST['Red']) && isset($_POST['15']) ) {
echo "<font size='15px'>";
echo $_POST['text'];
echo "</font>";
}

if (isset($_POST['Red']) && empty($_POST['15']) ) {
echo "<font color=red>";
echo $_POST['text'];
echo "</font>";
}

if (empty($_POST['Red']) && empty($_POST['15']) ) {
echo $_POST['text'];
}
?>


I think it's better way to do it is some XML/DOM tool

But you can use this code:

$attrs='';
if(isset($_POST['Red']))
    $attrs.='color=red';
if(isset($_POST['15']))
    $attrs.='size="15px";

Besides, you should know that <font> is deprecated now.


Radiobuttons should have the same name, otherwise use checkbox, and also better to use not numeric names for form fields

<html>
<head>
<title>
</title>
</head>
<body>
<form method="post" action="step2.php">
<input type="text" name="text"  /><br>
Red<input type="checkbox" name="Red" value="Red" /> <br>
15px<input type="checkbox" name="px15" value="15" /> <br>
<input type="submit" name="submit"/>
</form>
</body>
</html>

step2.php

<?php
if (isset($_POST['Red']) && isset($_POST['px15']) ) {
echo "<font size='15px' color=red>";
echo $_POST['text'];
echo "</font>";
}

?>


Here is a solution :) :

    <?php
if (isset($_POST['Red']) && isset($_POST['15']) ) {
echo "<font size='15px' color=red>";
echo $_POST['text'];
echo "</font>";
}

if (empty($_POST['Red']) && isset($_POST['15']) ) {
echo "<font size='15px'>";
echo $_POST['text'];
echo "</font>";
}

if (isset($_POST['Red']) && empty($_POST['15']) ) {
echo "<font color=red>";
echo $_POST['text'];
echo "</font>";
}

if (empty($_POST['Red']) && empty($_POST['15']) ) {
echo $_POST['text'];
}
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜