www.webdeveloper.com
Results 1 to 4 of 4

Thread: Weird POST behavior - only works on refresh

  1. #1
    Join Date
    Mar 2008
    Posts
    7

    Weird POST behavior - only works on refresh

    I've just uploaded my production site to Go Daddy so I can get it testing on the actual server, etc... I've run into a bug that I can't figure out for the life of me. Here's what's happening:

    I'm allowing users to input a "project" consisting of four fields: project name, project category, project date, and project description. From the first page, it takes their information submitted through POST into a second page where it displays the information they submitted for confirmation. So far, nothing has been put into the database. Upon pressing "Submit" on the confirm page, it again sends their info through POST (this time as hidden fields) to the final page where the query runs that puts it into the database. Here's the code:

    The script on the confirmation page:
    PHP Code:
    <?php
    function postProjDetails() {

    //create short variable names

    $proj_name $_POST['proj_name'];
    $proj_cat $_POST['projectcat'];
    $proj_date $_POST['proj_date'];
    $desc $_POST['proj_desc'];

    //draw form containing project details and submit form.  This form calls an action on the next page to actually submit the project.  The function passes the user submitted values as hidden fields so as to pass via post to the actual submit function.

    ?>
    <form action="post_submit.php" method="post">
    <input name="proj_name" type="hidden" value="<?php echo $proj_name?>" />
    <input name="projectcat" type="hidden" value="<?php echo $proj_cat?>" />
    <input name="proj_date" type="hidden" value="<?php echo $proj_date?>" />
    <input name="proj_desc" type="hidden" value="<?php echo $desc?>" />

    <p>Confirm your project details:</p>
    <p>Project Name: <?php echo $proj_name?></p>
    <p>Project Category: <?php echo $proj_cat?></p>
    <p>Project Date: <?php echo $proj_date?></p>
    <p>Project Description: <?php echo $desc?></p>

    <input name="Submit" type="submit" value="Submit" />

    </form>

      <?php
    }
    As of this point, everything is displaying fine. Information has passed as expected from page 1 to page 2.

    The script on the final page that actually executes the query:

    PHP Code:
    function submitProj($user_id$user_type) {

    //create short var names

    $proj_name $_POST['proj_name'];
    $proj_cat $_POST['projectcat'];
    $proj_date $_POST['proj_date'];
    $desc $_POST['proj_desc'];




    $query "insert into projects values (NULL, '".$user_id."', '".$proj_name."', '".$proj_cat."', '".$proj_date."', '".$desc."', '".$user_type."')";
    $result mysql_query($query);

    if (!
    $result)
    {
    echo 
    'Sorry, there was a problem entering your project into the database.  The error was '.mysql_error();
    exit;
    }
    else
    {
    echo 
    'Done!  Your project is now in the database!';
    }


    The problem is, when it gets to the third page, it doesn't have the info they submitted - it simply passes through as blank. That is, UNLESS I hit refresh. Then, it resends the info and everything works as it should. Obviously, I don't want the user having to refresh everytime they want to submit something. Anything jump out that might be the problem?

    Thanks ya'll!

  2. #2
    Join Date
    Feb 2007
    Location
    Scotland
    Posts
    121
    Could you post more of the 3rd page so we can get a better idea of how the page is functioning.

    Have you tried just printing the post variables to screen to see if its a problem with them or the vriables being added to the query string?

  3. #3
    Join Date
    Mar 2008
    Posts
    7
    Sure, no problem. Here's the actual page that calls the function I posted above:

    PHP Code:
    <?php 
    require('includes/functions.php');

    $page_title 'Project Submitted!';



    require(
    'includes/header.php');

    require(
    'includes/sidebar_left.php');
    require(
    'includes/sidebar_right.php');
    require(
    'includes/pri_nav_loggedin.php');

    // Begin center content
    ?>
    <div id="contentwell"> 

    <p>Post a project</p>


    <div id = "current_proj">
    <p>Your Current Projects</p><hr />
    <?php myCurrentProj($user_id); ?>
    </div>
    <div id = "confirm">
    <?php submitProj($user_id$user_user_type); ?> <!-- just for your information, these variables are set via a function that loads global variables according to whichever user is logged in at the time. -->
    </div>



    <?php
    require('includes/footer.php');
    ?>
    </div>
    <!-- end #container --></div>
    </body>
    </html>
    <?php

    ?>
    I've tried printing the vars, and the same thing happens. Basically, the first time I submit, they come up empty. If I hit refresh, they fill in correctly.

    I'm thinking it may have something to do with variable scope. I've fixed the problem for now by using $_SESSION['var'] instead of $_POST['var'] but I'd still like to know why the latter is being screwy.

    Thanks much.

  4. #4
    Join Date
    Feb 2007
    Location
    Scotland
    Posts
    121
    hmm that is weird.

    Have you tried using $_REQUEST instead of $_POST, shouldnt make a difference. But wont hurt to try. Also try submitting the form using GET to see if the same issue occurs with the querystring.

    When you were printing the $_POST data, did you print inside the function?
    You could try assigning the variables outside the function, then give them global scope inside the function.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles