if else statement is returning always same value [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am new about PHP and ajax
Ajax return data=="OK" or data=="NO" depends on the email in the database
but if-else block returns always same value.It prints always last block like "email is available!" I couldn't find the mistake.
$.ajax({
type: 'post',
url: 'check_email.php',
data: {email:email,},
success: callback});}
function callback(data){
email_error = false;
$("#email_error").css("display","block");
if(data =="OK"){
$("#email_error").html("Email was already used!");
$("#email_error").css("color","#990000");
$("#email_error").show();
email_error = true;
}else{
$("#email_error").html("Email is available!");
$("#email_error").css("color","#F1F0D1");
$("#email_error").show();
email_error = false;
}
}

ajax
var email = "example#example.com";
$.ajax(
{
type: 'post',
url: 'check_email.php',
data:
{
email: email,
},
success: callback
});
function callback(data)
{
email_error = false;
$("#email_error").css("display", "block");
if (data == "OK")
{
$("#email_error").html("Email was already used!");
$("#email_error").css("color", "#990000");
$("#email_error").show();
email_error = true;
}
else if(data =="NO")
{
$("#email_error").html("Email is available!");
$("#email_error").css("color", "#F1F0D1");
$("#email_error").show();
email_error = false;
}
}
html
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="check_email.js"></script>
<title></title>
</head>
<body>
<div id="email_error">
</body>
</html>
php
<?php
echo "NO";
?>

I got a simple solution in link Only I changed "OK" with "\r\nOK".
$.ajax({
type: 'post',
url: 'check_email.php',
data: {email:email,},
success: callback});}
function callback(data){
email_error = false;
$("#email_error").css("display","block");
if(data =="\r\nOK"){
$("#email_error").html("Email was already used!");
$("#email_error").css("color","#990000");
$("#email_error").show();
email_error = true;
}else{
$("#email_error").html("Email is available!");
$("#email_error").css("color","#F1F0D1");
$("#email_error").show();
email_error = false;
}
}

Related

What is wrong js and php objects [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
In test.php i have this code
<?php
if (isset($_POST['user'])) {
echo "hi";
}
exit; ?>
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
<script>
var user = {
'name': 'Wayne',
'country': 'Ireland',
'selected': new Array(1, 5, 9)
};
var userStr = JSON.stringify(user);
$.ajax({
url: 'test.php',
type: 'POST',
data: {user: userStr},
success: function(response){
//do whatever.
}
});
</script>
</head>
</html>
I changed it, but isset($_POST['user']) = false because my page is empty so why?
All this code is in test.php
You are not printing / visualizing the actual response of the ajax.
<?php
if (isset($_POST['user'])) {
$user_string = $_POST['user'];
var_dump($user_string);
$user_asarr = json_decode($user_string, true); // associative array
var_dump($user_asarr);
exit();
}
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
var user = {
'name': 'Wayne',
'country': 'Ireland',
'selected': new Array(1, 5, 9)
};
var userStr = JSON.stringify(user);
$.ajax({
url: 'test.php',
type: 'POST',
data: {user: userStr},
success: function(response){
console.log(response); // check out your console!
}
});
</script>

Condition not working and form submitting even though email already exists

I am trying to check email avaliablilty using AJAX and PHP, but I am facing the following two problems:
1) If I'm getting response true or false showing me "Email available" message, means if else condition is not working with response.
2) The form still submitting if email already exist.
Here is my code in view:
<script>
$(document).ready(function(){
$("#email").blur(function(){
var email = $("#email").val();
$.ajax(
{
type:"post",
url: "<?php echo base_url(); ?>index.php/Register/check_emails",
data:{ email:email},
success:function(response)
{
alert($.trim(response));
if($.trim(response) == "true")
{
alert("trueeeee");
$('#lgmsg').html('<span style="color:red;">Valid Email</span>');
}
else
{
alert("falseeee");
$('#lgmsg').html('<span style="color:green;">Email avaliable</span>');
}
}
});
});
});
</script>
Here is my controller function where I am getting response/result
public function check_emails()
{
$email = $this->input->post('email');
$record['data']=$this->Home->check_emailsd($email);
if($record['data']=="exist")
{
echo "true";
}
else
{
echo "false";
}
}

Ajax - problem with response inside a div

