Can't Check Checkbox Value With Php and AJAX - php

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.

Related

Ajax Crud Returning Error Message

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')

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

PHP $_GET with jQuery $.get call

I am currently learning jQuery and php.
So I have this php file that gets called with a jQuery function (there is another call somewhere else in the script where a=chkPw):
$.get("checkName.php?a=chkUser&user=" + rqUser, function(data) { ... });
My problem is that the php script doesn't seem to get in the if(isset($_GET)) { } block. I have debugged the value that it returns (1), so I don't understand what's wrong! I have also debugged the value for $_GET['a'] and it is indeed 'chkUser'.
Any help would be greatly appreciated.
<?php
$users = array('bill' => 'ore', 'ted' => 'wood');
if (isset($_GET)) {
if ($_GET['a'] == 'chkUser') {
if (!array_key_exists($_GET['user'], $users)) {
echo 'okay';
} else {
echo 'denied';
}
} elseif ($_GET['a'] == 'checkPw') {
$user = $_GET['user'];
$pw = $_GET['pw'];
$i = 0;
// get username id
foreach (array_keys($users) as $value) {
if ($value == $user) {
$user = $users[i];
}
i++;
}
// match pw
if ($pw == $user) {
echo 'okay';
} else {
echo 'denied'
}
}
}
?>
Hey it seems to me that this is working:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script type="text/javascript">
$.get("checkNames.php?a=chkUser&user=bill", function(data) { console.log(data) });
$.get("checkNames.php?a=chkPass&user=bill&pw=ore", function(data) { console.log(data) });
</script>
<?php
$users = array('bill' => 'ore', 'ted' => 'wood');
if (isset($_GET["a"])) {
if ($_GET["a"] == 'chkUser') {
if (!array_key_exists($_GET['user'], $users)) {
echo 'okay';
} else {
echo 'denied';
}
} elseif ($_GET["a"] == 'chkPass') {
$user = $_GET['user'];
$pw = $_GET['pw'];
if(array_key_exists($user, $users))
{
if($users[$user] == $pw)
{
echo "okay";
}
else
{
echo "denied";
}
}
else
{
echo "denied";
}
}
}
?>
Try this if statement instead of current
if (count($_GET) > 0) {
// existing code
}
Hope this will help you.
With a url like www.something.com/index.html?a=1 This code does in fact work on testing:
$users = array('bill' => 'ore', 'ted' => 'wood');
if (isset($_GET)) {
echo 'something';
}
Try commenting it down further to let us know if it is the whole whole elseif or just a part of it
You could try this:
<?php
if(!empty($_GET)){
...
}
?>
To be sure about the GET payload use var_dump($_GET)

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