• Adding Bookmarks the AJAX Way

    Please find following code for adding bookmarks the ajax way here.
    Here are the scripts:

    THE AJAX.JS FILE

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    
    function getXMLHTTPRequest() {
       var req =  false;
       try {
          /* for Firefox */
          req = new XMLHttpRequest();
       } catch (err) {
          try {
             /* for some versions of IE */
             req = new ActiveXObject("Msxml2.XMLHTTP");
          } catch (err) {
             try {
                /* for some other versions of IE */
                req = new ActiveXObject("Microsoft.XMLHTTP");
             } catch (err) {
                req = false;
             }
         }
       }
     
       return req;
    }
     
    function addNewBookmark() {
      var url = "add_bms.php";
      var params = "new_url=" + encodeURI(document.getElementById('new_url').value);
      myReq.open("POST", url, true);
      myReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      myReq.setRequestHeader("Content-length", params.length);
      myReq.setRequestHeader("Connection", "close");
      myReq.onreadystatechange = addBMResponse;
      myReq.send(params);
    }
     
    function addBMResponse() {
      if (myReq.readyState == 4) {
        if(myReq.status == 200) {
           result = myReq.responseText;
           document.getElementById('displayresult').innerHTML = result;
        } else {
          alert('There was a problem with the request.');
        }
      }
    }

    THE ADD BOOKMARKS FUNCTION FROM THE OUTPUT LIBRARY

    1
    2
    3
    4
    5
    
    function display_add_bm_form() {
      // display the form for people to enter a new bookmark in
    ?&gt;<script type="text/javascript">// <![CDATA[
    var myReq = getXMLHTTPRequest();
    // ]]></script>
    New BM:

    THE ADD_BM FUNCTION FROM THE URL FUNCTION LIBRARY

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    
    function add_bm($new_url) {
      // Add new bookmark to the database
     
      echo "Attempting to add ".htmlspecialchars($new_url)."
    ";
      $valid_user = $_SESSION['valid_user'];
     
      $conn = db_connect();
     
      // check not a repeat bookmark
      $result = $conn-&gt;query("select * from bookmark
                             where username='$valid_user'
                             and bm_URL='".$new_url."'");
     
      if ($result &amp;&amp; ($result-&gt;num_rows&gt;0)) {
        echo "

    Bookmark already exists.

    1
    2
    3
    4
    5
    6
    
    ";
      } else {
        //attempt to add
        if (!$conn-&gt;query("insert into bookmark values
           ('".$valid_user."', '".$new_url."')")) {
           echo "

    Bookmark could not be inserted.

    1
    2
    3
    
    ";
        } else {
           echo "

    Bookmark added.

    1
    2
    3
    4
    5
    6
    
    ";
        }
      }
     
      return true;
    }

    THE DB_CONNECT FUNCTION FROM THE DB FUNCTION LIBRARY

    1
    2
    3
    4
    5
    6
    7
    8
    
    function db_connect() {
       $result = new mysqli('localhost', 'bm_user', 'password', 'bookmarks');
       if (!$result) {
         throw new Exception('Could not connect to database server');
       } else {
         return $result;
       }
    }

Comments on this post

Leave a Reply

  • Security Code :


    − 7 = one