SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: ?addjoke=2

  1. #1
    SitePoint Wizard bronze trophy conradical's Avatar
    Join Date
    Jan 2005
    Location
    Atlanta
    Posts
    2,355
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)

    ?addjoke=2

    Alright, i'm a newbie to PHP and in chapter 4 of Kevin's PHP/Mysql book. Awesome book. Anyway, there are some little things that are unexplained (yes Kevin there are some people who need an even simpler version!) So if someone could tell me WHY i would apprecieate it!

    It is a simple php page which shows jokes and a link to add a joke, below is the entire code. I understand everything but for the '?addjoke=2 what is the number for? I changed the number and it still works, i removed the number and it still works....? so can someone tell me why it there?

    Heres the code
    ---------------------------------------------------------------------------
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>The Internet Joke Database</title>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
    </head>
    <body>
    <?php if (isset($_GET['addjoke'])): // If the user wants to add a joke
    ?>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <label>Type your joke herebr />
    <textarea name="joketext" rows="10" cols="40">
    </textarea></label><br />
    <input type="submit" value="SUBMIT" />
    </form>
    <?php else: // Default page display
    // Connect to the database server
    $dbcnx = @mysql_connect('localhost', 'root', '******');
    if (!$dbcnx) {
    exit('<p>Unable to connect to the ' .
    'database server at this time.</p>');
    }
    // Select the jokes database
    if (!@mysql_select_db('ijdb')) {
    exit('<p>Unable to locate the joke ' .
    'database at this time.</p>');
    }
    // If a joke has been submitted,
    // add it to the database.
    if (isset($_POST['joketext'])) {
    $joketext = $_POST['joketext'];
    $sql = "INSERT INTO joke SET
    joketext='$joketext',
    jokedate=CURDATE()";
    if (@mysql_query($sql)) {
    echo '<p>Your joke has been added.</p>';
    } else {
    echo '<p>Error adding submitted joke: ' .
    mysql_error() . '</p>';
    }
    }
    echo '<p>Here are all the jokes in our database/p>';
    // Request the text of all the jokes
    $result = @mysql_query('SELECT joketext FROM joke');
    if (!$result) {
    exit('<p>Error performing query: ' .
    mysql_error() . '</p>');
    }
    // Display the text of each joke in a paragraph
    while ($row = mysql_fetch_array($result)) {
    echo '<p>' . $row['joketext'] . '</p>';
    }
    // When clicked, this link will load this page
    // with the joke submission form displayed.
    echo '<p><a href="' . $_SERVER['PHP_SELF'] .
    '?addjoke=2">Add a Joke!</a></p>';
    endif;
    ?>
    </body>
    </html>
    --------------------------------------------------------------------------
    If i know this..i'll have a peaceful sleep..and maybe even have dreams...

    Thanks!
    Conrad

  2. #2
    SitePoint Member
    Join Date
    Jan 2005
    Location
    Indianapolis
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This line:

    PHP Code:
    <?php if (isset($_GET['addjoke'])): // If the user wants to add a joke ?>
    checks to see if the addjoke variable is set. As long as there is something in the addjoke variable (in your example, 2), the form to submit a joke would be displayed. That's why it works regardless of what you change it to.
    Dave Scott
    President/CEO
    The Sonic Group, LLC

  3. #3

    Join Date
    Oct 2003
    Location
    €uroLand
    Posts
    1,340
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In this example it doesnt seem to matter. addjoke can contain whatever you want as the actual value is not queried but the script only checks whether addvalue is set at all
    PHP Code:
    <?php if (isset($_GET['addjoke'])): // If the user wants to add a joke
    Imagine it as kind of status indicator which tells the script to do a certain thing. A cleaner and more obvious solution would be
    Code:
    ?addjoke=true
    Hope you have now a peaceful sleep and sweet dreams .... hey! I am talking to you as if you were a girl



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
  •