【发布时间】:2019-05-05 00:11:35
【问题描述】:
我搜索了有关如何使用基本 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> " . " " . "$companyname" . "<br />" . "<strong>Company Website: </strong> " . " " . "$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>
单击提交按钮后,表单应该消失,消息显示在同一页面上(不会重定向到另一个页面),同时通过电子邮件将结果发送给您。
注意:我在 sn-p 中尝试过,但没有成功。但我将它加载到一个实时站点,它运行良好,没有错误。
【问题讨论】:
-
您是从哪里了解到
<align="center">标签的? -
@Script47 我相信 W3schools 在线。它们是初学者的绝佳来源。
-
您能否提供一个链接或告诉我您是如何到达该页面的?顺便说一句,如果人们听到您的评论,预计会被引用 w3fools 或其他一些嘲弄术语。
-
@Script47 我脸皮厚。我是初学者,该网站帮助我建立了继续进一步学习的信心。但感谢您的提醒。他们的网站上有多个参考资料。只需在他们的网站上搜索 align 标签。我不认为他们是专门为它做的,特别是。看起来我在这里没有正确使用它。哎呀。感谢您指出它。我现在正在删除它。
标签: php html forms email session