SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    May 2005
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Displaying and Editing data using only one php form.

    Can anyone direct me to an example of how to create a php form but the trick is to use the same form for displaying, editing, and saving the data that you have in each field. I have looked high and low and can't find an example of this.

    Thanks.....

  2. #2
    get into it! bigduke's Avatar
    Join Date
    May 2004
    Location
    Australia
    Posts
    847
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmmm I just did this on my new app.
    Displaying
    have the user click on some icon or maybe the data row or any event for that matter could trigger a remote operation through an iframe which then retrieves the data and populates the fields using javascript. You might want to set the fields as readonly on user event

    Editing
    Same procedure as above, only in this case the form points to the iframe for its "action". The processing is pretty much like normal form processing. However you might need some way to inform the user of the operation's success/failure.

    Its difficult to post the code here since there's tons of it because of a lot of javascripting along with php. So you'll have to follow the lead and figure out the what-to-use yourself now that you have the how-to

  3. #3
    SitePoint Member
    Join Date
    May 2005
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the feedback but there is bound to be a php example out there somewhere. Anyone ???

  4. #4
    SitePoint Guru mwolfe's Avatar
    Join Date
    Mar 2005
    Posts
    912
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if you are really looking for an example here is one that i dug up.. i don't know if you'll be able to follow it though. Its actually two forms (only one gets displayed) which depends on whether the user is registered or not. If they have registered then it shows them their preson information. If they have not registered it shows them a register form.

    PHP Code:
    <?php
    include_once ("./include/include.php");
    include_once (
    "./include/pre.php");
    require_once (
    "./include/users_profile_class.php");
    require_once (
    "./include/messages.inc.php");

    // Date in the past
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

    // always modified
    header("Last-Modified: " gmdate("D, d M Y H:i:s") . " GMT");
     
    // HTTP/1.1
    header("Cache-Control: no-store, no-cache, must-revalidate");
    header("Cache-Control: post-check=0, pre-check=0"false);

    // HTTP/1.0
    header("Pragma: no-cache");

    $page_name USER_INFO_PAGE;

    $profile_page = new Users_profile;
    $profile_page->access_page();
    $profile_page->page_access_level GUEST_LEVEL;
    $profile_page->get_user_info(); //this gets the user info
    $access_level $profile_page->user_access_level;
    $id $profile_page->id;

    $using_session=false;
    if (
    $access_level == GUEST_LEVEL) { 
        
    $using_session true;
        
    $id $profile_page->guest;
        
    $key $profile_page->guest_key;
    } else {
        
    $user_full_name $profile_page->user_full_name;
        
    $user_email $profile_page->user_email;
        
    $user_name $profile_page->user;
        
    $profile_page->get_profile_data();
        
    $fiance_types = array("Groom""Bride");
        
    $fiance         =    !empty($profile_page->fiance_type) ? $profile_page->fiance_type "Fiance";
    }
        
    if (isset(
    $_POST['register'])) {
       
    $profile_page->register_user(
           
    $_POST['user_name'],
           
    $_POST['password1'],
           
    $_POST['password2'],
           
    $_POST['real_name'],
           
    $_POST['email']);
    }
    $error $profile_page->the_msg//error message
    if (isset($_GET['message'])) {
       
    $error get_err_message($_GET['message']);
    }


    if (isset(
    $_POST['submit'])) { //this will update all of the information for a logged in user
        
    $profile_page->save_profile_data($profile_page->profile_id,
                                
    $_POST['address'],
                                
    $_POST['zip'],
                                
    $_POST['city'],
                                
    $_POST['phone'],
                                
    $_POST['notes'],
                                
    $_POST['wedding_date'],
                                
    $_POST['wedding_time'],
                                
    $_POST['wedding_loc'], 
                                
    $_POST['fiance_name'],
                                
    $_POST['b_or_g']);    
        
    header("Location: " $_SERVER['PHP_SELF']);

    }

        
        
    site_header(SITE_TITLE " - " $page_namefalse); //this generates the script that goes in the head section.
        
    ?>
    <div id="outer">
        <div id="clearheader"></div>
        <div id="left"></div>
        <div id="right"> <?php options_links($using_session); ?></div>
        <div id="centrecontent">
            <div class="mid_inner">
            <?php $which_info $using_session "Registration" "Wedding"?>
                <div class="mid_inner_header"><?php echo $which_info?> Information</div>
                <h3 style="text-align:left; padding-left:12px">Please fill out the following details for your wedding so that
                    proper accommodations can be made in fulfilling your Wedding
                    DJ request. <br /> Please Select "Update My Wedding Information" After Filling out the form.</h3>
                
                <p><b><?php echo (isset($error)) ? $error "&nbsp;"?></b></p>
                <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
                
                <?php
                
    if ($using_session) {
                
    ?>
                    <p>Our Records show that either you have not registered your account yet,
                        or you have not logged in. If you have not logged in yet, please 
                        <a href="login/login.php">Login</a>.  Otherise, fill out the following information
                        and an account will be created for you.
                    </p>
                    <br />
                    <b>Your Real Name</b>
                        <br />
                        <input type='text' name='real_name' value="<?php echo (isset($_POST['user_name'])) ? $_POST['user_name'] : ""?>" size='10' maxlength='15' />
                        <br /><br />
                        <b>Pick a User Name</b>
                        <br />
                        <input type='text' name='user_name' value="<?php echo (isset($_POST['user_name'])) ? $_POST['user_name'] : ""?>" size='10' maxlength='15' />
                        <br /><br />
                        <b>Password:</b>
                        <br />
                        <input type='password' name='password1' value='' size='10' maxlength='15' />
                        <br /><br />
                        <b>Password (again):</b>
                        <br />
                        <input type='password' name='password2' value='' size='10' maxlength='15' />
                        <br /><br />
                        <b>Email (Required - Must be accurate to confirm):</b>
                        <br />
                        <input type='text' name='email' value='<?php echo (isset($_POST['email'])) ? $_POST['email'] : ""?>' size='30' maxlength='50' />
                        <br /><br />
                        <input type='submit' name='register' value='register' />
            
                <?php
                    
    } else {
                
    ?>
                        <p>Your Name: <?php echo (isset($user_full_name)) ? $user_full_name "   " "Not given  "?>&nbsp;<a href='login/changepass.php'>Edit</a><p>
                        
                        <br />
                        <label for="input2_2">Your Fiance is the:</label>
                        <select name='b_or_g' id="input2_2">
                        
                        <?php echo $fiance;
                        for (
    $i=0$i<2$i++) {
                            if ((
    $fiance) == ($fiance_types[$i])) {
                                echo 
    "<option selected=\"selected\" value='$fiance_types[$i]'>$fiance_types[$i]</option>";
                            } else {
                                echo 
    "<option value='$fiance_types[$i]'>$fiance_types[$i]</option>";
                            }
                        }
                        
    ?>
                        </select>
                        <br /><br />
                            <b><?php echo $fiance?>'s Name</b>
                        <br />
                        <input type='text' name='fiance_name' value='<?php echo (isset($profile_page->fiance_name)) ? $profile_page->fiance_name ""?>' size='20' maxlength='35'>
                        <br /><br />
                        <b>Wedding Date (Month-Day-Year)</b>
                        <br />
                        <input type='text' name='wedding_date' value='<?php echo (isset($profile_page->wedding_date)) ? $profile_page->wedding_date ""?>' size='20' maxlength='35'>
                        <br /><br />
                            <b>Wedding Time</b>
                        <br />
                        <input type='text' name='wedding_time' value='<?php echo (isset($profile_page->wedding_time)) ? $profile_page->wedding_time ""?>' size='10' maxlength='25'>
                        <br /><br />
                            <b>Wedding Location</b>
                        <br />
                        <input type='text' name='wedding_loc' value='<?php echo (isset($profile_page->wedding_location)) ? $profile_page->wedding_location"";?>' size='15' maxlength='35'>
                        <br /><br />
                        <h2>Your Contact Information</h2>
                            <b>Phone Number</b>
                        <br />
                        <input type='text' name='phone'' value='<?php echo (isset($profile_page->phone)) ? $profile_page->phone:"";?>' size='10' maxlength='13'>
                        <br /><br />
                            <b>Address</b>
                        <br />
                        <input type='text' name='address' value='<?php echo (isset($profile_page->address)) ? $profile_page->address ""?>' size='20' maxlength='35'>
                            <br /><br />
                            <b>City</b>
                        <br />
                        <input type='text' name='city' value='<?php echo (isset($profile_page->city)) ? $profile_page->city ""?>' size='20' maxlength='35'>
                        <br /><br />
                            <b>Zip Code (5 digit)</b>
                        <br />
                        <input type='text' name='zip' value='<?php echo (isset($profile_page->zipcode)) ? $profile_page->zipcode ""?>' size='5' maxlength='5'>
                        <br /><br />
                            <B> Notes, special requests, songs that you will provide.</B>
                            <br />
                        <textarea name='notes' rows=8 cols=30/><?php echo (isset($profile_page->notes)) ? $profile_page->notes :"";?></textarea>
                        <br /><br />
                        <input type='submit' name='submit' value='Update My Wedding Information'>
                    <?php    
                    

                    
    ?>
                    </form>

                <?php user_options(); ?>
            </div><!-- End Mid_inner Div-->
        </div><!-- End Centre Div-->
        <div id='clearfooter'></div>
    </div><!-- End Outer Div -->

    <?php
            site_footer
    ();
            
    site_nav($page_name);
        
    ?>
            
        </body>
    </html>
    this isnt a great method of doing this though, i would recommend putting edit links next to each item so that items do not "accidently" get updated.

    You basically just echo the information in the textfield if it is available, otherwise the field will be blank. Then when they submit the form, the information should get updated and displayed in the page.. this requires that you run the header function to reload the page after its been updated. You also have to put in the no-cache headers or else it won't work. I had the wierdest thing happen in firefox without these. The form would update, only it would show the previous information, even when i used header to reload the page. If i checked the source code, everything would be correct, only it would dispaly the old information on the page(its pretty strange when the code says one thing and the page says something else).

  5. #5
    SitePoint Member
    Join Date
    May 2005
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks very much MWOLFE! I will adapt this to my program and see how it works .

  6. #6
    SitePoint Guru DenverDave's Avatar
    Join Date
    Feb 2001
    Location
    Denver, Colorado
    Posts
    630
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure if we are on the same track, but I had a client with a fairly complicated form and wished to view data using the same form. I looked and looked for a script that would read a form and convert it to a form that would display the data if available as memory variables. I eventually, modified the form by hand with a bunch of "if (IsSet($fieldname))'s on each field. A little strange to setup, but compared to a pdf form the display is great and very very fast.

  7. #7
    does not play well with others frezno's Avatar
    Join Date
    Jan 2003
    Location
    Munich, Germany
    Posts
    1,391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    as DenverDave mentioned, use isset() for the form values und PHP_SELF as the form's action. And that's about it.
    We are the Borg. Resistance is futile. Prepare to be assimilated.
    I'm Pentium of Borg.Division is futile.Prepare to be approximated.


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
  •