gpt4 book ai didi

php - 单击提交按钮时隐藏 html,回显结果停留在同一页面上,电子邮件表单结果 示例提供

转载 作者:行者123 更新时间:2023-11-28 00:40:57 24 4
gpt4 key购买 nike

我搜索了关于如何使用基本 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 } ?>

关于php - 单击提交按钮时隐藏 html,回显结果停留在同一页面上,电子邮件表单结果 示例提供,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53598174/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com