title: WP Show wp die error as an alert
body: <p>I have the following function in a project im working on and everything up to now works well apart from im stuck with showing error message in alert popup when the function die. </p>
  
  <p>This is Ajax Script</p>
  
  <pre><code>    jQuery(window).load(function() {
          action = jQuery.QueryString["action"];
          collection_id = jQuery.QueryString["cc"];
          current_userid = jQuery.QueryString["cu"];
          user_name = jQuery.QueryString["su"];
          collection_name = jQuery.QueryString["cn"];
          cbcolor = jQuery.QueryString["ccol"];
          permission = jQuery.QueryString["permission"];
  
          if(action == 'sharecookbook' &amp;&amp; jQuery('body').hasClass('logged-in')) {
              new jQuery.flavr({
                  content     : 'Do you really want to add this collection?',
                  dialog      : 'confirm',
                  onConfirm   : function(){
                      jQuery.ajax({
                          url: wpb_ajax_url,
                          data: 'action=wpb_sharecollection&amp;collection_id='+collection_id+'&amp;current_userid='+current_userid+'&amp;user_name='+user_name+'&amp;collection_name='+collection_name+'&amp;cbcolor='+cbcolor+'&amp;permission='+permission+'&amp;nonce='+ajax_object.nonce,
                          dataType: 'JSON',
                          type: 'POST',
                          success:function(data){
                              window.location = "/";  
                          }
                      });
                  },
              });
          }
      });
  </code></pre>
  
  <p>Then the following is the ajax php funtion</p>
  
  <pre><code>    /* share new collection */
      add_action('wp_ajax_nopriv_wpb_sharecollection', 'wpb_sharecollection');
      add_action('wp_ajax_wpb_sharecollection', 'wpb_sharecollection');
      function wpb_sharecollection(){
          global $wpb;
          $output = '';
          extract($_POST);
  
          check_ajax_referer( 'ajax_form_nonce', 'nonce' );
  
          $wpb-&gt;share_collection( $current_userid,$collection_id,$collection_name,$cbcolor,$user_name,$permission );
  
          wp_send_json ( $output );
      }
  </code></pre>
  
  <p>And the following is the share collection function defined in above php funtion</p>
  
  <pre><code>/* Share collection */
  function share_collection($uid,$id,$name,$cbcolor,$user,$permission) {
  
      $cbcolor = str_replace(" ", "#", $cbcolor);
      $type = 'shared';
  
      $user_id = get_user_by( 'email', $user );
      $collections = $this-&gt;get_collections($user_id-&gt;ID);
      $collectionsdata = $this-&gt;get_collectionsdata($user_id-&gt;ID);
      $collectionsdataoriginal = $this-&gt;get_collectionsdata($uid);
      $collectionsdatashared = $this-&gt;get_collectionsdatashared($uid);
  
      foreach($collectionsdata as $j =&gt; $ajj) {
          if ( isset($ajj['parent']) &amp;&amp; $ajj['parent'] == $id &amp;&amp; isset($ajj['owner']) &amp;&amp; $ajj['owner'] == $uid ) {
             wp_die( 'This is an error', 'Error' );                   
          }
      }
      $collections[] = array();
      $collectionsdata[] = array('label' =&gt; $name,'cbcolor' =&gt; $cbcolor,'owner' =&gt; $uid,'parent'=&gt;$id,'type' =&gt; $type,'permission' =&gt; $permission);
      $collectionsdataoriginal[$id] = array('label' =&gt; $name,'cbcolor' =&gt; $cbcolor,'owner' =&gt; $uid,'type' =&gt; $type);
  
      $idx = count($collectionsdata) - 1;
  
      $collectionsdatashared[] = array('myid'=&gt;$id,'suser' =&gt; $user_id-&gt;ID,'child'=&gt;$idx);
  
      update_user_meta($user_id-&gt;ID, '_wpb_collections', $collections);
      update_user_meta($user_id-&gt;ID, '_wpb_collectionsdata', $collectionsdata);
      update_user_meta($uid, '_wpb_collectionsdata', $collectionsdataoriginal);   
      update_user_meta($uid, '_wpb_collectionsdatashared', $collectionsdatashared);   
  }
  </code></pre>
  
  <p>In the last code you can see there is wp_die defined with error message. I need to alert this when the function dies to the user. Can you suggest a way to do this. </p>
  
  <p>Thanks in advance</p>