Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8840833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:33:28+00:00 2026-06-14T10:33:28+00:00

I have the following code which sends an AJAX POST to a php file

  • 0

I have the following code which sends an AJAX POST to a php file on my server to add a row into a MySQL table. This function gets run when a user marks an item as their favorite:

$.ajax({
    type: 'POST',
    data: 'school='+school+'&token='+token,
    url: 'http://www.codekraken.com/testing/snowday/database9.php',
    success: function(data){
    console.log(data);
    },
    error: function(){
    console.log(data);
    }
 });

I also have a similar function that runs when they delete the favorite:

data: 'school=&token='+token,

This works, but not consistently. The PHP code is below:

<?php
$server = "localhost";
$username = "";
$password = "";
$database = "";
$con = mysql_connect($server, $username, $password) or die ("Could not connect: " . mysql_error());

mysql_select_db($database, $con);

$token = $_POST["token"];
$school = $_POST["school"];

if (strlen($token) == 0){
    die('Incorrect token');
}

if (strlen($school) == 0){
    die('Invalid school name');
}

$sql = "INSERT INTO Snow (Token, School, Skip) ";
$sql .= "VALUES ('$token', '$school', '0') ON DUPLICATE KEY UPDATE School='$school'";

if (!mysql_query($sql, $con)) {
    die('Error: ' . mysql_error());
} else {
    echo "$token, $school = Success";
}

mysql_close($con);
?>

What I mean by it not working consistently is that if I mark one item as the favorite, it will create a row. If I unmark it, it will change $school to nothing. If I then mark a different item, it will change to that item. IF, however, I try marking the first item as a favorite, it won’t update, but will retain the last change.

So, it seems that it’s only updating the row each time something new happens, but not if the value has already been present even though it’s not CURRENTLY present.

NOTE: Column Token is indexed as a UNIQUE PRIMARY

The JS code that pertains to var school:

function setFavorite() {
    var threshold = {
        x: 30,
        y: 10
    };
    var originalCoord = {
        x: 0,
        y: 0
    };
    var finalCoord = {
        x: 0,
        y: 0
    };

    function touchMove() {
        finalCoord.x = event.targetTouches[0].pageX;
        changeX = originalCoord.x - finalCoord.x;
        var changeY = originalCoord.y - finalCoord.y;
        if (changeY < threshold.y && changeY > (threshold.y * -1)) {
            changeX = originalCoord.x - finalCoord.x;
            if (changeX > threshold.x) {
                window.removeEventListener('touchmove', touchMove, false);
                $(document).off("touchmove", ".row");
                if ($(event.target).attr("class") === "row-inside") {
                    var element = $(event.target);
                }
                if ($(event.target).attr("class") === "row-l") {
                    var element = $(event.target).parent();
                }
                if ($(event.target).attr("class") === "row-r") {
                    var element = $(event.target).parent();
                }
                var text = $(element).find(".row-l").text();

                var token = window.deviceToken;
                var school = $(element).find(".row-l").text();

                var favstatus = $(element).find(".row-r").text();
                var thisStar = $(element).parent().find(".star-inside");
                $(element).css("margin-left", "-75px");
                if ($(thisStar).hasClass("favorite")) {
                    $.ajax({
                        type: 'POST',
                        data: 'school=&token=' + token,
                        url: 'http://www.codekraken.com/testing/snowday/database10.php',
                        success: function (data) {
                            console.log(data);
                        },
                        error: function () {
                            console.log(data);
                        }
                    });
                    $(".clear span").text("");
                    $(thisStar).removeClass("favorite");
                    localStorage.removeItem("favorite");
                    localStorage.removeItem("favorite-status");
                } else {
                    $.ajax({
                        type: 'POST',
                        data: 'school=' + school + '&token=' + token,
                        url: 'http://www.codekraken.com/testing/snowday/database10.php',
                        success: function (data) {
                            console.log(data);
                        },
                        error: function () {
                            console.log(data);
                        }
                    });
                    $(".clear span").text("\"" + text + "\"");
                    localStorage.setItem("favorite", text);
                    localStorage.setItem("favorite-status", favstatus);
                    $(".star-inside").not(thisStar).removeClass("favorite");
                    $(thisStar).addClass("favorite");
                }
                setTimeout(function () {
                    $(element).css("margin-left", "0px");
                }, 500);
                setTimeout(function () {
                    $(document).on("touchmove", ".row", function () {
                        touchMove();
                    });
                }, 800);
            }
        }
    }

    function touchStart() {
        originalCoord.x = event.targetTouches[0].pageX;
        finalCoord.x = originalCoord.x;
    }
    $(document).on("touchmove", ".row", function () {
        touchMove();
    });
    $(document).on("touchstart", ".row", function () {
        touchStart();
    });
}

MySQL Output:

CREATE TABLE `Snow` (
 `Token` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
 `School` text COLLATE utf8_unicode_ci NOT NULL,
 `Skip` int(1) NOT NULL,
 PRIMARY KEY (`Token`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicod
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T10:33:31+00:00Added an answer on June 14, 2026 at 10:33 am

    I figured it out.

    The problem seemed to be with Phonegap caching the AJAX request. I then thought if I added cache: false, to the AJAX request, it would solve it, but once again, no dice.

    I then googled around a bit, and found the piece of code that made it work:

    beforeSend: function(xhr) { xhr.setRequestHeader("Cache-Control", "no-cache"); xhr.setRequestHeader("pragma", "no-cache"); },
    

    You put that within your AJAX request, so it would look something like:

    $.ajax({
        cache: false,
        beforeSend: function (xhr) {
            xhr.setRequestHeader("Cache-Control", "no-cache");
            xhr.setRequestHeader("pragma", "no-cache");
        },
        type: 'POST',
        ...
    });
    

    I’m not entirely sure if it can work without also setting cache: false, but I don’t feel like rocking the boat. I also specified within my PHP code these headers:

    header("Expires: Tue, 01 Jul 2001 06:00:00 GMT");
    header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
    header("Cache-Control: no-store, no-cache, must-revalidate");
    header("Cache-Control: post-check=0, pre-check=0", false);
    header("Pragma: no-cache");
    

    Turns out the immediate above is not nescesary, but I’ll leave it here in case it helps someone else.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code which works great:- $('.ajax a').click(function() { getpageajax(this.href); return false;
I have the following code, which sends emails but it is embedding one email
I have following code which works for radio buttons but need to be changed
I want to know is below code correct ? I have following code which
I have the following code which definitely returns a proper data result if I
I have the following code which is working, I was wondering if this can
I have the following code which is used to upload large files (~6MB) to
i have the following code which switches some fullscreen-background-images (fadeOut, fadeIn). setInterval(function() { var
I have the following code which is fine if I give invalid parameters (though,
I have the following code which will generate two pdf files containing plots to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.