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

How hide part of an php page until form is submitted [closed]

提问人:DiogoSaraiva 提问时间:2/2/2015 更新时间:2/2/2015 访问量:2821

问:


想改进这个问题吗?通过编辑这篇文章来更新问题,使其仅关注一个问题。

8年前关闭。

我在 php 中制作了这个脚本,但我需要隐藏最后两个表,直到单击“Procurar uma Letra”。我是怎么做到的?
或者,如果 is null,请将其定义为“EmptyLetter”Word 或其他东西......
$_POST[letra]

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

<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);
?>
PHP 的 HTML MySQL

评论

2赞 Yair.R 2/2/2015
添加条件 if(isset($_POST['submit']) 并在其中添加两个表。
1赞 DiogoSaraiva 2/2/2015
@Yair.R 谢谢它起作用了,但我不得不添加到我的表格中......而不是但是name="submit"(isset($_POST['submit'])(isset($_POST['submit']))

答:

1赞 Robert 2/2/2015 #1

PHP手册给你的答案如下:

<?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[]

0赞 Yair.R 2/2/2015 #2

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

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

提交表单输入也必须这样声明:

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

评论

0赞 DiogoSaraiva 2/2/2015
就我而言:<input type="submit" value="Procurar Letra" name="submit">
0赞 mplungjan 2/2/2015
如果您想使用 JavaScript 提交此表单,您需要将“submit”重命名为其他名称