简体   繁体   English

PHP表单提交成功消息在消息后插入不需要的数字

[英]PHP form submit success message inserts unwanted number after message

Created a php form with a success message that appears just below the Submit button upon successful mission. 创建了一个带有成功消息的php表单,成功完成任务后,该消息就会显示在“提交”按钮的正下方。 After adding some additional code in the php to create an email confirmation, I'm now noticing that a number "1" has been inserted in the line after my success message - see below: 在php中添加了一些其他代码以创建电子邮件确认后,我现在注意到在成功消息后的行中插入了数字“ 1”-参见下文:

在此处输入图片说明

Any ideas on how to make that number 1 go away? 关于如何使数字1消失的任何想法? Code below: 代码如下:

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$company = $_POST['company'];
$message = $_POST['message'];
$human = $_POST['human'];
$from = 'From: Page Name'; 
$to = 'email@mysite.com';  
$subject = 'Service Inquiry';
$body = "From: $name\n E-Mail: $email\n Phone: $phone\n Company: $company\n   
Message:\n $message";

// Confirmation email.
$conf_subject = 'Your recent inquiry';

$conf_sender = 'MY SITE <no-reply@mysite.com>';

$msg =  $_POST['name'] . ",\n\nThank you for your recent inquiry. A member of 
our team will respond to your message as soon as possible.\n\nThanks,\n\nMy 
Company Team";

if ($_POST['submit']) {
   if ($name != '' && $email != '' && $phone != '' && $message != '') {
       if ($human == '4') {              
           if (mail ($to, $subject, $body, $from)) { 
           echo '<p>Thanks for your inquiry, we will get back to you as soon 
                 as we can&#33;</p>';
           echo (mail ($email, $conf_subject, $msg, 'From: ' . $conf_sender 
           ));
    } else { 
        echo '<p>Something went wrong, go back and try again&#33;</p>'; 
    } 
    } else if ($_POST['submit'] && $human != '4') {
        echo '<p>You answered the anti-spam question incorrectly&#33;</p>';
    }
    } else {
        echo '<p>You need to fill in all required fields&#33;</p>';
    }
}
?>

Thanks everyone! 感谢大家!

This line: 这行:

echo (mail ($email, $conf_subject, $msg, 'From: ' . $conf_sender));

You are echoing out the result of your call to the mail function. 您正在回显对mail函数的调用结果。 Since the mail was successfully handed over to the server it returns true. 由于邮件已成功移交给服务器,因此它返回true。 When you echo out a boolean true it gets converted to an integer which is 1 . 当您回显布尔值true它将转换为1的整数。 That's why you see that in your code. 这就是为什么您在代码中看到它的原因。

Remove the echo to remove the 1 from being displayed in your output. 删除echo以从输出中删除显示的1

 echo (mail ($email, $conf_subject, $msg, 'From: ' . $conf_sender ));

This code is causing the echo, its echoing 1 as the mail() function is returning true. 这段代码引起了回显,当mail()函数返回true时,其回显为1。 I'm not sure why you're echoing the mail function here any way, just remove the echo and all is good. 我不确定为什么您要以任何方式在此处回显邮件功能,只需删除回显就可以了。

That is because of this code: 那是因为这段代码:

echo (mail ($email, $conf_subject, $msg, 'From: ' . $conf_sender ));

You "echo" the result of the mail function. 您“回显”邮件功能的结果。 "1" is equal to "true" on mail sending success. 发送成功时,“ 1”等于“ true”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM