Ajax Crud Returning Error Message - php

I have an ajax crud table. It works perfectly fine when i want to edit and view (modal pops-up with correct info). Now when i submit the modal to insert a new user i get an error from php and ajax saying there is not data available but when i run var_dump in php it shows they are values. i have an isset condition to check if values are posted, if they are it should assign the posted data to a php variable else if false it will pass the error back to ajax.
The issue is no matter what i do it always returns the condition as false despite the data being posted
PHP Code
<?php
var_dump($_POST);
$balance = $nextdate = $error = $action = $interest_id = "";
$valid = true;
if(isset($_POST['balance']) && empty($_POST['balance']))
{
$balance = ['balance'];
}
else
{
$valid = false;
$error .= "* balance is required.\n";
$balance = '';
}
if(isset($_POST['nextdate']) && empty($_POST['nextdate']))
{
$nextdate =$_POST['nextdate'];
}
else
{
$valid = false;
$error .= "* date is required.\n";
$accountnumber = '';
}
if(isset($_POST['interest_id']) && empty($_POST['interest_id']))
{
$interest_id = $_POST['interest_id'];
}
else
{
$valid = false;
$error .= "* email is required.\n";
$interest_id = "";
}
?>
Ajax Code
if(valid == true)
{
var form_data = {
balance : balance,
nextdate : nextdate,
action : action,
interest_id : interest_id
};
$.ajax({
url : "insert.php",
type : "POST",
data : form_data,
dataType : "json",
success: function(response){
if(response['valid']==false)
{
alert(response['msg']);
}
else
{
if(action == 'add')
{
$("#add_new_user_modal").modal('hide');
html += "<tr class=user_"+response['interest_id']+">";
html += "<td>"+response['interest_id']+" </td>";
html += "<td>"+response['balance']+" </td>";
html += "<td>"+response['nextdate']+" </td>";
html += "<td><a href='javascript:void(0);' onclick='edit_user("+response['interest_id']+");'><i class='glyphicon glyphicon-pencil'></i></a> <a href='javascript:void(0);' onclick='delete_user("+response['bank_id']+");'><i class='glyphicon glyphicon-trash'></i></a></td>";
html += "<tr>";
$("#usersdata").append(html);
}
else
{
window.location.reload();
}
}
}
});
}
else
{
return false;
}
});
These are my var_dump results
array(4)
{ ["balance"]=> string(8) "50000.00"
["nextdate"]=> string(10) "2016-08-18"
["action"]=> string(4) "edit"
["interest_id"]=> string(19) "anonymous#gmail.com" }

Your test is incorrect. You accepting a blank value. Thats why fails when you have a value.
if(isset($_POST['balance']) && empty($_POST['balance']))
should be
if(isset($_POST['balance']) && !empty($_POST['balance']))

So i finally figured it out, the problem was the '== 'add'' on the if statement
Original
if($action=='add')
Changed
if($action = 'add')

Related

Can't Check Checkbox Value With Php and AJAX

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.

allow only one or some email extension

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";

Stop Execution Php

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

Button Submit false works, but not the redirecting

Currently, I am working on a formula. I created the check function and they are all working. If the check function returns false nothing gets saved, but it still redirects to the congrats page?
here the code:
function check() {
var msg = "";
if ($_POST['Bustransfer'] == "ja" ) {
msg = "wrong\n";
document.formular.v6.style.backgroundColor = "#FF0000";
}
if (msg == "") return(true);
else {
alert(msg);
return(false);
}
}
and then I got the following in the body section:
<form method="post" action="Email.php" name="formular" onsubmit="return check()">
I don't get it, hope you can help me. Cheers!
You are mixing them(javascript & php) up. Should be '<?php echo $_POST['Bustransfer']; ?>' instead of $_POST['Bustransfer'] in the check.
function check() {
var msg = "";
if ('<?php echo $_POST['Bustransfer']; ?>' == "ja" ) {
msg = "wrong\n";
document.formular.v6.style.backgroundColor = "#FF0000";
}
if (msg == "") return(true);
else {
alert(msg);
return(false);
}
}

AJAX with HTML?

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

Categories