【问题标题】:Redirect in same page and display message to user在同一页面重定向并向用户显示消息
【发布时间】:2021-06-16 12:05:53
【问题描述】:

我希望用户填写表单并单击按钮以在同一页面中重定向并向他发送消息“一切顺利,谢谢!”

目前我只使用action="mail_handler.php" 执行此操作,但它会转到其他 php 文件并在其他空白页中显示消息。

有没有什么方法可以使用 mail_handler.php 的功能,但不在那里导航? 我试过action="" 和按钮之前<?php include 'mail_handler.php'; ?> 但没有成功

Index.html 表单:

<!-- The Form Section -->
<div style="background-color: #5A5377" id="form_section">
    <h2 class="w3-wide" align="Center"><font color="white"><strong><br>Přihláška</strong></font></h2>
    <form align="center" name="form1" action="" method="post" enctype="multipart/form-data">
      <ul class="errorMessages"></ul>
      <div class="row">
        <div class="col">
          <label for="fname"><font color="white"><strong>Jméno</strong></font></label></div>
        <div class="col">
          <input type="text" id="fname" name="fname" value="" required="required"><br></div>
      </div>
      <div class="row">
        <div class="col">
          <label for="lname"><font color="white"><strong><br>Příjmení</strong></font></label></div>
        <div class="col">
          <input type="text" id="lname" name="lname" value required="required"div>
      </div>
      <div class="row">
        <div class="col">
          <label for="vysoho"><font color="white"><strong><br>Vysoká škola</strong></font></label></div>
        <div class="col">
          <input type="text" id="vysoka_skola" name="vysoka_skola" value="" required="required"></div>
      </div>
      <div class="row">
        <div class="col">
          <label for="fname"><font color="white"><strong><br>Studijní obor</strong></font></label></div>
        <div class="col">
          <input type="text" id="obor" name="obor" value="" required="required"></div>
      </div>
      <div class="row">
        <div class="col">
          <label for="prace"><font color="white"><strong><br>Odkaz na bakalářskou práci</strong></font></label></div>
        <div class="col">
          <input type="text" id="prace" name="prace" value="" required="required"></div>
      </div>
      <div class="row">
        <div class="col">
          <label for="myfile"><font color="white"><strong><br>Bakalářská práce</strong></font></label></div>
        <div class="col">
          <input type="file" id="myfile" name="myfile[]" multiple="multiple" required="required"></div>
      </div>
      <div class="row">
        <div class="col">
          <label for="dropdown"><font color="white"><strong><br>Kategorie v soutěži</strong></font></label></div>
        <div class="col">
          <select id="kategorie" name="kategorie" required="required">
            <option value="science">Science</option>
            <option value="technology">Technology</option>
            <option value="science">Engineering</option>
            <option value="science">Mathematics</option>
          </select>
        </div>
        <br>
      </div>
      <div class="row">
        <div class="col"><input type="checkbox" id="gdpr" name="gdpr" value="" required="required">
          <label for="gdpr"><font color="white"><strong> I agree with GDPR</strong></font></label></div>
      </div>
      <div class="row">
        <div class="col">
        <?php include 'mail_handler.php'; ?>
          <input type="submit" name="submit" value="Poslat"><br><br></div>
      </div>
    </form>
  </div>
</div>
<!--END OF FORM-->

mail_handler.php

<?php 
if(isset($_POST['submit'])){
    $to = "alex@gmail.com"; // this is your Email address
    $first_name = $_POST['fname'];
    $last_name = $_POST['lname'];
    $skola = $_POST['vysoka_skola'];
    $obor = $_POST['obor'];
    $prace = $_POST['prace'];
    $files = $_FILES['myfile']['name'];
    $kategorie = $_POST['kategorie'];
    //echo gettype($files);
    $all_thesis_string="";
    for($index=0;$index<count($_FILES['myfile']['name']);$index++){
        $all_thesis_string.=$_FILES['myfile']['name'][$index].","; //create a string with all bachelor attachments of user
    }

    $subject = "Form submission of ".$first_name." ".$last_name;
    $message = "User Details: \n\n Jméno: $first_name \n Příjmení: $last_name \n Vysoká škola: $skola \n Studijní obor: $obor \n Odkaz na bakalářskou práci: $prace \n Kategorie: $kategorie \n Bakalářská práce: $all_thesis_string";

    $headers = "From:" . $first_name;
    mail($to,$subject,$message,$headers);
    echo "Přihláška úspěšně odeslána. Děkuji " . $first_name . ".\n \n";
    include 'upload.php';
    // You can also use header('Location: thank_you.php'); to redirect to another page. 
    }
?>

