SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Jan 2002
    Location
    Roskilde, Denmark
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post vs. Get when passing $vars

    I have created this example, taken from Kevin's newest book.
    It works fine when I use method="get" in the form, but it does not work, if I use method=post"
    Any ideas why?

    <html>
    <!-- Creation date: 10/01/2002 -->
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Torben's Internet joke database</title>
    <meta name="description" content="">
    <meta name="keywords" content="">
    <meta name="author" content="Unregistered user">
    <meta name="generator" content="AceHTML 5 Pro">
    </head>
    <body>
    <?php
    if (isset($addjoke)): //If user wants to add a joke
    ?>
    <form action="<?=$php_self?>" method="get">
    <p>Type your joke herebr />
    <input type="textarea" name="joketext" rows="10" columns="40" wrap>
    </textarea><br />
    <input type="submit" name="submitjoke" value="submit" />
    </p></form>
    <?php
    else: //Default page display
    //connect to database server
    $dbcnx = @mysql_connect("torben2","root");
    if (!$dbcnx)
    {echo ("<p>Unable to connect to database server.</p>");
    exit();}
    //select jokes database
    if (! @mysql_select_db("jokes"))
    {echo ("<p>Unable to locate jokes database.</p>");
    exit();}
    //if a joke has been submitted, add it to the jokes database
    if ($submitjoke == "submit")
    {$sql = "insert into jokes 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 the database/p>");
    //Request the text of all the jokes
    $result = @mysql_query("select joketext from jokes");
    if (!$result)
    {echo("Error performing query: " . mysql_error() . "</p>");
    exit();}
    //Display the text of each joke in a paragraph
    while ($row = mysql_fetch_array($result))
    {echo ("<p>Text:" . $row[joketext] . "</p>");}
    //When clicked, this link will load this (same) page
    // with the joke submission form dispalyed
    echo ("<p><a href='$php_self?addjoke=1'>" . "Add a joke!</a></p>");
    endif;
    ?>
    </body>
    </html>

  2. #2
    SitePoint Guru
    Join Date
    Aug 2001
    Location
    Amsterdam
    Posts
    788
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmmm a couple of things...

    I would always use $PHP_SELF instead of $php_self !! but that doesn't seem to be it..

    If you press enter instead of clicking submit your not sure if $submitjoke is filled (this also depends on the browser) I would use a hidden field to set a action variable or something like that..

    I also don't understand why you use one time if():else:
    and the next if(){}else{} format?? You are best off just using one type with the {

    ohh and I use textarea different but I don't know what the right usage is
    Lets see:
    PHP Code:
    <html> 
    <!-- Creation date: 10/01/2002 --> 
    <head> 
    <title>Torben's Internet joke database</title> 
    <meta name="description" content=""> 
    <meta name="keywords" content=""> 
    </head> 
    <body> 
    <?php 
    if(isset($addjoke))
    //If user wants to add a joke 
    ?> 
    <form action="<?=$PHP_SELF ?>" method="POST"> 
    <p>Type your joke here:<br /> 
    <textarea name="joketext" rows="10" columns="40" wrap> 
    </textarea><br /> 
    <input type=hidden name="submitjoke" value="submit" />
    <input type="submit" value="submit" /> 
    </p></form> 
    <?php 
    // end of add joke part
    else
    //Default page display 

    //connect to database server 
    $dbcnx = @mysql_connect("torben2","root"); 
    if (!
    $dbcnx
    {
       echo 
    "<p>Unable to connect to database server.</p>"
       exit;

    //select jokes database 
    $db = @mysql_select_db("jokes");
    if (!
    $db
    {
       echo 
    "<p>Unable to locate jokes database.</p>"
       exit;

    //if a joke has been submitted, add it to the jokes database 
    if($submitjoke == "submit"
    {
       
    $sql "insert into jokes set joketext = '$joketext', jokedate = curdate()"

    // this is personal but I like it this way
       
    $result = @mysql_query($sql); 
       if (!
    $result
       {
          echo 
    "<p>Error adding submitted joke: " mysql_error() . "</p>";
       } 
       else
       {
          echo 
    "<p>Your joke has been added.</p>";
       } 

    echo 
    "<p>Here are all the jokes in the database:</p>"
    //Request the text of all the jokes 
    $result = @mysql_query("select joketext from jokes"); 
    if (!
    $result
    {
       echo 
    "Error performing query: " mysql_error() . "</p>"
       exit;

    //Display the text of each joke in a paragraph 

    while ($row mysql_fetch_array($result)) 
    {
       echo 
    "<p>Text:" $row[joketext] . "</p>";

    //When clicked, this link will load this (same) page 
    // with the joke submission form dispalyed 
    /*************************
    * You can't use ' or " in a echo without \ infront of it
    *************************/
    echo "<p><a href=\"$PHP_SELF?addjoke=1\">Add a joke!</a></p>"
    // there is no use for this endif; 
    ?> 
    </body> 
    </html>
    Last edited by peanuts; Jan 15, 2002 at 07:09.
    the neigbours (free) WIFI makes it just a little more fun

  3. #3
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by peanuts
    I would always use $PHP_SELF instead of $php_self !!
    better yet, $HTTP_SERVER_VARS['PHP_SELF']
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  4. #4
    SitePoint Member
    Join Date
    Jan 2002
    Location
    Roskilde, Denmark
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Unfortunately this does not help me much.
    You refer to my using this or that....
    I have taken the example from Kevin's book bit by bit, and the only thing that I have changed id the method="post" to method="get"

  5. #5
    SitePoint Guru
    Join Date
    Aug 2001
    Location
    Amsterdam
    Posts
    788
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by trugge
    Unfortunately this does not help me much.
    You refer to my using this or that....
    I have taken the example from Kevin's book bit by bit, and the only thing that I have changed id the method="post" to method="get"
    I have the book and don't have any problems with the scripts ???
    the neigbours (free) WIFI makes it just a little more fun

  6. #6
    SitePoint Member
    Join Date
    Jan 2002
    Location
    Roskilde, Denmark
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh, I just love this kind of help....

    You say you have the book and no problems. Should that make me happy or ...?

    regards
    Torben

  7. #7
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is register_globals on in your php.ini?

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  8. #8
    SitePoint Guru
    Join Date
    Aug 2001
    Location
    Amsterdam
    Posts
    788
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by trugge
    Oh, I just love this kind of help....

    You say you have the book and no problems. Should that make me happy or ...?

    regards
    Torben
    excuse me.....

    You might start with the error message you are getting so we can see that...
    and did you try my code in the earlier post??
    the neigbours (free) WIFI makes it just a little more fun


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
  •