Php 在同一页上发送时如何隐藏表单?

Php 在同一页上发送时如何隐藏表单?,php,Php,此表单的开发目的是在同一页上发送数据 我想在发送和显示消息时隐藏联系人表单,例如:感谢与我们联系。 我提出的问题是,当您执行发送操作时,表单被隐藏并显示错误消息,但联系人表单保持隐藏状态 <!DOCTYPE html> <html> <head> <title>Contact US</title> </head> <body> <?php session_start(); $errors = '';

此表单的开发目的是在同一页上发送数据

我想在发送和显示消息时隐藏联系人表单,例如:感谢与我们联系。

我提出的问题是,当您执行发送操作时,表单被隐藏并显示错误消息,但联系人表单保持隐藏状态

<!DOCTYPE html>
<html>
<head>
  <title>Contact US</title>
</head>
<body>

<?php
session_start();
$errors = '';
$nombre = '';
$email = '';
$telefono = '';
$mensaje = '';
if(isset($_POST['submit'])) {

  if($_SESSION['vcode'] != $_POST['vcode']) {
    $errors .= "The characters do not match the captcha code";
  }

  $nombre = $_POST["name"];
  $email    = $_POST["mail"];
  $telefono = $_POST["phone"];
  $mensaje  = $_POST["message"];

  if(empty($nombre)) {
    $errors .= "\n Please enter your name. "; 
  }else {
    if (!preg_match("/^[a-zA-Z ]*$/",$nombre)) {
      $errors .= "\n Only letters and spaces are allowed.";
    }
  }
  if(empty($email)) {
    $errors .= "\n Please enter your email. "; 
  }else{
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
      $errors .="\n Invalid email";
    }
  }
  if(empty($telefono)) {
    $errors .= "\n Please enter your phone number. "; 
  }else{
    if(!preg_match("/^\+?\d{1,3}?[- .]?\(?(?:\d{2,3})\)?[- .]?\d\d\d[- .]?\d\d\d\d$/", $telefono)) {
      $errors .="\n Invalid number";
    }
  }
  if($_POST['producto'] == 0){
     $errors .= "\n You must select a product";
  }
  if(empty($mensaje)) {
    $errors .= "\n Please enter your message. "; 
  }
  if(empty($errors)) {
    $asunto = "";
    $message = "Usuario:".$_POST['name']." Email:".$_POST['mail']." Telefono ".$_POST['phone']." Informacion ".$_POST['message'];
    $destino = "contacto@example.com";
    $remitente = "From: contacto@example.com";
    mail($destino,$asunto,$message,$remitente);
    unset($_POST['submit']);
    echo "Thanks for your comments";
  }
}
?>

<div class="error">
<?php
if(!empty($errors)){
  echo "<p class='err'>".nl2br($errors)."</p>";
}else{
?>
</div>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" autocomplete="off" enctype="multipart/form-data">
  <div class="touch">
    <div class="name">
      <input type="text" name="name" placeholder="Name" value='<?php echo htmlentities($nombre) ?>'>
    </div>
    <div class="email">
      <input type="text" name="mail"  placeholder="Email" value='<?php echo htmlentities($email) ?>'>
    </div>
    <div class="phone">
      <input type="tel" name="phone" placeholder="Phone" value='<?php echo htmlentities($telefono) ?>'>
    </div>
    <div class="select-pro">
      <select name="producto">
        <option value="0" selected>Affair...</option>
        <option value="1">Product 1</option>
        <option value="2">Product 2<option>
        <option value="3">Product 3</option>
        <option value="4">Other</option>
      </select>
    </div>
    <div class="Customer-message">
      <textarea id="message" name="message" placeholder="Su consulta..."><?php echo htmlentities($mensaje) ?></textarea>
    </div>
    <div class="capcha">
      <img src="image.php" name="vcode" id="phoca-captcha"/>
      <input name="vcode" type="text" placeholder="Codigo captcha">
    </div>
      <input type="submit" name="submit" value="Enviar">
  </div>
</form>
<?php } ?>
</body>
</html>