【问题讨论】:

  • 只需将 HTML 放入 PHP 中,然后运行 ​​PHP。
  • 不知道为什么将一个包含在另一个中应该起作用。但是你的第二个脚本又包含upload.php,我们不知道那里发生了什么……
  • “但没有成功” - 这不是真正的有用信息;在这种情况下,请给出一个正确的问题描述。发生了什么而不是您预期会发生什么,您遇到了什么错误,等等。

标签: php html


【解决方案1】:

正如 Ken Lee 在 cmets 中建议的那样,最好的方法是将 mail_handler.php 本身用作表单操作。这将成为您的 index.php:

<?php 
if(isset($_POST['submit'])){
    $to = "alex@gmail.com"; // this is your Email address
    $first_name = $_POST['fname'];
    $last_name = $_POST['lname'];
    $skola = $_POST['vysoka_skola'];
    $obor = $_POST['obor'];
    $prace = $_POST['prace'];
    $files = $_FILES['myfile']['name'];
    $kategorie = $_POST['kategorie'];
    //echo gettype($files);
    $all_thesis_string="";
    for($index=0;$index<count($_FILES['myfile']['name']);$index++){
        $all_thesis_string.=$_FILES['myfile']['name'][$index].","; //create a string with all bachelor attachments of user
    }

    $subject = "Form submission of ".$first_name." ".$last_name;
    $message = "User Details: \n\n Jméno: $first_name \n Příjmení: $last_name \n Vysoká škola: $skola \n Studijní obor: $obor \n Odkaz na bakalářskou práci: $prace \n Kategorie: $kategorie \n Bakalářská práce: $all_thesis_string";

    $headers = "From:" . $first_name;
    mail($to,$subject,$message,$headers);
    echo "Přihláška úspěšně odeslána. Děkuji " . $first_name . ".\n \n";
    include 'upload.php';
    // You can also use header('Location: thank_you.php'); to redirect to another page. 
    }
?>
<!-- The Form Section -->
<div style="background-color: #5A5377" id="form_section">
    <h2 class="w3-wide" align="Center"><font color="white"><strong><br>Přihláška</strong></font></h2>
    <form align="center" name="form1" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" enctype="multipart/form-data">
      <ul class="errorMessages"></ul>
      <div class="row">
        <div class="col">
          <label for="fname"><font color="white"><strong>Jméno</strong></font></label></div>
        <div class="col">
          <input type="text" id="fname" name="fname" value="" required="required"><br></div>
      </div>
      <div class="row">
        <div class="col">
          <label for="lname"><font color="white"><strong><br>Příjmení</strong></font></label></div>
        <div class="col">
          <input type="text" id="lname" name="lname" value required="required"div>
      </div>
      <div class="row">
        <div class="col">
          <label for="vysoho"><font color="white"><strong><br>Vysoká škola</strong></font></label></div>
        <div class="col">
          <input type="text" id="vysoka_skola" name="vysoka_skola" value="" required="required"></div>
      </div>
      <div class="row">
        <div class="col">
          <label for="fname"><font color="white"><strong><br>Studijní obor</strong></font></label></div>
        <div class="col">
          <input type="text" id="obor" name="obor" value="" required="required"></div>
      </div>
      <div class="row">
        <div class="col">
          <label for="prace"><font color="white"><strong><br>Odkaz na bakalářskou práci</strong></font></label></div>
        <div class="col">
          <input type="text" id="prace" name="prace" value="" required="required"></div>
      </div>
      <div class="row">
        <div class="col">
          <label for="myfile"><font color="white"><strong><br>Bakalářská práce</strong></font></label></div>
        <div class="col">
          <input type="file" id="myfile" name="myfile[]" multiple="multiple" required="required"></div>
      </div>
      <div class="row">
        <div class="col">
          <label for="dropdown"><font color="white"><strong><br>Kategorie v soutěži</strong></font></label></div>
        <div class="col">
          <select id="kategorie" name="kategorie" required="required">
            <option value="science">Science</option>
            <option value="technology">Technology</option>
            <option value="science">Engineering</option>
            <option value="science">Mathematics</option>
          </select>
        </div>
        <br>
      </div>
      <div class="row">
        <div class="col"><input type="checkbox" id="gdpr" name="gdpr" value="" required="required">
          <label for="gdpr"><font color="white"><strong> I agree with GDPR</strong></font></label></div>
      </div>
      <div class="row">
        <div class="col">
          <input type="submit" name="submit" value="Poslat"><br><br></div>
      </div>
    </form>
  </div>
</div>
<!--END OF FORM-->

在 PHP 脚本中,您可以通过添加 echo 来显示您想要显示的任何消息

您的第二个选择是将表单定位到 iframe:

<iframe name="myframe" id="frame1" src="thankyou.php" style="display:none"></iframe>
<form action="../thankyou.php" method="post" target="myframe">
<input type="submit" name="DoIt" value="DoIt">
</form> 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-07
    • 2019-01-06
    • 1970-01-01
    • 2011-09-05
    相关资源
    最近更新 更多