Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Prompt Box Cancel Button?

I have a JavaScript function as follows:

function popup(username) {
var req = createAjaxObject();
var message = prompt("Message:","");
if(message != ""){
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
            alert(req.responseText);
        }
    }
    req.open('POST','getmessage.php',true);
    req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    req.send("username=" + username +"&message="+message);
} else {
    alert("Please enter a message");
}
}

When the Cancel button is hit, the form is still processed through getmessage.php. Any way to have the Cancel button do nothing?

EDIT:

Here is the way this function is called:

                <?php
        mysqlLogin();
        $username = $_COOKIE['sqlusername'];
        $sql = mysql_query("SELECT username FROM `users` WHERE username!='$username'");
        if(mysql_num_rows($sql) != 0) {
            echo "<table class='usertable' align='center'>";
            while($row = mysql_fetch_array($sql)){
                $username = $row['username'];
                echo "<tr><td><center>" . $row['username'] . "</center></td><td>&nbsp;&nbsp;<a href='#' onClick=\"popup('$username');\">Send Message</a></td></tr>";
            }
            echo "</table>";
        } else {
            echo "<center>No users found!</center>";
        }

    ?>

The PHP script its linked to:

<?php
$id = rand(1,1500);
$poster = $_POST['username'];
$message = $_POST['message'];
$to = $_COOKIE['sqlusername'];

require('functions.php');
mysqlLogin();

$sql = mysql_query("INSERT INTO `messages` VALUES ('$id','$message','$to','$poster','')");
if($sql){
    echo "Message sent!";
} else {
    echo "Woops! Something went wrong.";
}
?>
like image 526
Jason Avatar asked Aug 05 '11 21:08

Jason


People also ask

What does clicking on Cancel button in the prompt dialog box?

If the user clicks the Cancel button, prompt( ) returns null . If the user clicks the Clear button, prompt( ) erases any current text in the input field. If the user clicks the OK button, prompt( ) returns the value currently displayed in the input field.

How to show alert with OK and Cancel button in JavaScript?

The confirm() method displays a dialog box with a message, an OK button, and a Cancel button. The confirm() method returns true if the user clicked "OK", otherwise false .

How do I use multiple prompts in JavaScript?

var a = prompt("A : ", ""); var b = prompt("B : ", ""); alert(a + "\n" + b); javascript.


2 Answers

In the case of Cancel, the prompt result is null, and null != '' (as per ECMA-262 Section 11.9.3).

So, add an extra explicit check for null inequality:

if(message != "" && message !== null) {

However, since the message is either some string or null and you only want to pass when it's a string with length > 0, you can also do:

if(message) {

This means: if message is truthy (i.e. not null or an empty string, amongst other falsy values), then enter the if clause.

like image 164
pimvdb Avatar answered Sep 27 '22 18:09

pimvdb


Are you using Safari by any chance? I have found that Safari seems to be returning empty string instead of null when the user clicks Cancel.

See here: Safari 5.1 prompt() function and cancel.

like image 25
user3734688 Avatar answered Sep 27 '22 17:09

user3734688