SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Evangelist
    Join Date
    Aug 2010
    Posts
    503
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    UPDATE Display on Reload

    Hi all, I have a form which is for the user to update their details. When they press 'Update', this does indeed update the database and page refreshes but refreshes with still the old data. I need to refresh *again* for it to show the latest updated record. Any ideas as to what I can do to conquer this?

    PHP 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">
    <?php
    require('auth.php');
    require(
    'includes/head.php');
    ?>
    <body>
    <?php
    include('includes/header.php');
    require(
    'includes/connection.php');
        
    $url $_GET['id'];
            
    $query mysql_query("SELECT * FROM customers WHERE customer_ref='$url'");
                
    $res mysql_fetch_array($query);
                    if (isset(
    $_POST['update'])) {
                        
    $update mysql_query("UPDATE customers SET c_dob='$_POST[cdob]' WHERE customer_ref='$url'");
                        }
    ?>
    <div id="wrap">
    <div id="mainWrap">
    <form action="<?=$_SERVER['PHP_SELF'].'?id='.$url?>" method="post" id="createorder">
    <div class="headerTitles">Edit Customer Details</div>
    <!-- BOF Left Col -->
    <div id="leftCol">
    <span class="fontField bold">Title </span>
    <select class="textField" <?=$disabled?>  name="title">
      <option value="NULL">Choose a Title</option>
      <option value="Mr" <?php if ($res['c_title']=="Mr"){ echo "selected=\"selected\"";} ?>>Mr</option>
      <option value="Mrs" <?php if ($res['c_title']=="Mrs"){ echo "selected=\"selected\"";} ?>>Mrs</option>
      <option value="Miss" <?php if ($res['c_title']=="Miss"){ echo "selected=\"selected\"";} ?>>Miss</option>
      <option value="Ms" <?php if ($res['c_title']=="Ms"){ echo "selected=\"selected\"";} ?>>Ms</option>
      <option value="Dr" <?php if ($res['c_title']=="Dr"){ echo "selected=\"selected\"";} ?>>Dr.</option>
      <option value="Rev" <?php if ($res['c_title']=="Rev"){ echo "selected=\"selected\"";} ?>>Rev</option>
    </select>
    <span class="fontField bold lower">First Name </span>
    <input type="text" name="cfirstname" <?=$disabled?> value="<?php echo $res['c_firstname'?>" class="<?php if(isset($alert[1])){echo $alert[1];} else {echo "textField";} ?>" />

    <span class="fontField bold lower">Last Name </span>
    <input type="text" name="clastname" <?=$disabled?> value="<?php echo $res['c_lastname'?>" class="<?php if(isset($alert[2])){echo $alert[2];} else {echo "textField";} ?>" />

    <span class="fontField bold lower">Address </span>
    <textarea name="caddress" <?=$disabled?> rows="6" cols="0" class="<?php if(isset($alert[3])){echo $alert[3];} else {echo "textField";} ?>"><?php echo $res['c_address'?></textarea>

    <span class="fontField bold lower">Contact Number</span>
    <input type="text" <?=$disabled?> name="ccontactnumber" value="<?php echo $res['c_number'?>" class="textField" />

    <span class="fontField bold lower">Email Address</span>
    <input type="text" <?=$disabled?> name="cemail" value="<?php echo $res['c_email'?>" class="textField" />
    </div>
    <!-- EOF Left Col -->

    <!-- BOF Right Col -->
    <div id="rightCol">
    <span class="fontField bold">Registration Date</span>
    <input type="text" name="cname" value="<?php echo $res['c_regdate'?>" class="textField" disabled="disabled" />

    <span class="fontField bold lower">Customer ID Number</span>
    <input type="text" name="custnotes" value="<?php echo $res['customer_ref'?>" class="textField" disabled="disabled" />

    <span class="fontField bold lower">Customer Notes</span>
    <textarea name="cnotes" <?=$disabled?> class="textField" rows="6" cols="0"><?php echo $res['c_notes'?></textarea>

    <span class="fontField bold lower">Date of Birth</span>
    <input type="text" name="cdob" <?=$disabled?> value="<?=$res['c_dob']?>" class="textField" />
    </div>
    <div class="headerTitles"><input type="submit" value="Update" name="update" id="update" /></div>
    </form>
    </body>
    </html>

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Just perform a PRG, it should help you out.

    So, check if the form is being submitted, if so update then redirect the user back to the same page using header. If not, render the form.

    As I don't know exactly what is going on, something like...

    PHP Code:
    <?php
      
    require('auth.php');
      require(
    'includes/connection.php');
      
      if(
    'POST' === $_SERVER['REQUEST_METHOD']){
        
    #update record
        #if successfull, redirect back to this page
        
    header('Location: http://www.example.org/update.php'true302);
        exit;
      }
    ?>
    <!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">
    <?php
      
    require('includes/head.php');
    ?>
    <body>
    <?php
      
    include('includes/header.php');
      
    $url $_GET['id'];
      
    $query mysql_query("SELECT * FROM customers WHERE customer_ref='$url'");
      
    $res mysql_fetch_array($query);
    ?>
    <div id="wrap">
    <div id="mainWrap">
    <form action="<?=$_SERVER['PHP_SELF'].'?id='.$url?>" method="post" id="createorder">
    <div class="headerTitles">Edit Customer Details</div>
    <!-- BOF Left Col -->
    <div id="leftCol">
    <span class="fontField bold">Title </span>
    <select class="textField" <?=$disabled?>  name="title">
      <option value="NULL">Choose a Title</option>
      <option value="Mr" <?php if ($res['c_title']=="Mr"){ echo "selected=\"selected\"";} ?>>Mr</option>
      <option value="Mrs" <?php if ($res['c_title']=="Mrs"){ echo "selected=\"selected\"";} ?>>Mrs</option>
      <option value="Miss" <?php if ($res['c_title']=="Miss"){ echo "selected=\"selected\"";} ?>>Miss</option>
      <option value="Ms" <?php if ($res['c_title']=="Ms"){ echo "selected=\"selected\"";} ?>>Ms</option>
      <option value="Dr" <?php if ($res['c_title']=="Dr"){ echo "selected=\"selected\"";} ?>>Dr.</option>
      <option value="Rev" <?php if ($res['c_title']=="Rev"){ echo "selected=\"selected\"";} ?>>Rev</option>
    </select>
    <span class="fontField bold lower">First Name </span>
    <input type="text" name="cfirstname" <?=$disabled?> value="<?php echo $res['c_firstname'?>" class="<?php if(isset($alert[1])){echo $alert[1];} else {echo "textField";} ?>" />
    <span class="fontField bold lower">Last Name </span>
    <input type="text" name="clastname" <?=$disabled?> value="<?php echo $res['c_lastname'?>" class="<?php if(isset($alert[2])){echo $alert[2];} else {echo "textField";} ?>" />
    <span class="fontField bold lower">Address </span>
    <textarea name="caddress" <?=$disabled?> rows="6" cols="0" class="<?php if(isset($alert[3])){echo $alert[3];} else {echo "textField";} ?>"><?php echo $res['c_address'?></textarea>
    <span class="fontField bold lower">Contact Number</span>
    <input type="text" <?=$disabled?> name="ccontactnumber" value="<?php echo $res['c_number'?>" class="textField" />
    <span class="fontField bold lower">Email Address</span>
    <input type="text" <?=$disabled?> name="cemail" value="<?php echo $res['c_email'?>" class="textField" />
    </div>
    <!-- EOF Left Col -->
    <!-- BOF Right Col -->
    <div id="rightCol">
    <span class="fontField bold">Registration Date</span>
    <input type="text" name="cname" value="<?php echo $res['c_regdate'?>" class="textField" disabled="disabled" />
    <span class="fontField bold lower">Customer ID Number</span>
    <input type="text" name="custnotes" value="<?php echo $res['customer_ref'?>" class="textField" disabled="disabled" />
    <span class="fontField bold lower">Customer Notes</span>
    <textarea name="cnotes" <?=$disabled?> class="textField" rows="6" cols="0"><?php echo $res['c_notes'?></textarea>
    <span class="fontField bold lower">Date of Birth</span>
    <input type="text" name="cdob" <?=$disabled?> value="<?=$res['c_dob']?>" class="textField" />
    </div>
    <div class="headerTitles"><input type="submit" value="Update" name="update" id="update" /></div>
    </form>
    </body>
    </html>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    151 Post(s)
    Tagged
    3 Thread(s)
    Use header() to redirect the user once the database has been updated

    PHP Code:
    if (isset($_POST['update'])) { 
                        
    $update mysql_query("UPDATE customers SET c_dob='$_POST[cdob]' WHERE customer_ref='$url'"); 

    header("Location: whateverthispageiscalled.php");
    exit();
                        } 
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:

    Quote Originally Posted by spikeZ View Post
    Use header() to redirect the user once the database has been updated
    Hoy, that's what I said!


    Here's a simpler example.

    PHP Code:
    <?php
    function getUser($id){
      return array(
        
    'id'        => 3,
        
    'username'  => 'AnthonySterling'
      
    );
    }

    $user getUser($_GET['id']);

    #if we're updating
    if('POST' === $_SERVER['REQUEST_METHOD']){
      
    #update
      
    $sql sprintf(
        
    "UPDATE user SET username = '%s' WHERE id = %d LIMIT 1;",
        
    $user['username'],
        
    $user['id']
      );
      
    #redirect
      
    header('Location: http://www.example.org/update.php'true302);
      exit;
    }

    ?>
    <html>
      <head>
        <title>Demo</title>
      </head>
      <body>
        <form action="update.php?id=<?php echo $user['id']; ?>" method="post">
          <input type="text" name="username" value="<?php echo $user['username']; ?>" />
          <input type="hidden" name="id" value="<?php echo $user['id']; ?>" />
          <input type="submit" value="update" />
        </form>
      </body>
    </html>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #5
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    151 Post(s)
    Tagged
    3 Thread(s)
    Great minds Anthony, great minds
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  6. #6
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    ... but fools seldom differ.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  7. #7
    SitePoint Evangelist
    Join Date
    Aug 2010
    Posts
    503
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey chaps, the header doesn't seem to want to redirect to another page. I have an exit() underneath. When I submit the update, the background image loads but that's all which I'm presuming is because of the exit. Any ideas why the header isn't working?

  8. #8
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Just so we're clear, you're not redirecting to another page, it's the same one.

    Check your logic/flow, if the exit is begin reached for a standard 'get' request, it's wrong.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  9. #9
    SitePoint Evangelist
    Join Date
    Aug 2010
    Posts
    503
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AnthonySterling View Post
    Just so we're clear, you're not redirecting to another page, it's the same one.

    Check your logic/flow, if the exit is begin reached for a standard 'get' request, it's wrong.
    Hey, no I want to redirect the same page, have the fields disabled (which is working perfectly) so the user can see their changes. But it's refreshing with the same results until you load it once more and then changes to the data the user has just entered.

  10. #10
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Can you post your modified code?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  11. #11
    SitePoint Evangelist
    Join Date
    Aug 2010
    Posts
    503
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey dude, I feel like a right spanner, I just need to move the following piece of code below the database update, so now it's all good. Cheers for help buds

    PHP Code:
    $query mysql_query("SELECT * FROM customers WHERE customer_ref='$url'");
                
    $res mysql_fetch_array($query); 


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
  •