JavaScript button Prompt Box Cancel?

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 clicked, the form is still being processed via getmessage.php . Any way for the Cancel button to do nothing?

EDIT:

This is what 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>"; } ?> 

PHP script related 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."; } ?> 
+7
source share
6 answers

In the case of Cancel the query result is null and null != '' (According to ECMA-262, section 11.9.3).

So, add an extra explicit check for the null inequality:

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

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

 if(message) { 

This means: if the message is true (i.e. not null or an empty string, among other falsehoods), then enter the if clause.

+19
source

Are you using Safari by accident? I found that Safari seems to return an empty string instead of zero when the user clicks the Cancel button.

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

+3
source

Yes, my suggested comment really works

 var message = prompt("Message:",""); if(message){ alert("Not working!"); } else { alert("Working!"); } 

Jsfiddle

+2
source
 var message = prompt("Message:",""); if(message){ alert("Message accepted, now i can process my php or script and blablabla!"); } else { alert("Cancel Press or Empty Message, do nothing!"); } 
+1
source
 var message = prompt('type any...', ''); if(message+'.' == 'null.') { alert("you've canceled"); } else { alert("type ok"); } 
0
source
  $.messager.prompt('Save To File', 'FileName:', function(e){ if (e.response!='undefined'){ if (r!="") { alert('Your FileName is:' + r); } else { $.messager.alert('Err...','FileName cannot empty!!!'); } } }); 
0
source

All Articles