Jump to content

I need to refresh the page before AJAX works


etsted
 Share

Recommended Posts

I have created a button that is supposed to delete all of your messages inside the DB

if(!$numrows >= 1) {            $error = "You have no messages in your inbox<br>";        } else {            $deleteAll = "            <button onmousedown='askFirst()'>Delete all messages</button>            ";            while ( $row=mysqli_fetch_array($query) )            {                $id = $row['id']; $reciever = $row['reciever']; $sender = $row['sender']; $date_sent = $row['date_sent'];                $message = $row['message']; $subject = $row['subject'];                                                                $msgDeleteButton = '';                            // gives the author of the comment, or the page owner the ability to delete the reply                if($sender == $log_username){                    $msgDeleteButton = '<span><a href="#" onclick="return false;" onmousedown="deleteMsg(''.$id.'','sent_'.$id.'');" title="DELETE THIS MESSAGE">remove</a></span>';                }                                // holds all of the replies                $status_replies .= '<div id="sent_'.$id.'" class="sent_message"><div><b>Sent by '.$sender.' '.$date_sent.':</b> '.$msgDeleteButton.'<br />'.$message.'</div></div>';                    }        }

The function the "button" calls "askFirst()" is here:

function askFirst(){            $answer = alert("Are you sure you want to delete all of your messages?");            if ($answer != true) {                return false;            }                var ajax = ajaxObj("POST", "php_parsers/status_message.php");                            ajax.onreadystatechange = function() {                    if(ajaxReturn(ajax) == true) {                        if(ajax.responseText == "delete_ok"){                            _(success).innerHTML = 'All of you messages has now been deleted';                        } else {                            alert(ajax.responseText);                        }                    }                }                ajax.send("action=delete_all_msg");        }

They are on the same page: sentMsg.php

also in my editor "komdo", there is a green underline under "function askFirst()" says that askFirst() doesnt always return a value. I dont think that it matters as i tried to comment out

 

if($answer != true){

return false;

}

Then suddently the green underline disapeared.

 

The AJAX makes a call to php_parsers/system_message.php. Which is here:

if (isset($_POST['action']) && $_POST['action'] == "delete_all_msg") {    $sql = "UPDATE conversation SET deleted='1' WHERE sender='$log_username'";    $query = mysqli_query($con, $sql);    echo "delete_ok";}
Link to comment
Share on other sites

Also, you should use var answer (not $answer, I mean yes its valid but people will confuse it as PHP), and use prompt(), not alert() as alert won't return anything to the var answer. The reason askfirst was getting that warning is exactly due to what it said. the things it could return weren't the same datatype. when the answer wasn't true you'd return false... but if it was true, you'd continue on with the function normally and finish without returning anything, by which the function will infer to mean it should implictly return null. To remove that warning you need to make sure the function will return the same datatype in ever scenario in the function. You did this by commenting out the first return, but it should also be possible to remove the warning by adding a return true at the very end of the function.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...