在提交表单之前如何隐藏php页面的一部分[关闭]

在提交表单之前如何隐藏php页面的一部分[关闭]

问题描述:

I made this script in php but I need hide the two last tables until "Procurar uma Letra" is clicked.. How I do that?
Or maybe if $_POST[letra] is null define it to "EmptyLetter" Word or something else...

I tried a couple of things but didn't worked. How can I do that??

<html>
<head>
<script>
function validateForm(form) {
    var x = document.forms["myForm"]["nome"].value;
    if (x==null || x=="") {
        alert("Nome a Inserir, deve ser preenchido");
        return false;
    }

    var y = document.forms["myForm"]["sexo"];
    if ( ( y[0].checked == false ) && ( y[1].checked == false ) ) {
        alert ( "Por favor escolha: Nome Masculino ou Feminino" ); 
        return false;

    }
}
</script>
</head>
<body>
<h1>BASE DE DADOS DE NOMES PR&Oacute;PRIOS</h1>
<br>
<h3>Enviar um nome para a base de dados</h3>

<form name="myForm" action="insert.php" onsubmit="return validateForm(this.form)" method="post">
Nome a inserir: <input type="text" name="nome" /><br><br>
<input type="radio" name="sexo" value="m" /> Nome Masculino&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="radio" name="sexo" value="f" /> Nome Feminino <br><br>
<input type="submit" value="Enviar">
</form>

<form action="index.php" method="post">
Ir para a letra:    <input type="text" name="letra" />
<input type="submit" value="Procurar Letra">
</form>

<br><br><br>

</body>
</html>




<?php
$servername = "localhost";
$username = "username";
$password = "password";
$database ="nomes";

$con=mysqli_connect($servername,$username,$password,$database);

$masculinos = mysqli_query($con,"SELECT nome FROM nomes WHERE sexo = 'm';");
$femininos = mysqli_query($con,"SELECT nome FROM nomes WHERE sexo = 'f';");
$letramasculinos = mysqli_query($con,"SELECT nome FROM nomes WHERE nome LIKE '$_POST[letra]%' AND sexo = 'm';");
$letrafemininos = mysqli_query($con,"SELECT nome FROM nomes WHERE nome LIKE '$_POST[letra]%' AND sexo = 'f';");

echo "<table style='display: inline-block;' width='150px' border='1'>
<tr>
<th>Nomes Masculinos</th>
</tr>";

while($row = mysqli_fetch_array($masculinos))
{
echo "<tr>";
echo "<td><center>" . $row['nome'] . "</center></td>";
echo "</tr>";
}
echo "</table>";

echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";

echo "<table style='display: inline-block;' width='150px' border='1'>
<tr>
<th>Nomes Femininos</th>
</tr>";

while($row = mysqli_fetch_array($femininos))
{
echo "<tr>";
echo "<td><center>" . $row['nome'] . "</center></td>";
echo "</tr>";
}
echo "</table>";

echo "<table style='display: inline-block;' width='150px' border='1'>";

echo "<br><br><br>";

while($row = mysqli_fetch_array($letramasculinos))
{
echo "<tr>";
echo "<td><center>" . $row['nome'] . "</center></td>";
echo "</tr>";
}
echo "</table>";

echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";

echo "<table style='display: inline-block;' width='150px' border='1'>";

while($row = mysqli_fetch_array($letrafemininos))
{
echo "<tr>";
echo "<td><center>" . $row['nome'] . "</center></td>";
echo "</tr>";
}
echo "</table>";

mysqli_close($con);
?>

Ok so the resolved code so everyone can see:

if(isset($_POST['submit'])){
     //tables will shown only after submitted
}

And also the submit form input must be declared like this:

<input type="submit" name="submit" value="Submit">

PHP Manual gives you the answer as follows:

<?php if ($expression == true): ?>
  This will show if the expression is true.
<?php else: ?>
  Otherwise this will show.
<?php endif; ?>

In this example PHP will skip the blocks where the condition is not met, even though they are outside of the PHP open/close tags, PHP skips them according to the condition since the PHP interpreter will jump over blocks contained within a condition what is not met. For outputting large blocks of text, dropping out of PHP parsing mode is generally more efficient than sending all of the text through echo or print.

This is known as, "Escaping from HTML". You can put the $_POST[] in the conditional.