Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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="submit" 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'];
}
?>
like image 247
Jordan Avatar asked Nov 05 '22 13:11

Jordan


2 Answers

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.

like image 157
RiaD Avatar answered Nov 09 '22 11:11

RiaD


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>";
}

?>
like image 37
Dmitry Avatar answered Nov 09 '22 10:11

Dmitry