联系我们
试试这个

<div class="error">
<?php
if(!empty($errors)){
  echo "<p class='err'>".nl2br($errors)."</p>";
?>
</div>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" autocomplete="off" enctype="multipart/form-data">
  <div class="touch">
    <div class="name">
      <input type="text" name="name" placeholder="Name" value='<?php echo htmlentities($nombre) ?>'>
    </div>
    <div class="email">
      <input type="text" name="mail"  placeholder="Email" value='<?php echo htmlentities($email) ?>'>
    </div>
    <div class="phone">
      <input type="tel" name="phone" placeholder="Phone" value='<?php echo htmlentities($telefono) ?>'>
    </div>
    <div class="select-pro">
      <select name="producto">
        <option value="0" selected>Affair...</option>
        <option value="1">Product 1</option>
        <option value="2">Product 2<option>
        <option value="3">Product 3</option>
        <option value="4">Other</option>
      </select>
    </div>
    <div class="Customer-message">
      <textarea id="message" name="message" placeholder="Su consulta..."><?php echo htmlentities($mensaje) ?></textarea>
    </div>
    <div class="capcha">
      <img src="image.php" name="vcode" id="phoca-captcha"/>
      <input name="vcode" type="text" placeholder="Codigo captcha">
    </div>
      <input type="submit" name="submit" value="Enviar">
  </div>
</form>
<?php
}
else
{
?>

<p>thanks for contacting us.</p>

<?php
}
?>

谢谢联系我们

如果($\u服务器['REQUEST\u METHOD']=='POST'){
//ur php代码(所有方式使用)$\u服务器['REQUEST\u METHOD']
}否则{
}
更新您的代码:

<!DOCTYPE html>
<html>
<head>
  <title>Contact US</title>
</head>
<body>

