hi guys even i have success delete the message return false delete message("could not delete") besides success delete messege but query is wokrking
please tell me what i am doing wrong here is my codes \
delete.php
if(isset($_POST['delete_id']) && !empty($_POST['delete_id'])) {
$delete_id = mysql_real_escape_string($_POST['delete_id']);
$query=mysql_query("DELETE FROM color WHERE idColor='$delete_id'");
if($query != false) {
echo 'true';
}
mysql_close();
<script>
$(document).ready(function(){
$('.del_btn').click(function(){
if (confirm("Are you sure you want to delete this Color?"))
var del_id = $(this).attr('rel');
$.post('script/delete_color.php', {delete_id:del_id}, function(data) {
if(data == 'true') {
$('#'+del_id).remove();alert('Color has been deleted!');
}
else {
alert('Color could not delete!');
}
document.messages.submit();
return false; // This line added}
});
});
});
</script>
I didn't really understand your problem, but I'm going to take an educated guess...
You need to wrap your logic inside the "confirm" IF statement. Also, you had an extra "});".
$(document).ready(function(){
$('.del_btn').click(function() {
if (confirm("Are you sure you want to delete this Color?")) {
var del_id = $(this).attr('rel');
$.post('script/delete_color.php', {delete_id:del_id}, function(data) {
if(data == 'true') {
$('#'+del_id).remove();alert('Color has been deleted!');
} else {
alert('Color could not delete!');
}
document.messages.submit();
return false; // This line added
}
}
});
});
Related
This should be simple, but doesn't work
Essentially I want to get a return result and reload my page if the answer is 'yes'
Here is the
timer.php
<?php
$filename = 'timer.tmr';
if (file_exists($filename)) {
unlink($filename);
echo 'yes';
} else {
echo 'no';
}
?>
and here is the script in my main file
var myVar = setInterval(function () {myTimer()}, 5000);
function myTimer() {
sendto = "timer.php";
$.get(sendto, function(data, status) {
if (data === 'yes') {
location.reload();
}
});
}
Basically in the timer.php
If the file exists I delete it and return a 'yes'
But the jquery doesn't do the reload function, if I check the data with an alert(data), it returns a 'yes' So I just don't understand the problem.
If you are getting into the if condition, then you should try window.location.href = window.location.href; instead of location.reload();
and If you're not getting into the if condition, change condition with if ($.trim(data) === "yes")
thanks to Shaunak Shukla (again thank you)
here is the final solution
var myVar = setInterval(function () {myTimer()}, 10000);
function myTimer() {
sendto = "timer.php";
$.get(sendto, function(data, status) {
if ($.trim(data) === "yes"){
location.reload();
}
});
}
it needed the trim(data) must have had something in it..
Use json_encode in the echos and improve your checking:
<?php
$filename = 'timer.tmr';
if (file_exists($filename)) {
unlink($filename);
if (!file_exists($filename))
echo json_encode('yes');
else
echo json_encode('no');
} else {
echo json_encode('no');
}
?>
And change the reload to a more direct scope:
var myVar = setInterval(function () {myTimer()}, 5000);
function myTimer() {
sendto = "timer.php";
$.get(sendto, function(data, status) {
if (data === 'yes'){
window.location.reload();
}
});
}
Let complete ajax request then try to reload :
setTimeout(function(){// wait for 5 secs(2)
location.reload(); // then reload the page.(3)
}, 5000);
I would Suggest, please check the Type casting (data type). I find, you have used === , it will check string to string or int to int.
What I want to do
When writing in the text field, I want the <div class="result"> to be filled with what PHP is echoing.
But it doesn't work!
Jquery
$(document).ready(function() {
var search = $("#search");
if (search.val() !== '') {
search.keyup(function() {
$.post("index.php", { search : search.val()}, function(data) {
$(".result").html(data);
});
});
}
});
php
if (isset($_POST['search'])) {
echo 'hello';
}
html
<input type="text" name="search" id="search"/>
<br />
<div class="result"></div>
Problem
When filling the input, nothing happens, and it meant to POST the entered data on keyup (When entering a new character/or deleting.
What is stopping it from working? I am new to jQuery .
Thanks.
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
This is wrong.
if (search.val() !== '') {
The above line should be,
if (search.val() != '') {
EDIT:
Then wrap the if condition inside the keyup function.
$(document).ready(function() {
var search = $("#search");
search.keyup(function() {
if (search.val() != '') {
$.post("getInputs.php", { search : search.val()}, function(data) {
$(".result").html(data);
});
}
});
});
When I run into situations like this, I just start breaking the problem in half to see where its failing. Here are a couple things I would try.
First, in your jQuery, add some output to the console:
if (search.val() !== '') {
console.log("I am not empty so I should go to index.php");
search.keyup(function() {
$.post("index.php", { search : search.val()}, function(data) {
$(".result").html(data);
});
});
}
else
{
console.log("search val was empty");
}
Of course you could always check the browsers network profiler to see if it made a POST to that resource. This will tell you if the problem is in your search.val test.
Then, if you want to debug the PHP side, you could remove the isset test and just always return "hello". That will tell you if its an issue with your POST variables or checks.
Finally, you could output the data result to be sure something is coming back at all. This will remove any issues with $(".result").html() being the problem:
$.post("index.php", { search : search.val()}, function(data) {
console.log(data);
$(".result").html(data);
});
If none of these work, maybe you could just switch around the way you bind to keyup in the first place:
$(document).ready(function() {
$("#search").keyup(function() {
if ($(this).val() !== '') {
$.post("index.php", { search : $(this).val()}, function(data) {
$(".result").html(data);
});
});
}
});
$(document).ready(function() {
var search = $("#search");
});
This fire only at document ready but not on keyup, means in var $("#search").val() will be blank.
Change your code to capture inpute value on every key-up stroke.
$(document).ready(function() {
search.keyup(function() {
var value = $("#search").val();
if(value!="")
{
$.post("index.php", { search : value}, function(data) {
$(".result").html(data);
});
}
});
});
Your logic is incorrect. You are only setting the keyup event handler if your #search has text in it. Unfortunately when that script runs on document ready, there is NO value in #search so your keyup handler never gets set, which is why it never fires.
I rewrote some of your logic and was able to get it to work. One being the way your checking to ensure you have a value. Instead of string comparing I am checking the length. Also, instead of binding the event to the field, I bind the event on the document and target the field. Try it:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<input type="text" name="search" id="search"/>
<br />
<div class="result"></div>
<script>
$(document).ready(function() {
$(document).on('keyup', 'input#search', function() {
if($(this).val().length > 0) {
$.post('index.php', {"search":$(this).val()}, function(data) {
$('div.result').html(data);
});
}
});
});
</script>
// when the html is loaded
$(document).ready(function() {
// find an element with the id 'search'
var search = $("#search");
// if this element's value is NOT an empty string -- oh look, it is!
if (search.val() !== '') {
// well, going to skip all this here then
search.keyup(function() { // don't care
$.post("index.php", { search : search.val()}, function(data) { // don't care
$(".result").html(data); // don't care
});
});
}
// YAAAAY! All done!
});
Actually nothing is wrong in your code. I have tried your code itself. Only issue was that you have called keyup function conditionally. Your Javascript code should be like below:
$(document).ready(function() {
var search = $("#search");
search.keyup(function() {
if (search.val() != '') {
$.post("index.php", { search : search.val()}, function(data) {
$(".result").html(data);
});
}
});
});
Here, condition should be inside the keyup function.
I have these 4 pages and works fine, but now i need to send the "word" typed in the input field at (index.php) to another page (pag2.php) at the same time it sends to (pag1.php) using this javascript code at (script.php).
index.php
<form method="post">
<input type="text" name="word" id="word" onkeyup="getSugest(this.value);">
</form>
<div class='search'><img ..... searching.gif></div>
<div id="sugest">
<div id="resultIndex"></div>
<div id="buttonsIndex">
<ul>
<?
for($i=1; $i<=page; $i++;){
echo "<li id='".$i."'>".$i."</li>";
}
?>
<ul>
</div>
</div>
script.js
function getSugest(value){
if(value != ""){
if (callPage1(value) || callPage2(value)) {
var data1 = callPage1(value);
var data2 = callPage2(value);
//var combinedData = combine as data1 and data2 as you want
$("#sugest").html(combinedData);
} else {
$("#sugest").html("Theres nothing in DB!");
}
}
}
function callPage1(value) {
$.post("pag1.php",{word:value},function(data){
if(data != ""){
return data;
}
else{
false;
}
});
}
function callPage2(value) {
$.post("pag2.php",{word:value},function(data){
if(data != ""){
return data;
}
else{
false;
}
});
}
$(document).ready(function(){
function showLoader(){
$('.search').fadeIn(200);
}
function hideLoader(){
$('.search').fadeOut(200);
};
$("#buttonIndex li").click(function(){
showLoader();
$("#buttonIndex li").css({'background-color' : ''});
$(this).css({'background-color' : '#D8543A'});
$("#resultIndex").load("pag1.php?page=" + this.id, hideLoader);
return false;
});
$("#buttonCar li").click(function(){
showLoader();
$("#buttonCar li").css({'background-color' : ''});
$(this).css({'background-color' : '#D8543A'});
$("#resultCar").load("pag2.php?page=" + this.id, hideLoader);
return false;
});
$("#1").css({'background-color' : '#D8543A'});
showLoader();
$("#resultIndex").load("pag1.php?page=1", hideLoader);
$("#resultCar").load("pag2.php?page=1", hideLoader);
});
pag1.php
$word = mysql_real_escape_string(addslashes($_POST['word']));
echo "Word is: ".$word;
//here is the php and mysql querys to return the result
pag2.php
$word = mysql_real_escape_string(addslashes($_POST['word']));
echo "Word is: ".$word;
//here is the php and mysql querys to return the result
I appreciate any help.
Don't use inline functions, use jQuery's native methods instead:
$(function(){
$('#word').keyup(function(){
if(this.value) {
$.post("pag1.php", { word: value }, function(data) {
if(data){
$("#suggest").html(data);
} else {
$("#suggest").html("Theres nothing in DB!");
}
});
}
});
});
There is an example scenario here
You can implement a method for each call, and after calling all of them, you can combine their results
You can also have a look at my sample code for reproduce your scenario (modify it according to your needs)
function getSugest(value){
if(value != ""){
if (callPage1(value) || callPage2(value)) {
var data1 = callPage1(value);
var data2 = callPage2(value);
//var combinedData = combine as data1 and data2 as you want
$("#sugest").html(combinedData);
} else {
$("#sugest").html("Theres nothing in DB!");
}
}
}
function callPage1(value) {
$.post("pag1.php",{word:value},function(data){
if(data != ""){
return data;
}
else{
false;
}
});
}
function callPage2(value) {
$.post("pag2.php",{word:value},function(data){
if(data != ""){
return data;
}
else{
false;
}
});
}
I am trying to check some checkboxes based on it value.
In my code, I can update the database using this ckbxs, but when I try load the updated date, the status is showed as uncheck.
So, I am trying to read the html, after create the form, to check the values and then check the ckbx.
What can I do here to fix it?
Thanks in advance
$(document).ready(function(){
$('input[type=checkbox]').each(function () {
if (this.value == 1) {
(function(){this.attr('checked', true);})
}
} )
})
You need to enclose the thiss in a $( ) and change value to val().
$(document).ready(function(){
$('input[type=checkbox]').each(function () {
if ($(this).val() == 1) {
$(this).attr('checked', true);
}
});
});
$(document).ready(function () {
$('input[type="checkbox"][value="1"]').each(function () {
$(this).attr('checked', true);
});
});
Thanks Willian, now I am using a onChange and I got what I need...
function changerCB(idCheck){
$(document).ready(function() {
$.post('ajaxUpdate.php', {
id: document.getElementById(idCheck).id,
value: document.getElementById(idCheck).checked?1:0
}, function(data){
$('#display').html(data.returnValue)
if(data.success) {
} else {
}
}, 'json');
return false;
});
}
I have a rather confusing problem.
I have a php file (http://example.com/delete.php)
<?php
session_start();
$user_id = $_SESSION['user_id'];
$logged_in_user = $_SESSION['username'];
require_once('../classes/config.php');
require_once('../classes/post.php');
$post = new Post(NULL,$_POST['short']);
#print_r($post);
try {
if ($post->user_id == $user_id) {
$pdo = new PDOConfig();
$sql = "DELETE FROM posts WHERE id=:id";
$q = $pdo->prepare($sql);
$q->execute(array(':id'=>$post->id));
$pdo = NULL;
}
else {throw new Exception('false');}
}
catch (Exception $e) {
echo 'false';
}
?>
and I'm trying to get this jquery to post data to it, and thus delete the data.
$('.post_delete').bind('click', function(event) {
var num = $(this).data('short');
var conf = confirm("Delete This post? (" + num + ")");
if (conf == true) {
var invalid = false;
$.post("http://example.com/delete.php", {short: num},
function(data){
if (data == 'false') {
alert('Deleting Failed!');
invalid = true;
}
});
if (invalid == false) {
alert("post Has Been Deleted!");
}
else {
event.preventDefault();
return false;
}
}
else {
event.preventDefault();
return false;
}
});
and when I do that, it returns "Post Has Been Deleted!" but does not delete the post.
Confused by that, I made a form to test the php.
<form action="http://example.com/delete.php" method="POST">
<input type="hidden" value="8" name="short"/>
<input type="submit" name="submit" value="submit"/>
</form>
which works beautifully. Very odd.
I have code almost identical for deleting of a comment, and that works great in the javascript.
Any ideas? Beats me.
Thanks in advance,
Will
EDIT:
this works... but doesn't follow the href at the end, which is the desired effect. Odd.
$('.post_delete').bind('click', function(event) {
var num = $(this).data('short');
var conf = confirm("Delete This Post? (http://lala.in/" + num + ")");
if (conf == true) {
var invalid = false;
$.post("http://example.com/delete/post.php", {short: num},
function(data){
if (data == 'false') {
alert('Deleting Failed!');
invalid = true;
}
});
if (invalid == false) {
alert("Post Has Been Deleted!");
******************************************
event.preventDefault();
return false;
******************************************
}
else {
event.preventDefault();
return false;
}
}
else {
event.preventDefault();
return false;
}
});
If your PHP script delete the post, it doesn't return anything.
My bad, it's not answering the real question, but still is a mistake ;)
Actually, it seems that PHP session and AJAX doesn't quite work well together sometimes.
It means that if ($post->user_id == $user_id) will never validate, hence the non-deleting problem.
2 ways to see this :
Log $user_id and see if it's not null
Try to send the $_SESSION['user_id'] with your ajax post and check with it. But not in production, for security reason.
1-
Your PHP should return something in every case (at least, when you're looking for a bug like your actual case).
<?php
[...]
try {
if ($post->user_id == $user_id) {
[...]
echo 'true';
}
else {throw new Exception('false');}
}
catch (Exception $e) {
echo 'false';
}
?>
2-
jQuery is nice to use for AJAX for many reasons. For example, it handles many browsers and make checks for you but moreover, you can handle success and error in the same .ajax() / .post() / .get() function \o/
$('.post_delete').bind('click', function(event) {
var num = $(this).data('short'); // If that's where your data is... Fair enough.
if (confirm("Delete This Post? (http://lala.in/" + num + ")")) {
$.post("delete/post.php", {short: num}, // Relative is nice :D
function(data){
if (data == 'false') {
alert('Deleting Failed!');
}else{
alert("Post Has Been Deleted!");
// Your redirection here ?
}
});
}
});
3-
If you need to send data from a form to a script and then do a redirection, I won't recommand AJAX which is usually use not to leave the page !
Therefore, you should do what's in your comment, a form to a PHP script that will apparently delete something and then do a redirection.
In your code I don't see num defined anywhere...and invalid isn't set when you think it is, so you're not passing that 8 value back and you're getting the wrong message, either you need this:
$.post("http://example.com/delete.php", {short: $("input[name=short]").val()},
Or easier, just .serialize() the <form>, which works for any future input type elements as well:
$.post("http://example.com/delete.php", $("form").serialize(),
I'm not sure where your code is being called, if for example it was the <form> .submit() handler, it'd look like this:
$("form").submit(function() {
$.post("http://example.com/delete.php", $(this).serialize(), function(data){
if (data == 'false') {
alert('Deleting Failed!');
} else {
alert("Post Has Been Deleted!");
}
});
Note that you need to check inside the callback, since invalid won't be set to true until the server comes back with data the way you currently have it, because it's an asynchronous call.