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

时间:2016-08-11 21:13:44

标签: php jquery email

当我输入家庭住址和电子邮件ID并单击“提交”时,电子邮件已成功发送。但它并没有在" div class = successbox"

中显示成功消息

我在做什么错?

我有这个index.html

<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

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

2 个答案:

答案 0 :(得分:1)

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

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

  1. 将index.html重命名为index.php,然后修改此部分:
  2. <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文件,在下半部分说这个:
    2. if ( ! empty($errors)) {
          header('Location:index.php?error=1');
      } else {
         header('Location:index.php?success=1');
      }   
      // echo json_encode($data);  /* remove this line */
      

答案 1 :(得分:0)

看起来您想使用ajax做某事(process.php),然后在原始页面上显示相应的消息。您缺少对process.php的ajax调用。

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

你想在你的ajax调用process.php之后做这样的事情

  $( document ).ajaxComplete(function( event,request, settings ) {
  $( "#msg" ).append( "<li>Request Complete.</li>" );
});

在你的html中,你没有msg id,但是你会显示其中一条消息,就像你显示的消息一样,取决于你的返回数据是否显示成功与失败。

希望这会有所帮助。