I have a email system I am building for a company that they want to send emails with it. I have a custom HTML editor for it. What I am wanting to do it post the contents to a external PHP file and have it add to the database.
function schedule_eblast (html) {
var answer = confirm("Are you sure you want to start sending this E-blast?");
if (answer) {
$.ajax({
type: "POST",
url: "./../../../processes/eblast_schedule.php",
data: {'html_text': html},
success: function(theRetrievedData) {
if (theRetrievedData == "done") {
alert("Your eblast has been successfully scheduled to send. To check it's status, go to the manage eblasts page.");
} else {
alert(theRetrievedData);
}
}
});
return false;
}
And here is what I have for the header in the eblast_schedule.php file:
<?php
include('connect.php');
if ((isset($_POST['html_text'])) && (strlen(trim($_POST['html_text'])) > 0)) {
$html_text = stripslashes(strip_tags($_POST['html_text']));
} else {
$html_text = "";
}
if ((isset($_POST['subject'])) && (strlen(trim($_POST['subject'])) > 0)) {
$subject = stripslashes(strip_tags($_POST['subject']));
} else {
$subject = "";
}
ob_start();
And yes, it does get called. But when outputting html_text, it removes all the HTML. And when adding to the database, it doesn't show the HTML either.
Help! Thanks.
Remove those functions and add mysql_real_escape_string and it should work fine...
include('connect.php');
if ((isset($_POST['html_text'])) && (strlen(trim($_POST['html_text'])) > 0)) {
$html_text = mysql_real_escape_string($_POST['html_text']);
} else {
$html_text = "";
}
if ((isset($_POST['subject'])) && (strlen(trim($_POST['subject'])) > 0)) {
$subject = mysql_real_escape_string($_POST['subject']);
} else {
$subject = "";
}
ob_start();
Related
I have a form and I use one checkbox. Thanks to the Checkbox, a new field is added and the user fills it.
I ran into a problem with everything going perfectly. The checkbox keeps returning empty, which completely destroys my if else structure.
Checkbox HTML Code:
<input type="checkbox" class="custom-control-input headerElementDrop" id="header_element_drop" name="header_element_drop" onchange="valueChanged()">
AJAX Code:
function headerElementInsert() {
var headerElementAdd = $("#headerElementAdd").serialize();
$.ajax({
type: "POST",
data: headerElementAdd,
url: "actions/header-element-add.php",
success: function (insertSuccess) {
if ($.trim(insertSuccess) == "null") {
//
} else if ($.trim(insertSuccess) == "success") {
//
} else if ($.trim(insertSuccess) == "error") {
//
})
}
}
});
}
header-element-add.php code:
if ($_POST) {
$header_element_name = post('header_element_name');
$header_element_icon = post('header_element_icon');
$header_element_link = post('header_element_link');
$header_element_drop = (isset($_POST["header_element_drop"])) ? 1 : 2 ;
$header_element_drop_class_name = post('header_element_drop_class_name');
$header_element_drop_elements = post('header_element_drop_elements');
if($header_element_drop == 1) {
if(!$header_element_name || !$header_element_drop || !$header_element_drop_class_name || !$header_element_drop_elements) {
echo "null";
}else{
$header_element_drop_class = replace_tr($header_element_drop_class_name);
$header_elements = $db->prepare("INSERT IGNORE INTO header_elements SET header_element_name=?, header_element_icon=?, header_element_link=?, header_element_drop=?, header_element_drop_class_name=?, header_element_drop_class=?, header_element_drop_elements=?");
$insert = $header_elements->execute(array($header_element_name,$header_element_icon,$header_element_link,$header_element_drop,$header_element_drop_class_name,$header_element_drop_class,$header_element_drop_elements));
if($insert) {
echo "success";
}else{
echo "error";
}
}
}else{
if(!$header_element_name) {
echo "null";
}else {
$header_elements = $db->prepare("INSERT IGNORE INTO header_elements SET header_element_name=?, header_element_icon=?, header_element_link=?");
$insert = $header_elements->execute(array($header_element_name,$header_element_icon,$header_element_link));
if($insert) {
echo "success";
}else{
echo "error";
}
}
}
}
I tried many ways to solve this problem but none of them worked. I would really appreciate if you could help.
I found the problem. I forgot to give name value to one input. I apologize for the inconvenience caused.
I am trying to send an email with PHP and AJAX, and it finally works, but it won't display validation errors returned from the server. I guess I'm doing something wrong in iterating through that data, or just don't understand something with connecting PHP and jQuery with JSON.
PHP mail script on server:
$to = "mymail#gmail.com";
if (isset($_POST['name'], $_POST['mail'], $_POST['text'])) {
if (empty($_POST['name'])) {
$errors[] = "Molimo unesite Vaše ime";
} else {
$contact_name = htmlentities($_POST['name']);
}
if (empty($_POST['mail'])) {
$errors[] = "Molimo unesite Vašu email adresu.";
} else if (strlen($_POST['mail']) > 60) {
$errors[] = "Vaša email adresa je predugačka.";
} else if (filter_var($_POST['mail'], FILTER_VALIDATE_EMAIL) === false ) {
$errors[] = "Unesite validnu email adresu.";
} else {
$contact_mail = "<" . htmlentities($_POST['mail']) . ">";
}
if (empty($_POST['text'])) {
$errors[] = "Molimo unesite poruku.";
} else {
$contact_text = htmlentities($_POST['text']);
}
}
if (empty($errors) === true) {
if(isset($contact_text, $contact_name, $contact_mail)) {
$subject = "Mail from " . $contact_name ." via www.mysite.com";
$headers = "From: " . $contact_mail;
$sent = mail($to, $subject, $contact_text, $headers);
if ($sent) {
die("true");
} else {
return json_encode($errors);
}
}
}
Relevant jQuery:
var mailBroker = {
send : function() { //initial validation and sending to server
var contact_name = $('input[name="contact-name"]').val();
var contact_mail = $('input[name="contact-mail"]').val();
var contact_text = $('textarea[name="contact-text"]').val();
var status = ""; //send success status
if (contact_name === "" || contact_mail === "" || contact_text === "") {
//form not complete
} else {
$.post("includes/mail.php", { //post form data to server
name : contact_name,
mail : contact_mail,
text : contact_text
}, function(data) {
var response = data;
if (data === "true") { //succesful
mailBroker.setStatus('Poruka poslata.');
} else {
var parsedData = $.parseJSON(data);
$.each(parsedData, function() {
var that = $(this);
setStatus(that);
});
}
});
}
},
setStatus : function(status) {
$('textarea[name="contact-text"]').after('<span>' + status + '</span>');
}
}
And inside $(document).ready():
$('#contact-us form').submit(function(event) {
event.preventDefault();
mailBroker.send();
$(this).trigger('reset');
});
Can somebody point out what I am doing wrong?
Of course, I know that I could just do it on the client-side, but that it is bad practice. So I left that part out for now and assumed that invalid or no data got through for required form fields.
Answer form is easier to explain this. The logic in your code never gives your script a chance to output the errors to the AJAX. You'd need to change the logic so it will. Ie.
if (empty($errors) === true) {
if(isset($contact_text, $contact_name, $contact_mail)) {
$subject = "Mail from " . $contact_name ." via www.mysite.com";
$headers = "From: " . $contact_mail;
$sent = mail($to, $subject, $contact_text, $headers);
if ($sent) {
die("true");
} else {
die("false"); // with json_encode here, $errors will always be empty
}
}
} else {
die(json_encode($errors)); //$errors wasn't empty, this is where you need to hand it back to the JS.
}
This is why firebug or another tool would help. You'd see that the information you were wanting given to your JS wasn't there - that way you know to look at the PHP (server-side) since it isn't outputting as expected. If it was, you'd check in to the JS code to see why that isn't processing it as expected.
EDIT: Your javascript doesn't allow the PHP to execute when a field is empty, but you are wanting the feedback PHP will give if one is empty, so you'd want to change your JS to something like:
var mailBroker = {
send : function() { //initial validation and sending to server
var contact_name = $('input[name="contact-name"]').val();
var contact_mail = $('input[name="contact-mail"]').val();
var contact_text = $('textarea[name="contact-text"]').val();
var status = ""; //send success status
$.post("includes/mail.php", { //post form data to server
name : contact_name,
mail : contact_mail,
text : contact_text
}, function(data) {
var response = data;
if (data === "true") { //succesful
mailBroker.setStatus('Poruka poslata.');
} else {
var parsedData = $.parseJSON(data);
$.each(parsedData, function() {
var that = $(this);
setStatus(that);
});
}
});
},
setStatus : function(status) {
$('textarea[name="contact-text"]').after('<span>' + status + '</span>');
}
}
A little modification of Jon's answer because you will still need to extract the messages from the returned JSON:
var mailBroker = {
'send' : function() { //initial validation and sending to server
var contact_name = $('input[name="contact-name"]').val();
var contact_mail = $('input[name="contact-mail"]').val();
var contact_text = $('textarea[name="contact-text"]').val();
var status = ""; //send success status
$.post("includes/mail.php", { //post form data to server
name : contact_name,
mail : contact_mail,
text : contact_text
}, function(data) {
var response = data;
if (data === "true") { //succesful
mailBroker.setStatus('Poruka poslata.');
$('#contact-us form').trigger('reset');
} else { //validation failed
var parsedData = $.parseJSON(data);
var msg = '';
$.each(parsedData, function() {
var that = $(this);
for (var i = 0; i < that.size(); i++) {
msg += that[i];
}
mailBroker.setStatus(msg); //msg used to be 'that'
msg = '';
});
}
});
},
'setStatus' : function(status) {
$('#validation-msgs').prepend('<p class="validation-msg">' + status + '</p>');
}
}
Essentially - you will need to pass through parsed data to get each of the messages. The problem is that they are also stored as arrays - of characters. So you will need to pass through those, too, and append those characters to a message String.
Then, you should prepend those messages to some container for warnings so they are in the right order. If you don't do that, you will get [Object] instead of the message text.
Hope this helps.
Hi I think you messed up on your line 3 mail script.
if (isset($_POST['name'], $_POST['mail'], $_POST['text'])) {
because you will use comparison operators for that.
Like this below.
if (isset($_POST['name'] && $_POST['mail'] && $_POST['text'])) {
I just want to ask how can i allow #abc.co.uk or #def.com.tr or something else email extenssions. when user register my website ?
Like if user try to register with (name#hotmail.com) then this email is not allowing. But if user try to register with (name#abc.co.uk or #def.com.tr) then user can register the website.
$("#email").change(function()
{
var email = $("#email").val();
var msgbox = $("#estatus");
if(email.length >= 3)
{
$("#estatus").html('<div class="checking">Checking availability...</div>');
$.ajax({
type: "POST",
url: "check_mail.php",
data: "email="+ email,
success: function(msg){
$("#estatus").ajaxComplete(function(event, request, settings){
var d = msg;
var str=msg.substr(0, 2);
$("#estatus").html('');
if(str == 'OK')
{
$("#email").removeClass("no");
$("#email").addClass("yes");
//msgbox.html('<font color="Green"> Ok </font> ');
}
else
{
$("#email").removeClass("yes");
$("#email").addClass("no");
msgbox.html(msg);
}
});
}
});
}
else
{
$("#email").addClass("no");
$("#estatus").html('<div class="error">Enter a valid e-mail</div>');
}
return false;
});
PHP check_mail.php
<?php
error_reporting(0);
include_once 'includes/db.php';
include_once 'includes/Sc_Script.php';
$Sc = new Check_Email();
if(isSet($_POST['email'])){
$value=$_POST['email'];
// Check the mail is already in using or not
$check=$Sc->Login_Check($value,0);
if($check) {
echo '<div class="error">'.$value.' = This email address is already in use.</div>';
} else {
// Else continue
echo 'OK';
}
}
?>
First you need to pull out the domain and then you need to check that it is contained within some whitelist array:
function isDomainAllowed($email_address)
{
$domain = substr($email_address, strrpos($email_address, '#') + 1);
if (in_array(strtolower($domain), array(
'abc.co.uk',
'def.com.tr',
)))
{
return TRUE;
}
return FALSE;
}
if (isDomainAllowed($email_address))
{
// Allowed
}
else
{
// Not allowed
}
You have check each & every whitelist domain with the supplied email.
$emailList = array();
$emailList = ["abc.in","def.uk"];
$flag = false;
foreach($emailList as $email)
{
if(stripos($_POST['email'],$email) != false)
$flag = true;
}
if($flag == false)
echo "Invalid email domain";
I have this code , iam trying to use javascript to stop executing but its not working with javascript , any suggestions ? Am just trying to stop executing if the return was false from the javascript
if(mysql_num_rows($runzz)==0){
echo "<p align='center'><font size='5'>This Item $code1 - $code2 - ".$rw2['description']. "</br></br> Doesn't Exist In The <u><b>".$rowto['name']."</b></u></br></br> Wanna Add IT ?</font></p>";
?>
<script>
function check(){
var r = confirm("Press a button!");
if (r == true) {
return true;
} else {
return false;
}
}
check();
</script>
<?php
}
$insert="INSERT INTO transfercopy(warehouseidfrom,warehouseidto,qty,itemid,uid)VALUES('$from','$to','$qty','$codeid','$uid')";
$run=mysql_query($insert,$con);
if(!$run)die("error".mysql_error());
I am adding sample code to give you an idea, how you could use AJAX Call with it.
<?php
if(mysql_num_rows($runzz)==0){
echo "<p align='center'><font size='5'>This Item $code1 - $code2 - ".$rw2['description']. "</br></br> Doesn't Exist In The <u><b>".$rowto['name']."</b></u></br></br> Wanna Add IT ?</font></p>";
?>
<script>
function check(){
var r = confirm("Press a button!");
if(r) {
// Add additional parameter
// You could use POST method too. Use whatever make sense to you.
var urlLink = 'http://www.example.com/warehouse/record.php?from=<?php echo $from?>&to=<?php echo $to?>';
$.ajax({
type: 'GET',
url: urlLink,
success: function(data) {
if(data == 'success') {
return 'You have successfully added new record!';
}
},
error: function(data) {
console.log(data);
}
});
} else {
return false;
}
}
check();
</script>
<?php } ?>
<?php
// -- New File: record.php File
//
// You might wanna add the check, that it's the legit request and all the PHP Validation
$form = $_GET['from'];
$to = $_GET['to'];
$qty = $_GET['qty'];
$codeid = $_GET['codeid'];
$uid = $_GET['uid'];
$insert="INSERT INTO transfercopy(warehouseidfrom,warehouseidto,qty,itemid,uid)VALUES('$from','$to','$qty','$codeid','$uid')";
$run=mysql_query($insert,$con);
if(!$run) die("error".mysql_error());
else return 'success';
?>
I cannot get this script work. I try to warn if login that user entered is available. But I cannot manage this script to work:
$( "#myRegForm" ).submit(function( event ) {
var errors = false;
var userAvi = true;
var loginInput = $('#login').val();
if( loginInput == ""){
$("#errorArea").text('LOGIN CANNOT BE EMPTY!');
$("#errorArea").fadeOut('15000', function() { });
$("#errorArea").fadeIn('15000', function() { });
errors = true;
}
else if(loginInput.length < 5 ){
$("#errorArea").text('LOGIN MUST BE AT LEAST 5 CHARACTERS!');
$("#errorArea").fadeOut('15000', function() { });
$("#errorArea").fadeIn('15000', function() { });
errors = true;
}
else if (loginInput.length >=5) {
$.post('checkLogin.php', {login2: loginInput}, function(result) {
if(result == "0") {
alert("this");
}
else {
alert("that");
}
});
}
if (errors==true) {
return false;
}
});
Everything works fine until loginInput.length >=5 else block. So I assume there is a problem with getting answer from PHP file, but I cannot handle it, though I tried many different ways. Here is checkLogin.php's file (note that jQuery script and PHP file are in the same folder):
<?php
include ("bd.php");
$login2 = mysql_real_escape_string($_POST['login2']);
$result = mysql_query("SELECT login FROM users WHERE login='$login2'");
if(mysql_num_rows($result)>0){
//and we send 0 to the ajax request
echo 0;
}
else{
//else if it's not bigger then 0, then it's available '
//and we send 1 to the ajax request
echo 1;
}
?>
<?php
include ("bd.php");
$login2 = mysql_real_escape_string($_POST['login2']);
$result = mysql_query("SELECT login FROM users WHERE login='$login2'");
if(mysql_num_rows($result)>0){
//and we send 0 to the ajax request
echo "0"; // for you to use if(if(result == "0") you should send a string
} else {
//else if it's not bigger then 0, then it's available '
//and we send 1 to the ajax request
echo "1";
}
?>
You're literally sending the string 'loginInput'.
change
$.post('checkLogin.php', {login2: 'loginInput'}, function(result) {
to
$.post('checkLogin.php', {login2: loginInput}, function(result) {
Edit
I would just comment out everything except the following for now and see if that at least works
$.post('checkLogin.php', {login2: 'loginInput'}, function(result) { // put loginInput back in quotes
alert('#'+result+'#'); // # to check for whitespace
});