单击“提交”按钮时隐藏html,回显结果保持在同一页面上,电子邮件表单结果示例提供 [英] Hide html when submit button clicked, echo results staying on same page, email form results EXAMPLE PROVIDED

查看:54
本文介绍了单击“提交”按钮时隐藏html,回显结果保持在同一页面上,电子邮件表单结果示例提供的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了有关如何隐藏基本php的html表单 onsubmit的基本说明和示例,同时又停留在同一页面上。我还需要通过电子邮件发送表格结果。我在这里和那里通常在初学者能力之外发现了一些困难。因此,我将分享一个基本示例,说明我认为这是最简单的方法。



带有php的HTML表单:

 <?php 
session_start();
//如果您需要登录启动会话,首先需要

?>
< 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>示例表格< / title>

<?php

//我的数据库连接在insert.php

include_once‘insert.php’;

//设置电子邮件以接收所需的表单结果

$ submit = $ _POST [’submit’];

$ to = youremail@yourdomain.com;
$ email = youremail@yourdomain.com;
$ user = $ _SESSION ['yoursession']; #这是如果需要用户登录
$ companyname = $ _POST [‘companyname’];
$ companyurl = $ _POST ['companyurl'];
$ ipaddress = $ _SERVER ['REMOTE_ADDR']; #捕获用户的IP地址
$ subject = $ companyname;

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


$ headers ='From:' 。 $ email。 \r\n; //发件人的电子邮件
// $ headers。=‘抄送:’。 $ email。 \r\n; //复制到发件人

$ body =

公司名称:$ companyname \n\n公司网址:$ companyurl \n\n用户IP地址:$ ipaddressadvertise

;

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

//选择提交按钮后将显示什么...

echo成功提交! 。 < br />;

echo< br /> 。 < strong>公司名称:< / strong>。 & nbsp; 。 $ companyname。 < br /> 。 < strong>公司网站:< / strong>。 & nbsp; 。 $ companyurl。 < br />;
} else {

echo糟糕,出了点问题。请重试或稍后再返回。 < br />;

}
}
?>

< body>

<?php

//这是您开始包装要在提交时隐藏的内容的地方。

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

//不要将大括号括起来,请参见下面。

?>


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


< table border = 0>

< tr>公司名称:< align = center><输入类型=文本 id =公司名称 name =公司名称 value =占位符=必需 required>< br />< br />

< tr>公司网站:< align = center><输入类型=文本 id = companyurl名称= companyurl值=占位符=必需 required>< br />< br />


<输入type = submit name = submit id = submit onclick = location.href =’mailto:youremail@yourdomain.com’; value =提交!>

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

< / form>

<?php

} //这是您放置闭合花括号的地方,其中包含了单击提交按钮时想要隐藏的所有信息。

?>
< / body>
< / html>

单击提交按钮后,该表单应消失,消息显示在同一页面上(不显示)

注意:我在代码段中尝试了此操作,但此操作无效。但是我将其加载到了一个实时站点,并且可以完美运行而没有错误。

解决方案

基本结构可以是这样的: / p>

 <?php 
if(isset($ _ POST ['submit'])){//如果提交-发送电子邮件
$ you = YOUREMAIL;
$ email = $ _REQUEST [’email’];
$ subject = $ _REQUEST ['subject'];
$ comment = $ _REQUEST [’comment’];
mail($ you,$ subject,$ comment,发件人:。$ email); //发送电子邮件
echo谢谢您与我们联系!; //电子邮件回复
}


else {// else显示形式:
?>
< form method = post action =<?php echo htmlspecialchars($ _ SERVER [’PHP_SELF’]);?>>
<标签为=电子邮件>电子邮件:
<输入名称=电子邮件 id =电子邮件 type =电子邮件 />
< / label>
< label for = subject> Subject:
< input name = subject id = subject type = text />
< / label>
< label for = comment>评论:
< textarea name = comment id = comment rows = 15 cols = 40>< / textarea>
< / label>
<输入类型= submit value =提交 />
< / form>
<?php}?>


I searched for a basic explanation and example on how to hide my html form "onsubmit" with basic php, while staying on the same page. I also needed to email the form results. I found bits here and there usually complicated and outside of my beginner abilities. So I am going to share a basic example of what I think is the easiest way to obtain this.

HTML Form with php:

<?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>

When the submit button is clicked the form should disappear, the message displays staying on the same page (without re-direct to another page) while emailing you the results.

NOTE: I tried this in the snippet and it didn't work. But I loaded it to a live site and it works perfectly without errors.

解决方案

A basic structure can be something like that:

<?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  } ?>

这篇关于单击“提交”按钮时隐藏html,回显结果保持在同一页面上,电子邮件表单结果示例提供的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