SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Member hoxr.com's Avatar
    Join Date
    Oct 2009
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help on form reload problem

    Dear all, I am writing a very simple code which involve HTML, PHP and Mysql, here is what I have done and the Help I need:

    the 1st file: index.php
    PHP Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>  
     <title>Testing Site <?php echo $_GET[id];?></title>
    </head>
    <body>
        <?php 
        
    if($_GET[id]){
         require_once(
    $_GET[id].'.html.php');
        }else{
         require_once(
    'bb.html.php');
        }
        
    ?>
             <?php require_once('comments.php'); ?>
              <a href = 'index.php?id=aa'> goto aa </a><br/>
        <a href = 'index.php?id=bb'> goto bb </a>
    </body>
    </html>
    the 2nd file aa.html.php
    PHP Code:
    <p>Hi...I am aa</p
    the 3rd file bb.html.php
    PHP Code:
    <p>Hi...I am bb</p
    the 4th file comments.php
    PHP Code:
    <form name="form" method="post" action="./index.php?id=<?php echo $_GET[id];?>">
        Name:<input name="user" type="text" size=20 id="user"><br>
        Comments:<input name="comments" type="text" size=30 id="comments"><br>
        <input type="submit" size=20 value="submit"><br>
    </form>
    <?php
    //connect db
    $conn mysql_connect("localhost","root","") or die ("cannot connection"); 
    mysql_select_db("mydb",$conn) or die ("cannot open db");
    //if post, then start process data
    if ($_POST){
    $user $_POST["user"];
    $comments $_POST["comments"];
    $query "INSERT INTO comments (user, comments) VALUES('$user','$comments')";  
    $result mysql_query($query);
    }
    //no matter, loop and Show all record to the user
    $selectall "select * from comments";  
    $showall mysql_query($selectall);
    while (
    $row mysql_fetch_array($showall)){
     echo 
    'user:'.$row[user].'comments:'.$row['comments'].'<br />'
    }
    ?>
    the DB/table related:
    I have a DB named mydb in MySQL, which have on table, comments:
    Code:
    CREATE TABLE IF NOT EXISTS `comments` (
      `user` varchar(30) NOT NULL,
      `comments` varchar(80) DEFAULT NULL,
    ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 ;
    run the scripts:

    load index.php after you creat the table using above sql..and jump between index.php?id=aa and index.php?id=bb, and u can submit the form and see the comments imm showing after ur submit...

    it looks great, BUT

    My problem:

    if user click the reload or refresh in IE, there is a warning window popup, and remind user not re-submit, but if some user really resubmit, my table will have duplicate record...

    HELP is need, with no change on my entire scripts stucture, means I must use above ?id=$_GET(id) tech to jumping between the aa file and bb file, and I must include both form and insert process in same file, the user expriense should not been impact........

    what I need is while user click the reload, no pop window, no duplicate record been insert

    thanks in advance for what ever help you can provide...but I already try to search solution, so, pls don't just give me very generical hints..

    I am total lost....need real help

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by hoxr.com View Post
    [B]if user click the reload or refresh in IE, there is a warning window popup, and remind user not re-submit, but if some user really resubmit, my table will have duplicate record...
    That is natural and will happen in all browsers, you need to redirect the user to another page (or back to the same page from a "post" page) to avoid this.

    More info here http://en.wikipedia.org/wiki/Post/Redirect/Get
    For php you can use this to redirect
    PHP Code:
    header('Location: http://example.com/somepage.php');
    exit; 
    // make sure the script stops 

  3. #3
    SitePoint Enthusiast Codebox's Avatar
    Join Date
    Jan 2004
    Posts
    78
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A simple way to fix this would be to store the following code that you wrote (with a little modification from my side) in a separate file (e.g. submit.php):

    PHP Code:
    <?php

    // enable output buffering to allow for redirect later in the page
    ob_start();

    //connect db
    $conn mysql_connect("localhost","root","") or die ("cannot connection"); 
    mysql_select_db("mydb",$conn) or die ("cannot open db");
    //if post, then start process data
    if ($_POST){
    $user $_POST["user"];
    $comments $_POST["comments"];
    $query "INSERT INTO comments (user, comments) VALUES('$user','$comments')";  
    $result mysql_query($query);
    }

    // redirect the user to the main page
    header("Location: index.php");

    ?>
    and update your HTML form to have an action that goes to the above submit.php script:

    Code:
    <form name="form" method="post" action="./submit.php">

  4. #4
    SitePoint Member hoxr.com's Avatar
    Join Date
    Oct 2009
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hash, thanks for the response, it not work in my case, I tried it....or let me know how I should change my code, u know, the form "action" in this case is goto load the same file, thurs I can keep user in same page..

    I almost try all methodology people metioned while I doing google research, but seems not work in my case, I am not sure where wrong

    the header(location:someotherpage) is good while the form action goto another page, and that page can process data ten redirect to form page, but in my case, the data process page is same with form page....

  5. #5
    SitePoint Member hoxr.com's Avatar
    Join Date
    Oct 2009
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    codebox, appreciate the answer, but the problem here is I don't want change my scripts structure....

    my question is w/o big change on my current 4 file, (which means only change code inside those 4 files) and DB/table as well user experiense, how to fix the reload issue.......

    thanks team.

  6. #6
    SitePoint Enthusiast Codebox's Avatar
    Join Date
    Jan 2004
    Posts
    78
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ummmm, i'm not too sure how you can avoid "warning window popup" for repost when using the same page for submission and display - unless you use AJAX - but for that too, you'd have to have additional files added up.

  7. #7
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Codebox View Post
    ummmm, i'm not too sure how you can avoid "warning window popup" for repost when using the same page for submission and display - unless you use AJAX - but for that too, you'd have to have additional files added up.
    .

  8. #8
    SitePoint Member hoxr.com's Avatar
    Join Date
    Oct 2009
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    any other solution? Ajax is good solution may be, but I really need learn a new language because this problem?

  9. #9
    SitePoint Zealot zalucius's Avatar
    Join Date
    Jul 2007
    Location
    Denmark
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The header() method also works when its to the same page.

    Here is an example, of your "comments.php" with the needed modifications
    Code PHP:
    <?php
    //connect db
    $conn = mysql_connect("localhost","root","") or die ("cannot connection"); 
    mysql_select_db("mydb",$conn) or die ("cannot open db");
    // if pageload is from a form submit, then insert.
    if (isser($_POST['post_comment']){
    	$user = mysql_real_escape_string($_POST["user"]);
    	$comments = mysql_real_escape_string($_POST["comments"]);
    	$query = "INSERT INTO comments (user, comments) VALUES('$user','$comments')";  
    	$result = mysql_query($query,$conn) or die(mysql_error($conn));
    	// after insert into db, then reload the page
    	header("location:".$_SERVER['PHP_SELF']."?id=".$_GET['id']);
    }
    ?>
     
    <form name="form" method="post" action="./index.php?id=<?php echo $_GET[id];?>">
        Name:<input name="user" type="text" size=20 id="user"><br>
        Comments:<input name="comments" type="text" size=30 id="comments"><br>
        <input type="submit" size=20 value="submit" name="post_comment"><br>
    </form>
     
    <?php
    //no matter, loop and Show all record to the user
    $selectall = "select * from comments";  
    $showall = mysql_query($selectall,$conn);
    while ($row = mysql_fetch_array($showall))
    {
    	echo 'user:'.$row['user'].'comments:'.$row['comments'].'<br />'; 
    }
    ?>
    zalucius

  10. #10
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by hoxr.com View Post
    Hash, thanks for the response, it not work in my case, I tried it...
    If you need help you have to always provide sufficient information on
    - what exactly you tried
    - what result you expect
    - what exactly you get

    In present case we can guess that you've got "Headers already sent" error, but in most cases guessing won't help.

    zalucius's code can solve this, with some corrections
    First, there must be "isset" instead of "isser" and one more brackert on line 6:
    Code PHP:
    if (isset($_POST['post_comment'])){
    Second, header("location") must be always followed by a line with exit;

    Third, make it 2-file way. Set comments.php as form's action and
    return to index with Location header.


    also, note mysql_real_escape_string function in his code. it is critically important. always use it then putting data into SQL query. It is not related to your current priblem but will save you from future ones.

    If you have any other problem, explain it using words of language and/or code, so we'll be able to help you.

  11. #11
    SitePoint Zealot zalucius's Avatar
    Join Date
    Jul 2007
    Location
    Denmark
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Shrapnel_N5, you're absolutely right.
    Then I can learn to test code before I post it
    zalucius

  12. #12
    SitePoint Member hoxr.com's Avatar
    Join Date
    Oct 2009
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    zalucius, thanks for the response, but I tested ur code as well plus the Shrapnel_N5's first and scecond correction, it not fix my problem, it have the warning message :"Cannot modify header information - headers already sent by (output started at index.php:5) in comments.php on line 17"

    Shrapnel_N5: noticed ur 3rd correction: "Third, make it 2-file way. Set comments.php as form's action and
    return to index with Location header." ---------but I already state very clear I expect the data process page is same with form page....and I don't want change this....

    still look for better solution....appreciate ur time and patients...

  13. #13
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, you're just wrong and have to change your mind.
    The problem not in the "Headers" error itself.
    But in the site structure you choose.
    There is no single reason to put all your site into one file.
    You have to split it into functional modules.
    So, let index.php do the index job and comments.php - comments.job.

    If your site going to save comment, it shouldn't make half of the index job as it goes when you make index as the comments form action. This will be waste and will cause errors in the future. Action url for the comments form must be comments.php, which will do the job of saving comment and nothing more.
    And then make Location to where you want.
    You need Location redirect anyway. And there is no matter from which script it called.

  14. #14
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by hoxr.com View Post
    zalucius, thanks for the response, but I tested ur code as well plus the Shrapnel_N5's first and scecond correction, it not fix my problem, it have the warning message :"Cannot modify header information - headers already sent by (output started at index.php:5) in comments.php on line 17"
    You can't send anything to the browser before sending headers. White space, echo, html etc cannot be before a header call.

    Quote Originally Posted by hoxr.com View Post
    Shrapnel_N5: noticed ur 3rd correction: "Third, make it 2-file way. Set comments.php as form's action and
    return to index with Location header." ---------but I already state very clear I expect the data process page is same with form page....and I don't want change this....

    still look for better solution....appreciate ur time and patients...
    Zalucius showed how this can be done, it's up to you, but I'd agree with shrapnel (without the bold :P)

  15. #15
    SitePoint Member hoxr.com's Avatar
    Join Date
    Oct 2009
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    appreciate all great comments and solution....especially for what Shrapnel's point on split to functional modules.that's convince me change my mind to 2-file way, then solution people help provided here works great....thanks again....team!


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
  •