/    Sign up×
Community /Pin to ProfileBookmark

PHP contact form sending details to txt file and email

Ok so my pop-up and contact form works perfectly. All details get sent to my email and txt file. Once I add the code to send the details also to my txt file, my sending button gets stuck on the prompt “Sending….” – when I remove the code

[code=html]$data = “$usermail | $content | $namen”;

$fh = fopen(“users.txt”, “a”);

fwrite($fh, $data);

fclose($fh);

print “User Submitted”;[/code]

the form then works 100% and goes from “Sending…”, to “Sucessfully Sent, Thanks!” and closes the popup. (Please note that even when it gets stuck on “Sending…” I still get the details to my txt file and my email so there is no issue there. The only issue is when I add the txt sending code, it gets stuck on “Sending…”

SendMessage.php

[code=html]<?php
$sendto = “[email protected]”;
$usermail = $_POST[’email’];
$content = nl2br($_POST[‘msg’]);
$name = nl2br($_POST[‘name’]);

$subject = “New PlanetChannel Contact”;
$headers = “From: ” . strip_tags($usermail) . “rn”;
$headers .= “Reply-To: “. strip_tags($usermail) . “rn”;
$headers .= “MIME-Version: 1.0rn”;
$headers .= “Content-Type: text/html;charset=utf-8 rn”;

$msg = “<html><body style=’font-family:Arial,sans-serif;’>”;
$msg .= “<h2 style=’font-weight:bold;border-bottom:1px dotted #ccc;’>New Contact Info</h2>rn”;
$msg .= “<p><strong>Sent by:</strong> “.$usermail.”</p>rn”;
$msg .= “<p><strong>Name:</strong> “.$content.”</p>rn”;
$msg .= “<p><strong>Video URL:</strong> “.$name.”</p>rn”;
$msg .= “</body></html>”;

if(@mail($sendto, $subject, $msg, $headers)) {
echo “true”;
} else {
echo “false”;
}

$data = “$usermail | $content | $namen”;

$fh = fopen(“users.txt”, “a”);

fwrite($fh, $data);

fclose($fh);

print “User Submitted”;

?>[/code]

Index.php

[code=html]
<div id=”input-popup-boxes”>
<form id=”contact” name=”contact” action=”#” method=”post”>
<label for=”msg”></label>
<input id=”msg” placeholder=”Name..” name=”msg” class=”txt”>
<br>
<label for=”email”></label>
<input type=”email” placeholder=”Email..” id=”email” name=”email” class=”txt”>
<div id=”add-video-box”><strong class=”add-video-text”>Add Video Here</strong>
<br>
<label for=”name”></label>
<input id=”name” placeholder=”Video URL..” name=”name” class=”txt”></div>

<div id=”connect-button”><button id=”send”>Connect Now</button>
</form>
</div>
</div>
</div>

<!– basic fancybox setup –>
<script type=”text/javascript”>
function validateEmail(email) {
var reg = /^(([^<>()[]\.,;:s@”]+(.[^<>()[]\.,;:s@”]+)*)|(“.+”))@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}])|(([a-zA-Z-0-9]+.)+[a-zA-Z]{2,}))$/;
return reg.test(email);
}

$(document).ready(function() {
$(“.modalbox”).fancybox();
$(“#contact”).submit(function() { return false; });

$(“#send”).on(“click”, function(){
var emailval = $(“#email”).val();
var msgval = $(“#name”).val();
var msgval = $(“#msg”).val();
var msglen = msgval.length;
var mailvalid = validateEmail(emailval);

if(mailvalid == false) {
$(“#email”).addClass(“error”);
}
else if(mailvalid == true){
$(“#email”).removeClass(“error”);
}

if(msglen < 4) {
$(“#msg”).addClass(“error”);
}
else if(msglen >= 4){
$(“#msg”).removeClass(“error”);
}

if(mailvalid == true && msglen >= 4) {
// if both validate we attempt to send the e-mail
// first we hide the submit btn so the user doesnt click twice
$(“#send”).replaceWith(“<em>Sending…</em>”);
$.ajax({
type: ‘POST’,
url: ‘sendmessage.php’,
data: $(“#contact”).serialize(),
success: function(data) {
if(data == “true”) {
$(“#contact”).fadeOut(“fast”, function(){
$(this).before(“<p><strong>Successfully Sent, Thanks!</strong></p>”);
setTimeout(“$.fancybox.close()”, 1300);
});
}
}
});
}
});
});
</script>

[/code]

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@mg3authorJun 11.2014 — $5 donation via paypal to the first person that can provide me the solution. All these views and no reply. Please help.
Copy linkTweet thisAlerts:
@GravyJun 12.2014 — Remove

[code=php]
print "User Submitted";
[/code]


Because of this you're getting:

[B]TrueUser Submitted[/B] returned instead of [B]True[/B], so it's not considered as a success or failure.

[PHP ] [/PHP ] also work better than [HTML ] [/HTML ] tags and may help geta faster response in the fuutre.
×

Success!

Help @mg3 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...