1

Hello i have an issue with an exercise with checkboxes in php. I know how to get the value of each checkbox but in this case the value should be aplying style to an input text. The code i have till now it's this:

<?php
$negrita = 'unchecked';
$cursiva = 'unchecked';
$subrayado = 'unchecked';

if (isset($_REQUEST['submit'])) {

    if (isset($_POST['negrita'])){

        echo "<p style='font-weight: bold'>" . $_POST['texto']. "</p>" ;
    }
    if (isset($_POST['cursiva'])){
        echo "<p style='font-style: italic'>" . $_POST['texto']. "</p>" ;
    }
    if(isset($_POST['subrayado'])){
        echo "<p style='text-decoration: underline'>" . $_POST['texto']. "</p>" ;
    }

}
?>

<form action="ejercicio2_form.php" method="post">
    <p>Introduce el texto a mostrar:
        <input type="text" name="texto" value="<?php if (isset($_REQUEST["texto"])) echo $_POST['texto'];?>" size="20"> <br />
    </p>
    Estilo del texto:
        <input type="checkbox" name="negrita" <?php if (isset($_POST['negrita']) && $_POST['negrita']=="negrita") echo "checked";?> value="negrita"> Negrita
        <input type="checkbox" name="cursiva" <?php if (isset($_POST['cursiva']) && $_POST['cursiva']=="cursiva") echo "checked";?> value="cursiva"> Cursiva
        <input type="checkbox" name="subrayado" <?php if (isset($_POST['subrayado']) && $_POST['subrayado']=="subrayado") echo "checked";?> value="subrayado"> Subrayado <br />
    <input type="submit" name="submit" value="Aceptar">

</form>

</body>

If in the text box i write "hello!" and then click all 3 checkboxes i get 3 different "hello!" one in bold another in italic and the third one underline. What i want is to get only one "hello!" with all the three text styles applyed.

1
  • You should populate a style variable inside your isset($_Post['...']) statements and apply it to the echoed paragraph. Commented Jan 23, 2016 at 12:31

1 Answer 1

2

Something like

if (isset($_REQUEST['submit'])) {

    $style = "";

    if (isset($_POST['negrita'])){
        $style .= "font-weight: bold;";
    }
    if (isset($_POST['cursiva'])){
        $style .= "font-style: italic;";
    }
    if(isset($_POST['subrayado'])){
        $style .= "text-decoration: underline;";
    }

    echo "<p style='" . $style . "'>" . $_POST['texto']. "</p>" ;
}

Here is a working example.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.