<?php
@session_start();
if(isset($_POST['submit'])) {

  if($_SESSION['vcode'] != $_POST['vcode']) {
    $errors = "The characters do not match the captcha code";
  }

  $nombre = $_POST["name"];
  $email    = $_POST["mail"];
  $telefono = $_POST["phone"];
  $mensaje  = $_POST["message"];
    $asunto = "";
    $message = "Usuario:".$_POST['name']." Email:".$_POST['mail']." Telefono ".$_POST['phone']." Informacion ".$_POST['message'];
    $destino = "contacto@example.com";
    $remitente = "From: contacto@example.com";
    mail($destino,$asunto,$message,$remitente);
    unset($_POST['submit']);
    $msg= "Thanks for your comments";
}
?>
<div class="error">
<?php
if(isset($msg)){
  echo "<p class='err'>".$msg."</p>";
}else{
    if(isset($errors) echo $errors;
?>
</div>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" autocomplete="off" enctype="multipart/form-data">
  <div class="touch">
    <div class="name">
      <input type="text" name="name" placeholder="Name" value='' oninvalid="this.setCustomValidity('Please enter your name.')" required>
    </div>
    <div class="email">
      <input type="email" name="mail"  placeholder="Email" value='' title="Please enter your email."  oninvalid="this.setCustomValidity('Please enter your email.')" required>
    </div>
    <div class="phone">
      <input type="tel" name="phone" maxlength="10" pattern="[0-9]{10}" title="Please enter your phone number." oninvalid="this.setCustomValidity('Please enter your phone number.')" placeholder="Phone" value='' required>
    </div>
    <div class="select-pro">
      <select name="producto" oninvalid="this.setCustomValidity('Please Select a product.')"  required>
        <option value="">Select One</option>
        <option value="0">Affair...</option>
        <option value="1">Product 1</option>
        <option value="2">Product 2<option>
        <option value="3">Product 3</option>
        <option value="4">Other</option>
      </select>
    </div>
    <div class="Customer-message">
      <textarea id="message" name="message" title="Please enter your message." placeholder="Su consulta..." oninvalid="this.setCustomValidity('Please enter your message.')"  required></textarea>
    </div>
    <div class="capcha">
      <img src="image.php" name="vcode" id="phoca-captcha"/>
      <input name="vcode" oninvalid="this.setCustomValidity('Please enter captcha.')"  type="text" placeholder="Codigo captcha">
    </div>
      <input type="submit" name="submit" value="Enviar">
  </div>
</form>
<?php } ?>
</body>
</html>

联系我们

我想在发送和显示此消息时隐藏表单动态决定何时打印表单或打印感谢消息?因为您有一个
if(isset($\u POST['submit']){…}
,然后只需将
html代码包装在
else{…}
block,提交表单后它将不显示表单……或者使用ajax/js。是的,只需回显您想要的消息并在其下方添加一个出口,然后您可以执行
if(isset($\u POST['submit'])&&$errors=''){……您的表单…}
请原谅我的无知,但我不明白如何在您向我指出的示例中构造PHP代码和HTML表单。刚才我编辑了答案。。请查收。!隐藏表单而不发送朋友?如果(空($errors)){$asunto=“”;$message=“Usuario:.$\u POST['name']”。电子邮件:.$\u POST['mail']。.Telefono.$\u POST['phone']。$\u POST['message'];$destino=”contacto@example.com“;$remotente=”发件人:contacto@example.com“邮件($destino、$asunto、$message、$remotente);未设置($_POST['submit']);$errors.=“\n感谢联系我们。”;}$errors.=“\n感谢联系我们。”如果($errors==“感谢联系我们”,请检查此条件。)
<!DOCTYPE html>
<html>
<head>
  <title>Contact US</title>
</head>
<body>

<?php
@session_start();
if(isset($_POST['submit'])) {

  if($_SESSION['vcode'] != $_POST['vcode']) {
    $errors = "The characters do not match the captcha code";
  }

  $nombre = $_POST["name"];
  $email    = $_POST["mail"];
  $telefono = $_POST["phone"];
  $mensaje  = $_POST["message"];
    $asunto = "";
    $message = "Usuario:".$_POST['name']." Email:".$_POST['mail']." Telefono ".$_POST['phone']." Informacion ".$_POST['message'];
    $destino = "contacto@example.com";
    $remitente = "From: contacto@example.com";
    mail($destino,$asunto,$message,$remitente);
    unset($_POST['submit']);
    $msg= "Thanks for your comments";
}
?>
<div class="error">
<?php
if(isset($msg)){
  echo "<p class='err'>".$msg."</p>";
}else{
    if(isset($errors) echo $errors;
?>
</div>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" autocomplete="off" enctype="multipart/form-data">
  <div class="touch">
    <div class="name">
      <input type="text" name="name" placeholder="Name" value='' oninvalid="this.setCustomValidity('Please enter your name.')" required>
    </div>
    <div class="email">
      <input type="email" name="mail"  placeholder="Email" value='' title="Please enter your email."  oninvalid="this.setCustomValidity('Please enter your email.')" required>
    </div>
    <div class="phone">
      <input type="tel" name="phone" maxlength="10" pattern="[0-9]{10}" title="Please enter your phone number." oninvalid="this.setCustomValidity('Please enter your phone number.')" placeholder="Phone" value='' required>
    </div>
    <div class="select-pro">
      <select name="producto" oninvalid="this.setCustomValidity('Please Select a product.')"  required>
        <option value="">Select One</option>
        <option value="0">Affair...</option>
        <option value="1">Product 1</option>
        <option value="2">Product 2<option>
        <option value="3">Product 3</option>
        <option value="4">Other</option>
      </select>
    </div>
    <div class="Customer-message">
      <textarea id="message" name="message" title="Please enter your message." placeholder="Su consulta..." oninvalid="this.setCustomValidity('Please enter your message.')"  required></textarea>
    </div>
    <div class="capcha">
      <img src="image.php" name="vcode" id="phoca-captcha"/>
      <input name="vcode" oninvalid="this.setCustomValidity('Please enter captcha.')"  type="text" placeholder="Codigo captcha">
    </div>
      <input type="submit" name="submit" value="Enviar">
  </div>
</form>
<?php } ?>
</body>
</html>