13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Adding Ajax Elements to Earlier Projects<br />

881<br />

Finally, <strong>and</strong> regardless of the errors in attempting to add a URL, the user’s existing bookmarks<br />

are displayed.You can see this result in Figure 34.8.<br />

Figure 34.8<br />

Success—a valid URL has been added.<br />

Although the core functionality around adding a bookmark has been successfully Ajaxenabled,<br />

a few elements still need some work. For instance, the add_bm() function in the<br />

url_fns.php file contains some exceptions that could be h<strong>and</strong>led differently to produce<br />

an error message in this new system. Listing 34.10 shows the existing add_bm() function.<br />

Listing 34.10<br />

Existing add_bm()Function in url_fns.php<br />

function add_bm($new_url) {<br />

// Add new bookmark to the database<br />

echo "Attempting to add ".htmlspecialchars($new_url)."";<br />

$valid_user = $_SESSION['valid_user'];<br />

$conn = db_connect();<br />

// check not a repeat bookmark<br />

$result = $conn->query("select * from bookmark<br />

where username='$valid_user'

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!