簡體   English   中英

單擊提交按鈕時隱藏 html,回顯結果保留在同一頁面上,提供電子郵件表單結果示例

[英]Hide html when submit button clicked, echo results staying on same page, email form results EXAMPLE PROVIDED

我搜索了一個基本的解釋和示例,說明如何使用基本的 php 隱藏我的 html 表單“onsubmit”,同時保持在同一頁面上。 我還需要通過電子郵件發送表單結果。 我在這里和那里發現了一些通常很復雜並且超出我初學者能力的東西。 因此,我將分享一個基本示例,說明我認為獲得此信息的最簡單方法。

帶有 php 的 HTML 表單:

<?php
session_start();
//if you require login start session first thing at top

?>
<html>

<head>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<script type="text/javascript">


</script>

</head>

<title>Example Form </title>

<?php

//my database connection is in insert.php

include_once 'insert.php';

//Set up email to receive the desired form results

$submit = $_POST['submit'];

$to = "youremail@yourdomain.com";
$email = "youremail@yourdomain.com";
$user = $_SESSION['yoursession']; #this is if require user login
$companyname = $_POST['companyname'];
$companyurl = $_POST['companyurl'];
$ipaddress = $_SERVER['REMOTE_ADDR']; #capture user's ip address
$subject = $companyname;

if(isset($submit) && !empty($companyname || $companyurl)){


$headers = 'From:'. $email . "\r\n"; // Sender's Email
//$headers .= 'Cc:'. $email . "\r\n"; // Carbon copy to Sender

$body = "

Company Name: $companyname \n\n Company URL: $companyurl \n\n User IP Address: $ipaddressadvertise

";

if(mail($to, $subject, $body, $headers)){

//What will show after submit button selected...

echo "Successfully submitted!" . "<br />";

echo "<br />" . "<strong>Company Name:  </strong>  " . "&nbsp;" . "$companyname" . "<br />" . "<strong>Company Website:  </strong>  " . "&nbsp;" . "$companyurl" . "<br />";
}else {

    echo "Oops something went wrong. Try again or come back later. " . "<br />";

}
}
?>

<body>

<?php

//This is where you start to wrap what you want to hide onsubmit.

$submit = $_POST['submit'];
if(!isset($submit)){

//Do NOT put closing curly brace leave open and see below.

?>


<form id="form" name="form" action="" method="post" <?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>


<table border="0">

<tr>Company Name: <align = "center"><input type = "text" id = "companyname" name = "companyname" value = "" placeholder = "Required" required><br /><br />

<tr>Company Website: <align = "center"><input type = "text" id = "companyurl" name = "companyurl" value = "" placeholder = "Required" required><br /><br />


<input type="submit" name="submit" id="submit" onclick = "location.href='mailto:youremail@yourdomain.com';" value="Submit!">

<tr></tr><br /><br />
<tr></tr>
<tr></tr>

</form>

<?php

  } //This is where you put your closing curly brace wrapping all of the information you want to hide when submit button is clicked.

?>
</body>
</html>

單擊提交按鈕時,表單應該消失,消息顯示在同一頁面上(無需重定向到另一個頁面),同時通過電子郵件將結果發送給您。

注意:我在代碼片段中嘗試過這個,但沒有用。 但是我將它加載到了一個實時站點,它運行良好,沒有錯誤。

基本結構可以是這樣的:

<?php
  if (isset($_POST['submit']))  {   // if submit - send email
    $you = "YOUREMAIL";
    $email = $_REQUEST['email'];
    $subject = $_REQUEST['subject'];
    $comment = $_REQUEST['comment'];
    mail($you, $subject, $comment, "From:" . $email);   //send email
    echo "Thank you for contacting us!";    //Email response
  }


  else  {   // else display the form:
?>
    <form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>">
      <label for="email">Email: 
        <input name="email" id="email" type="email" />
      </label>
      <label for="subject">Subject: 
        <input name="subject" id="subject" type="text" />
      </label>
      <label for="comment">Comment:
        <textarea name="comment" id="comment" rows="15" cols="40"></textarea>
      </label>
      <input type="submit" value="Submit" />
    </form>
<?php  } ?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM