首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在php文件中发布数据后,在索引页中打印成功消息

在php文件中发布数据后,在索引页中打印成功消息
EN

Stack Overflow用户
提问于 2016-08-12 05:13:44
回答 2查看 1.2K关注 0票数 1

当我输入家庭地址和电子邮件Id并单击提交时,电子邮件发送成功。但是它没有在"div class = successbox“中显示成功消息。

我做错了什么?

我有这个index.html

代码语言:javascript
复制
<div class="w-container report">
<h2 class="reportheading"><br><br>Get Your Free On Demand<br>Comprehensive Home Valuation</h2>
<p class="reporttext">Receive current market trends; algorithms; neighborhood facts; comparable listing and sale prices; Sell Time!
  <br>
  <br>Your custom report is published and returned same day.</p>
<div class="w-form">
  <form name="myForm"  id="wf-form-Free-Report-2" action="process.php" name="wf-form-Free-Report" data-name="Free Report" method="post" class="reportform">
    <div class="w-clearfix formcenter">
      <input id="Home" type="text" placeholder="Enter Home Address, City &amp; Zip" name="Home" data-name="Home" required="required" class="w-input formbox spaceright">
      <input id="Email-2" type="email" placeholder="Enter Email Address" name="Email-2" data-name="Email" required="required" class="w-input formbox spaceleft">
    </div>
    <div class="btncenter">
      <input id="submit" type="submit" value="Submit" data-wait="Please wait..." class="w-button formbutton">
    </div>
  </form>
  <div class="w-form-done succesbox">
    <p>Thank you!
      <br>Your Free report will be generated based on your physical address and will be emailed to you shortly.</p>
  </div>
  <div class="w-form-fail">
    <p>Excuse us but something went wrong when trying to submit the form.
      <br>Please try again.</p>
  </div>
</div>
</div>

我有process.php

代码语言:javascript
复制
<?php

$errors         = array();      // array to hold validation errors
$data           = array();      // array to pass back data

if (empty($_POST['name']))
    $errors['name'] = 'Name is required.';

if (empty($_POST['email']))
    $errors['email'] = 'Email is required.';

$to = "abc@xyz.com"; // this is your Email address
$from = $_POST['Email']; // this is the sender's Email address
$Home = $_POST['Home'];

$subject = "Request for Comprehensive Home Valuation";
$message = "House Address: ".$Home."<br/>Email Id: ".$from;

error_log("popat11 check this line", 0);    



if (mail($to,$subject,$message))
{

    $data['success'] = true;
    $data['message'] = 'Thank you!
                                  <br>Your Free report will be generated based on your physical address and will be emailed to you shortly.</p>';
}

// return all our data to an AJAX call

echo json_encode($data);
EN

回答 2

Stack Overflow用户

发布于 2016-08-12 05:27:16

这被设置为由javascript (AJAX)发起,并由javascript接收/处理。你有支持这个的javascript代码吗?

如果你没有使用javascript,你可以这样修改你的代码(一个非常简单的例子):

  1. 将index.html重命名为index.php,然后修改此部分:

代码语言:javascript
复制
<div class="w-form-done succesbox">
  <?php if (!empty($_REQUEST['success'])) { ?>
    <p>Thank you!
      <br>Your Free report will be generated based on your physical address and will be emailed to you shortly.</p>
  <?php } else if (!empty($_REQUEST['error'])) ?>
    <p>Sorry! There was a problem./p>
  <?php } ?>
</div>

  1. 修改你的php文件,让它在下面的部分显示:

代码语言:javascript
复制
if ( ! empty($errors)) {
    header('Location:index.php?error=1');
} else {
   header('Location:index.php?success=1');
}   
// echo json_encode($data);  /* remove this line */
票数 1
EN

Stack Overflow用户

发布于 2016-08-12 05:38:37

看起来您想使用ajax做一些事情( process.php ),然后在原始页面上显示一条适当的消息。您错过了对process.php的ajax调用。

您可以查看http://api.jquery.com/jquery.ajax/来查看一些示例ajax函数。

在对process.php进行ajax调用之后,YOu想要这样做

代码语言:javascript
复制
  $( document ).ajaxComplete(function( event,request, settings ) {
  $( "#msg" ).append( "<li>Request Complete.</li>" );
});

在您的html中,您没有msg id,但是您可以根据返回的数据显示成功或失败,而不是带有“通过”或“失败”内容的div,显示其中一条消息。

希望这能有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38906256

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档