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

时间:2015-02-01 16:05:20

标签: php html mysql

我在php中创建了这个脚本但是我需要隐藏最后两个表,直到" Procurar uma Letra"被点击..我是怎么做到的?
或者,如果$_POST[letra] null ,则将其定义为" EmptyLetter" Word或其他东西......

我尝试了几件事,但没有奏效。 我怎么能这样做?

<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);
?>

2 个答案:

答案 0 :(得分:0)

PHP Manual为您提供以下答案:

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

在此示例中,PHP将跳过条件不是的块   遇到了,即使它们不在PHP开放/关闭标签之外,PHP   因为PHP解释器会根据条件跳过它们   跳过未满足的条件中包含的块。对于   输出大块文本,退出PHP解析模式是   通常比通过echo或发送所有文本更有效   打印。

这被称为“逃离HTML”。您可以将$_POST[]放在条件中。

答案 1 :(得分:0)

好的,所以每个人都可以看到已解决的代码:

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

并且提交表单输入必须如下声明:

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