Question

<!DOCTYPE HTML>
<html>
<body style = "background-color: green;">

<?php

$inicioFibonacciErro = $fimFibonacciErro = "";
$inicioFibonacci = $fimFibonacci = "";

if($_SERVER["REQUEST_METHOD"] == "POST"){
    if(empty($_POST["inicioFibonacci"])){
        $inicioFibonacciErro = "O primeiro numero da sequencia eh necessario";
    }
    else{
        $inicioFibonacci = test_input($_POST["inicioFibonacci"]);
    }
    if(empty($_POST["fimFibonacci"])){
        $fimFibonacciErro = "O ultimo numero da sequencia eh necessario";
    }
    else{
        $fimFibonacci = test_input($_POST["fimFibonacci"]);
    }
}

function test_input($dados){
    $dados = trim($dados);
    $dados = stripslashes($dados);
    $dados = htmlspecialchars($dados);
    return $dados;
}
?>

<h1 style = "text-align: center; color: red;">Digite os valores desejado pra a    sequencia de fibonacci</h1>
<!-- Caso queira mandar pra outra pagina utilizar action = "nomedapagina" -->
<!-- Caso queira mandar pra a propria pagina '<?php echo  htmlspecialchars($_SERVER["PHP_SELF"]);?>' -->
<form action= <?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>  method="post"> 
Primeiro numero da sequencia: <input type="text" name="inicioFibonacci" />
<spam class = "error">* <?php echo $inicioFibonacciErro ?> </spam> <br /><br />
Ultimo numero da sequencia: <input type="text" name="fimFibonacci" />
<spam class = "error">* <?php echo $fimFibonacciErro ?> </spam> <br /><br />
<input type="submit" value="Veja o resultado!" />
</form>

</body>
</html>

This code will generate a page that requires two numbers to create a fibonacci sequence. I have written a conditional that shows a message to the user if he didn't write one these two numbers.

My doubt here is: The action i'm using is useful to send the error (if there is some) to the same page. But if there isn't any erros nothing happens.

What I want do, if possible, is to create a condition where I use this action if there is an error, but if there is not, it use the action to send the numbers to other page that will show the fibonacci sequence.

Was it helpful?

Solution

You can embed HTML in PHP conditionals during page generation as follows:

<p>Some html goes here</p>
<?php if(CONDITION) { ?>
  <p>Unlimited html goes here</p>
<?php } else { ?>
  <p>However much html you want to handle the false-case goes here</p>
<?php } ?>

If you want to perform conditionals after the page has been generated, you will need a client-side scripting language: JavaScript.

OTHER TIPS

you can use template frameworks (such as smarty) to use conditions to determine which html code to display. for example say you have results where one of the columns has a value of 1 or 2, you could take your result set and do this (html tags shown just to show the blending)

<table>
{foreach $results as $result}
    <tr><td>
    {if $result.mycol == 1}
        {$result.mycol}
    {else}
        random text
    {/if}
    </td></tr>
{/foreach}
</table>

this would output something like

1
random text
1
1
random text

in the case of smarty, your html code goes in a file with the extension .tpl and you use your php code to send the php variables to the template

$smarty->assign("results", $results);
$smarty->display("results.tpl");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top