I am trying to show the value of ajax response inside a div.Ajax. Problem arise when I get the response. Response is showing in the div only for split second and hides automatically. I tried changing id to class but the error persist still. The alert displays properly when tested. What do I miss? I have the following code in my view file.
The div:
<div id="result"></div>
Ajax:
$(document).ready(function() {
$('#submit').click(function() {
$.ajax
({
type:'post',
cache: false,
url:'save.php',
data: $("#action").serialize(),
dataType: "html",
success: function(response) {
if(response == 'success')
{
alert('success');
}
if(response == 'empty')
{
alert('empty');
}
if(response == 'bad')
{
$("#result").html(response);
}
}
});
});
});
save.php:
<?php
$co= 'aaa';
if(!empty($_POST['tosave']))
{
if($_POST['tosave'] == $co)
{
$foo = fopen("plik.txt","a+");
flock($foo, 2);
fwrite($foo,$_POST['tosave']."\r\n");
flock($foo ,3);
fclose($foo);
echo "success";
exit();
} else {
echo 'bad';
exit;
}
} else {
echo "empty";
exit;
}
?>
This might be because you don't prevent the default behavior on the submit button which is to send the form's data to the specified link, maybe in your case the link is on the same page than your PHP leading to only see the message in the div for a split second before the page reloads itself.
So to prevent this default behavior, you have to catch the event and use the preventDefault() function like so :
$('#submit').click(function(event) { //we pass the event as an attribute
event.preventDefault(); //we prevent its default behavior so we can do our own stuff below
$.ajax
({
type:'post',
cache: false,
url:'save.php',
data: $("#action").serialize(),
dataType: "html",
success: function(response) {
if(response == 'success')
{
alert('success');
}
if(response == 'empty')
{
alert('empty');
}
if(response == 'bad')
{
$("#result").html(response);
}
}
});
});
});
First of all there is a mistake in your Ajax call code you have write your submit.click() submit function under document.ready().remove document.ready() because submit.click()will run when you submit the form. Second thing is you should add
event.preventDefault() to prevent the default behabiour of your submit form otherwise it will display for short time and then it will disappeared.See my code below it is working.
AJAX WITH HTML FORM:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form id="action">
<input type="text" name="tosave" value="bbb">
<button type="submit">clickme</button>>
</form>
<div id="result"></div>
</body>
</html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script type="text/javascript">
$("#action").click(function(event) {
event.preventDefault();//TO PREVENT THE DEFAULT BEHAVIOUR OF FORM
$.ajax
({
type:'post',
cache: false,
url:'https:save.php',
data: $("#action").serialize(),
dataType: "html",
success: function(response) {
if(response == 'success')
{
alert('success');
}
if(response == 'empty')
{
alert('empty');
}
if(response == 'bad')
{
$("#result").html(response);
}
}
});
});
</script>
PHP:
<?php
$co = 'aaa';
if (!empty($_POST['tosave'])) {
if ($_POST['tosave'] == $co) {
$foo = fopen("plik.txt", "a+");
flock($foo, 2);
fwrite($foo, $_POST['tosave'] . "\r\n");
flock($foo, 3);
fclose($foo);
echo "success";
exit();
} else {
echo 'bad';
exit;
}
} else {
echo "empty";
exit;
}
?>
**OUTPUT DISPLAY IN DIV: bad**

$.ajax POST issue

I have a big issue with $.ajax. I try to send a file name to the PHP part (in the same file), to help me stocking that name in a $_SESSION.
It worked perfectly yesterday, but not anymore. Is there a browser issue instead of code issue ?
Here is my code :
JS function
<script type="text/javascript" charset="utf-8">
function no_select(){
var images = $("#selected_images").text();
if(images != ''){
var tmpFile = $("#selected_images").text();
var arrayFile = tmpFile.split(",");
if (arrayFile.length > 2){
alert("Il ne faut selectionner qu'un seul badge !");
return false;
}
var value = $.ajax({
type: "GET",
dataType: "json",
url: "overview_list.php",
data: "selectedImage="+arrayFile[0],
async: false,
success: function()
{
return true;
}
});
return true;
} else if(images == ''){
alert("Il faut selectionner un badge");
return false;
}
}
</script>
PHP
<?php
if(!empty($_GET['selectedImage'])){
$let = $_GET['selectedImage'];
} else {
$let = "";
}
$_SESSION['selectedImage'] = $let;
?>
I have already check a lot of solutions around the web, but there was no solution for me.
It should be like
$.ajax({
type: "GET",
dataType: "json",
url: "overview_list.php",
data: {selectedImage:arrayFile[0]},
async: false,
success: function()
{
return true;
}
and makesure arrayFile[0] is getting value

save jquery flip state into a php session variable?

I am using this tutorial http://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_slide_toggle
Here is the code without the style.
<script type="text/javascript">
$(document).ready(function()
{
$(".flip").click(function()
{
var panel = "open";
$(".panel").slideToggle("slow");
});
});
</script>
How would I go about saving the state of this so if I refreshed the page it would remain open or closed. I imagine a php session would be the correct way, but how do I write that in the javascript?
In JS:
var readWirite='write'; //or 'read'
$.ajax({
type: "POST",
url: "myPhpFile.php",
data: "panel="+panel+"&readWrite="+readWrite;
success: function(msg){
if(msg == '1'){
alert('Horay panel saved!');
} else {
$('#panelId').html(msg); //Write saved panel back to html
}
}
});
In myPhpFile.php:
<?php
if(!isset($_SESSION)) session_start();
if(isset($_POST['readWrite']) && isset($_POST['panel'])){
if($_POST['readWrite'] == 'write'){
$result = '0';
if($_SESSION['panel'] = $_POST['panel']) $result = '1';
echo $result;
} else if($_POST['readWrite'] == 'read') {
echo $_SESSION['panel'];
}
}
?>

Categories