title: JavaScript Prompt Box Cancel Button?
body: <p>I have a JavaScript function as follows:</p>
  
  <pre><code>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 +"&amp;message="+message);
  } else {
      alert("Please enter a message");
  }
  }
  </code></pre>
  
  <p>When the <code>Cancel</code> button is hit, the form is still processed through <code>getmessage.php</code>. Any way to have the <code>Cancel</code> button do nothing?</p>
  
  <p><b>EDIT:</b></p>
  
  <p>Here is the way this function is called:</p>
  
  <pre><code>                &lt;?php
          mysqlLogin();
          $username = $_COOKIE['sqlusername'];
          $sql = mysql_query("SELECT username FROM `users` WHERE username!='$username'");
          if(mysql_num_rows($sql) != 0) {
              echo "&lt;table class='usertable' align='center'&gt;";
              while($row = mysql_fetch_array($sql)){
                  $username = $row['username'];
                  echo "&lt;tr&gt;&lt;td&gt;&lt;center&gt;" . $row['username'] . "&lt;/center&gt;&lt;/td&gt;&lt;td&gt;&amp;nbsp;&amp;nbsp;&lt;a href='#' onClick=\"popup('$username');\"&gt;Send Message&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;";
              }
              echo "&lt;/table&gt;";
          } else {
              echo "&lt;center&gt;No users found!&lt;/center&gt;";
          }
  
      ?&gt;
  </code></pre>
  
  <p>The <code>PHP</code> script its linked to:</p>
  
  <pre><code>&lt;?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.";
  }
  ?&gt;
  </code></pre>