Results 1 to 2 of 2

Thread: Ampersands in PHP-populated drop-down list

  1. #1
    Join Date
    Oct 2008
    Location
    New York, NY
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Ampersands in PHP-populated drop-down list

    Hi,

    I'm trying to create a new photo gallery for my web site that calls up the various galleries and photos in a dynamic ajax div, and also using a MySQL database with information on each gallery.

    The user will select a gallery from a drop-down menu, the code for which I have below:

    HTML Code:
    <form name="gal" action="" method="GET">
    <div align="center">
    <select name="gal" onchange="this.form.submit();">
    <?php
    
    // Include the MySQL class
    $dbhost = 'XXXX';
    $dbuser = 'XXXX';
    $dbpass = 'XXXX';
    
    $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die                      ('Error connecting to mysql');
    
    $dbname = 'database';
    mysql_select_db($dbname);
    
    $result = mysql_query("SELECT DISTINCT gallerynumber, galleryname FROM phototable");
    if (!$result) {
    	exit ('Error performing query: ' .  mysql_error());
    }	
    
    while($row1 = mysql_fetch_array($result))
    {
    echo
    "<option value='".$row1['gallerynumber']."&photo=1'>".$row1['galleryname']."</option>";
    }
    
    ?>
    </select>
    </div>
    </form>
    Ideally, what I want is to pass the "gallerynumber" to the URL, which will ultimately look something like "/gallery.php?gal=1&photo=1". Everything comes out fine in the address bar until the ampersand, and then the "=". What ends up being passed is: "/gallery.php?gal=2%26photo%3D1".

    I managed to make it work if I passed the variables through regular links:
    PHP Code:
    while($row1 mysql_fetch_array($result))
    {
    echo
    "<a href='?gal=".$row1['gallerynumber']."&photo=1'>".$row1['galleryname']."</a>&nbsp;&nbsp;|&nbsp;&nbsp;";

    but would prefer not to do so.

    I'm new enough to be incredibly unfamiliar with escaping characters. Is there a simple way around this?

    Thanks so much in advance!

  2. #2
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    Use &amp; instead of &, even if you're echoing an ordinary link. & is reserved in HTML for declaring special characters (including &amp;), and should only be used to do so.
    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •