SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast buffyspazz's Avatar
    Join Date
    Jan 2002
    Location
    Richmond, VA
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP guestbook form problem on submit

    I am trying to install a php guestbook script on a website I'm working on. The script uses a mysql database to log the entries, and the following files for the script:

    guestbook.php - shows the last five entries and has links to view all of the entries or sign the guestbook
    signguestbook.php - the page to sign the guestbook
    viewguestbookall.php - the page to see all of the entries

    (Here's my normal disclaimer...I know NOTHING about PHP...well, I know how to use include/require, and I understand echo, but that's it. I'm just starting out, so please make your answers appropriate for an imbecile, because that's what you're dealing with.)

    I installed the script and tested it out, and everything worked fine if I just use the generic php pages provided with the script (with html tags, etc. in them).

    However, the site layout I'm working on uses "require" for all content - the menu and sidebar are includes, the copyright notice is an include, even the main content is an include. I modified the pages above from the script I downloaded to take out the html tags so I could use the files as includes, and the pages display correctly. However, when I fill out the form and click "send," no data is sent to the database, and users are taken to my home page (www.mydomain.com/index.php?page=main) instead of staying on the "signguestbook.php" page with the "thank you for signing" message showing up. When I go back to the guestbook.php page, the entry isn't recorded. I'm thinking the problem must be something to do with the fact that I'm using this form/script as part of an include, but I can't figure out what to change to make it work. I've looked through about 2937402983 pages, both here and on the internet, and I can't find an explanation that seems to address this specific problem that I can understand. ANY help is greatly appreciated.

    Here is the code from "signguestbook.php," since I'm guessing that's where the problem is:

    PHP Code:
    <p><b>.: guestbook :.</b></p>
    <?php
    include("config.php");
    $form "<form action=\"$PHP_SELF\" method=\"post\">";
    $form.= "name: <input type=\"text\" name=\"name\" ";
    $form.= "size=\"50\" value=\"$name\"> <br>";
    $form.= "email: <input type=\"text\" name=\"email\" ";
    $form.= "size=\"50\" value=\"$email\"> <br>";
    $form.= "comments:<br>";
    $form.= "<textarea name=\"comments\" cols=\"45\" ";
    $form.= "rows=\"4\">$comments</textarea> <br>";
    $form.= "<input type=\"submit\" name=\"submit\" ";
    $form.= "value=\"sign\"> </form>";
    if ( !
    $submit) { $msg $form; } else
    if ( !
    $name or !$email or !$comments)
    $msg "<b>please complete all the fields</b><br><br>";
    $msg.= $form; } else
    #connect to MySQL
    $conn = @mysql_connect ("$server""$db_user""$db_pass")
    or die (
    "could not connect to database, please make sure host, database name, user name and password are correct");
    $rs = @mysql_select_db ("$database"$conn)
    or die (
    "could not connect to database, please make sure host, database name, user name and password are correct");
    if (
    $name and $comments)

    $sql =" insert into gcguestbook (name, email, comments)
    values (\"
    $name\",\"$email\",\"$comments\")";
    $rs = @mysql_query ($sql$conn)
    or die (
    "this is really hot"); }
    if (
    $rs)
    $msg "<h3>thank you - your entry has been saved.";
    $msg.= "<br><a href = \"index.php?page=guestbook\">";
    $msg.= "view guestbook</a></h3>"; }
    }
    echo (
    $msg);
    ?>
    Can anyone give me any ideas on this? I thought at first it might be because I added the first line (
    HTML Code:
    <p><b>.: guestbook :.</b></p>
    above the PHP code, but when I took it out it still didn't work. Here is the link to the site: http://fans.dean-armstrong.com (please disregard the fact that my stylesheet isn't calling - I'm working on that with my webhost, since it literally stopped working since last night *grrr*) - the link to the guestbook is on the menu. The two entries that are in there are from when I was testing out the pages initially, before I incorporated them as includes.

    Please let me know if there is anything else I should post to help out. Thanks in advance!

  2. #2
    SitePoint Addict Knightime's Avatar
    Join Date
    Jan 2003
    Location
    USA
    Posts
    376
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this out.

    PHP Code:
    <p><b>.: guestbook :.</b></p>
    <?php
    $PHP_SELF 
    $_SERVER['PHP_SELF'];
    include(
    "config.php");
    $form "<form action=\"$_SERVER['PHP_SELF']\" method=\"post\">";
    $form.= "name: <input type=\"text\" name=\"name\" ";
    $form.= "size=\"50\" value=\"$name\"> <br>";
    $form.= "email: <input type=\"text\" name=\"email\" ";
    $form.= "size=\"50\" value=\"$email\"> <br>";
    $form.= "comments:<br>";
    $form.= "<textarea name=\"comments\" cols=\"45\" ";
    $form.= "rows=\"4\">$comments</textarea> <br>";
    $form.= "<input type=\"submit\" name=\"submit\" ";
    $form.= "value=\"sign\"> </form>";
     if (!isset(
    $_POST['submit'])) {
    $msg $form;
    } elseif ( !
    $name or !$email or !$comments){
    $msg "<b>please complete all the fields</b><br><br>";
    $msg.= $form;
    } else { 
    #connect to MySQL
     
      
    $conn = @mysql_connect ("$server""$db_user""$db_pass");
       or die (
    "could not connect to database, please make sure host, database name, user name and password are correct");
      
    $rs = @mysql_select_db ("$database"$conn);
       or die (
    "could not connect to database, please make sure host, database name, user name and password are correct");
     if (
    $name and $comments) {
     
      
    $sql =" INSERT INTO gcguestbook (name, email, comments) VALUES ('$name', '$email', '$comments')";
      
    $rs = @mysql_query ($sql$conn);
       or die (
    "this is really hot"); }
       if (
    $rs) {
    $msg "<h3>thank you - your entry has been saved.";
    $msg.= "<br><a href = \"index.php?page=guestbook\">";
    $msg.= "view guestbook</a></h3>";
     }
    }
    echo (
    $msg);
    ?>
    Troy

  3. #3
    SitePoint Enthusiast buffyspazz's Avatar
    Join Date
    Jan 2002
    Location
    Richmond, VA
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, I tried that out, but when I uploaded it, it wouldn't even show the page (it showed the index.php page plus all of the other includes, but not the content include, ie, signguestbook.php). I went ahead and re-uploaded the original file back, and the page showed up again.

    Thank you for responding, though! Any other ideas are still appreciated.

  4. #4
    SitePoint Zealot
    Join Date
    Jan 2004
    Location
    vta,ca,usa
    Posts
    180
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The problem lies in using $PHP_SELF. Because you include the script within your index page, it will always call your index page (which doesn't have the form handling code on it) rather than the 'signguestbook.php' script, that's meant to handle the form. So even if you change your logic around as Knightime suggests, it will never call that script.

    I can make two suggestions in this case: either change your form action to 'index.php?page=guestbook', or change the script around slightly so it points correctly to itself, 'signguestbook.php' and then logic dictates if the form is to be shown or if the database query runs. If the latter, after the db query and the info has been submitted to the database, use header() to send you back to 'index.php?page=guestbook' and have some sort of GET string or otherwise to determine if the form was inserted correctly.

    To Knightime: why bother assigning $_SERVER['PHP_SELF'] to $PHP_SELF var and then using $_SERVER['PHP_SELF'] as the target anyway??

  5. #5
    SitePoint Enthusiast buffyspazz's Avatar
    Join Date
    Jan 2002
    Location
    Richmond, VA
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So, I changed my form action to "index.php?page=signguestbook" (I tried "index.php?page=guestbook" at first, and it sent me to the guestbook page but didn't make the post), and it worked perfectly. Thank you so much...you have saved me much angst!

    As always, you guys rock.

  6. #6
    SitePoint Addict Knightime's Avatar
    Join Date
    Jan 2003
    Location
    USA
    Posts
    376
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In Case Globals are off.
    Troy


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
  •