Compare the results

Asked

Viewed 199 times

0

Dear colleagues.

I’m bringing two questions from the Mysql database as follows:

....
Qual sua idade? <input type="text" name="Perguntas[]" value="<?php echo $jmPerguntas->Perguntas; ?>">
Você tem o segundo grau? <input type="text" name="Perguntas[]" value="<?php echo $jmPerguntas->Perguntas; ?>">
Especifique:
<textarea name="Especifique[]" style="width: 200px; height: 80px"></textarea>
    .....

And I get the results with PHP this way:

for($i = 0; $i < count($_POST['Perguntas']); $i++){

if($_POST['Perguntas'][$i] == 'Você tem o segundo grau?'){
   echo "aqui";   
}else{
   echo "não aqui";
}

} // fim do for()

The only problem is that the stretch:

if($_POST['Perguntas'][$i] == 'Você tem o segundo grau?'){
   echo "aqui";   
}else{
   echo "não aqui";
}

Is giving "not here", even though the question is 'You have high school?'.

Would anyone know what I’m doing wrong?

Thank you!

  • What would be a value for $jmPerguntas->Perguntas ? yes or no?

  • 1) Give a var_dump or print_r in $_POST. 2) This is some kind of form validation check?

  • Could also explain why the fields have the same name(perguntas[])?

  • But in this example of yours, why don’t you put the idea instead of bringing the question itself? For there would facilitate in your comparison above... if($_POST['Questions'][$i]='1'){ echo "here"; }.. But I exemplified otherwise, maybe, with the example you can adapt and get where you want.

2 answers

3


I made the following example: See if it helps you.

PHP code

<?
    if(isset($_POST['pergunta'])){

            if(!empty($_POST['pergunta'][0])){
               echo "<strong>Idade:</strong> ".$_POST['pergunta'][0]."<br>";   
            }

            if(!empty($_POST['pergunta'][1])){
               echo "<strong>Segundo Grau:</strong> ".$_POST['pergunta'][1]."<br>";   
            }

            if(!empty($_POST['especifique'])){
               echo "<strong>Especificação:</strong> ".$_POST['especifique']."<br><br><br>";   
            }

    }
?>

HTML code

<html>
    <meta charset="utf-8">
    <form action="<? echo $PHP_SELF; ?>" name="perguntas" method="post" enctype="multipart/form-data">

        Qual sua idade? <br>
        <input type="text" name="pergunta[]" value="<? if(isset($_POST['pergunta'])){ echo $_POST['pergunta'][0]; } ?>">
        <br><br>
        Você tem o segundo grau? <br>
        <input type="text" name="pergunta[]" value="<? if(isset($_POST['pergunta'])){ echo $_POST['pergunta'][1]; } ?>">
        <br><br>
        Especifique:<br>
        <textarea name="especifique" style="width: 200px; height: 80px"><? if(isset($_POST['especifique'])){ echo $_POST['especifique']; } ?></textarea><br><Br>
        <input type="submit" value="enviar">
    </form>
</html>

In this way, we have the question array which in turn, in array 0, is the first question, if answered, returns on the screen.. so continue with the other fields.

  • Sorry for the delay. Thank you to all who are willing to help.

3

I’ve devised a simple code for you to understand what happens.

<?php

if($_POST){
    for($i = 0; $i < count($_POST['Perguntas']); $i++){

        if($_POST['Perguntas'][$i] == 'Você tem o segundo grau?'){
            echo "aqui";   
        }else{
            echo "não aqui";
        }

    } // fim do for()
}

?>

example

</head>

<body>

    <form method="POST" action="index.php" >
        <label>Qual sua idade?</label> <input type="text" name="Perguntas[]" value="Qual sua idade?" >
        <br/>
        <label>Você tem o segundo grau? </label><input type="text" name="Perguntas[]" value="Você tem o segundo grau?" >
        <input type="submit" value="Enviar">
    </form>


</body>

The reason it shows this message in the second question is in the comparison that your PHP is programmed to do. Note how your POST variable is filled during Submit:

    array (size=2)
  0 => string 'Qual sua idade?' (length=15)
  1 => string 'Você tem o segundo grau?' (length=25)
não aqui
array (size=2)
  0 => string 'Qual sua idade?' (length=15)
  1 => string 'Você tem o segundo grau?' (length=25)
aqui

Note that in its first iteration it compares if: 'Are you in high school? == 'How old are you? ' So the answer is no here.

Finally in the second iteration the result is as expected.

There is no error in PHP just it would be interesting if you review the logic.

  • 1

    That’s what I suggested... I also suggested to him to make this comparison through the idPergunta, which would make it easier and solve the question, but his explanation is very correct.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.