0

So I have yet another problem,

I am trying to get an AJAX script to work, but upon click the page will reload but fail to UPDATE the database field.

The code im using I have working for other similar scripts on the site but for some reason this one, using the same code doesnt work, the code used follows below;

HTML code to send the call to AJAX:

<input onClick="read('<? echo $id; ?>')" id="read" name="read" type="checkbox" value="1" style="position:relative; top:2px; width: auto">

The code to confirm user selection and send onto a form handling file:

function read(ID) {
    if(confirm('Are you sure you have read this carefully, you will not be alerted again "' + ID + '" ?')) {
        $.get('http://<? echo ROOT . ADMIN . INCLUDES; ?>formHandling.php', { read: ID }, function(data) {
            window.location.href = 'http://<? echo $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].""; ?>';
        });
    }
    return false;
}

Lastly the code to handle the SQL query:

if (isset($_GET['read'])) {

    // Pass the GET data and associate them to variables
    $read = trim($_GET['read']);

    $query  = "UPDATE cms_motd SET read='$read' WHERE id='1'";
    $result = mysql_query($query)or die("Database query died: " . mysql_error());

    unset($_GET['readConfirm']);
}

Thanks in advance for all that help.

Regards,

Dan.

2 Answers 2

1

Don't you mean:

$query  = "UPDATE cms_motd SET read='1' WHERE id='$read'";

Instead of:

$query  = "UPDATE cms_motd SET read='$read' WHERE id='1'";

Edit:

I don't know if it is a copy&past error:

$result = mysql_query($query);or die("Database query died: " . mysql_error());

Needs to be:

$result = mysql_query($query) or die("Database query died: " . mysql_error());
Sign up to request clarification or add additional context in comments.

10 Comments

Sadly no, $read is the id of the user that has confirmed reading the message and that value goes into the database as a relational entry to show that user has read the message for future usage.
You are sure that the record exists in the database then? When u run the query from PMA it affects rows?
yeah the table cms_motd exsists as does the "id" field and the "read" field, "read" is set to an int(2) field and is currently empty as of writing. Also yes the ";" you found was a typo, removed but didnt fix the problem :(
Is there a record in the table cms_motd where ID=1? I don't understand why there is chosen for a UPDATE, I would design the system different. When someone reads the message, then you will INSERT instead inserting at first, and update later.
Yes there is a row in the cms_motd table where id = 1, to aim for efficiency i.e. not creating a new table or a new field inside a heavy populated table. So cms_motd is the message itself and one of its fields is "read" and that will list all the relative id's of the users that have confirmed they have read the new message.
|
0

I have some challenges with the fact that you do the query but don't give your code any feedback to continue. For example, what if the query fails? Do you just press on?

Here's how I handle these sort of Ajax transactions (yes, longhand!)

$('#read').click(function() {
  $("#div").dialog({        //Shows dialog
    height: 250,
    width: 450,
    modal: true,
buttons: {
   "Cancel": function() {
    $( this ).dialog( "close" );
   },
   "Save": function() {
        $.ajax({
        url: "url.php",                   //
        timeout: 30000,
        type: "POST",
        data: $('#form').serialize(),
        dataType: 'json',
        error: function(XMLHttpRequest, textStatus, errorThrown)  {
            alert("An error has occurred making the request: " + errorThrown)
        },
        success: function(data){                                                        
               $('#updatediv).html(data.stuff);
            src="web/imgs/icons/24deleteB.png"></td>';

        }

        $( this ).dialog( "close" );
   }


    }


   });
});

Now, the URL.php will do the query and return a json_encoded string back to the AJAX that will then be able to know if the transaction was succesful via the success/error functions. You could do additional conditioning on the success case to ensure that something was saved a particular way or that a result matched a case that you wanted it to before doing something. On success, I show just a simple .html inner html type action, but you could do any quantity or variety of things such as show/hide, inner html, etc. The choice is yours. Also, note that I use Jquery UI dialogs instead of system dialogs, so you'd need Jquery UI to make it look pretty if you used this verbatim. Finally, rather than onclick note that I'm using the .click functionality that Jquery provides, which is just a hair cleaner.

2 Comments

Thank you for your help but I was not looking for a re working of the code I have, I was after the solution to fix the code I had.
@Daniel, regardless, error handling is a vital part of what we do, especially when working with Asynchronous connections that can and do fail. JQuery makes it so easy, it's criminal not to use it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.