Professional Web Applications Themes

setting up form fields automatically - PHP Development

Hi, I am trying to set up some form fields based on values queried from a database, in a PHP file. If users have already filled out a form and have an ID, they can enter the ID and have the form filled automaticaaly based on results fetched from the DB (using customer id). I put the <input ...> field where users enter their id in one form that submits to itself and the actual form field (FirstName, LastName,...) in a separate form. There is no problem, I can enter the id and get the results from DB and have ...

  1. #1

    Default setting up form fields automatically

    Hi,
    I am trying to set up some form fields based on values queried from a
    database, in a PHP file.
    If users have already filled out a form and have an ID, they can enter the
    ID and have the form filled automaticaaly based on results fetched from the
    DB (using customer id).
    I put the <input ...> field where users enter their id in one form that
    submits to itself and the actual form field (FirstName, LastName,...) in a
    separate form. There is no problem, I can enter the id and get the results
    from DB and have the fields filled in. The problem is that I can only fill
    in the <inpt...> fields and don't know how to get the checkboxes checked or
    Select boxes show the proper text(option).
    // $cid is customer id that user enters, using an input field called cid
    $cid = $_POST['cid'];
    $query = "SELECT * from Customers WHERE CustomerID=\"$cid\"";
    $result = mysql_query($query);
    if(mysql_numrows($result)>0)
    {
    $result = mysql_fetch_array($result);
    $FirstName = $result['FirstName'];
    $LastName = $result['LastName'];
    ....
    $select_field = $result['value_from_a_select_box'] // Let's say = text2
    $checked_value = $result['value_based_on_a_checkbox_] // if == "yes",
    check the checkbox
    }

    Then in form:
    <input type="text" name="FirstName" size="30" value="<?php echo $FirstName
    ?>" />
    <select name="select_name">
    <option>text1
    <option>text2
    <option>text3
    </select>

    <input name="FR" type="checkbox">

    I really appreciate your help.

    TIA.


    Xerxes Guest

  2. #2

    Default Re: setting up form fields automatically

    Checkboxes and lists requie you to set the checked / selected property.

    So you would need to do something like (In Pseudocode):

    if (thisCheckBoxValueFromDatabase == Selected){
    echo "<input type=\"checkbox\" checked name=\"theName\">\n";
    }else{
    echo "<input type=\"checkbox\" name=\"theName\">\n";
    }


    The same applies for your Selection boxes, only u set the <option
    selected>Blah</option> for selected and <option>Blah</option> for
    unselected.

    - Ali

    "Xerxes" <ashkan57hotmail.com> wrote in message
    news:91970219665e37b1995bc9f1086fc9f8news.teranew s.com...
    > Hi,
    > I am trying to set up some form fields based on values queried from a
    > database, in a PHP file.
    > If users have already filled out a form and have an ID, they can enter the
    > ID and have the form filled automaticaaly based on results fetched from
    the
    > DB (using customer id).
    > I put the <input ...> field where users enter their id in one form that
    > submits to itself and the actual form field (FirstName, LastName,...) in a
    > separate form. There is no problem, I can enter the id and get the results
    > from DB and have the fields filled in. The problem is that I can only fill
    > in the <inpt...> fields and don't know how to get the checkboxes checked
    or
    > Select boxes show the proper text(option).
    > // $cid is customer id that user enters, using an input field called cid
    > $cid = $_POST['cid'];
    > $query = "SELECT * from Customers WHERE CustomerID=\"$cid\"";
    > $result = mysql_query($query);
    > if(mysql_numrows($result)>0)
    > {
    > $result = mysql_fetch_array($result);
    > $FirstName = $result['FirstName'];
    > $LastName = $result['LastName'];
    > ....
    > $select_field = $result['value_from_a_select_box'] // Let's say = text2
    > $checked_value = $result['value_based_on_a_checkbox_] // if == "yes",
    > check the checkbox
    > }
    >
    > Then in form:
    > <input type="text" name="FirstName" size="30" value="<?php echo
    $FirstName
    > ?>" />
    > <select name="select_name">
    > <option>text1
    > <option>text2
    > <option>text3
    > </select>
    >
    > <input name="FR" type="checkbox">
    >
    > I really appreciate your help.
    >
    > TIA.
    >
    >

    Alistair Baillie \(SE2002\) Guest

  3. #3

    Default Re: setting up form fields automatically

    "Alistair Baillie (SE2002)" <abailliecis.strath.ac.uk> wrote in message
    news:409f66c9nntphost.cis.strath.ac.uk...
    > Checkboxes and lists requie you to set the checked / selected property.
    >
    > So you would need to do something like (In Pseudocode):
    >
    > if (thisCheckBoxValueFromDatabase == Selected){
    > echo "<input type=\"checkbox\" checked name=\"theName\">\n";
    > }else{
    > echo "<input type=\"checkbox\" name=\"theName\">\n";
    > }
    Yes, but, syntactically, I would do...

    The field in the DB that represents the Checkbox field on the form would
    contain a 1 (one) or a ZERO (0). 1 = selected, 0 = not selected

    Then in the PHP code...

    (at the top of the FORM)
    // This makes sure that this variable name is still passed and has a
    value
    // Remember, an unSELECTed checkbox is NOT passed back, therefore
    // any existing value in the DB field would NOT be overwritten if this
    // SELECTion is changed.
    echo '<input type="hidden" name="theName" id="theName" value="0">

    (wherever you need this in the form)
    // Create Check box, if this is SELECTed, the HIDDEN object is discarded
    // if this Checkbox in NOT SELECTed, then the HIDDEN object will be sent
    echo '<input type="checkbox" ';

    // If there is a TRUE value (1) in the field, then SELECT this checkbox
    if (thisCheckBoxValueFromDatabase)
    echo ' checked '; // don't forget the space!

    // define name and close checkbox tag
    echo ' name="theName" id="theName" value="1">';

    Hope this helps.

    Walter


    jsWalter Guest

  4. #4

    Default Re: setting up form fields automatically

    Thanks a lot guys. I got it working.

    "jsWalter" <jsWalterKILLtorresSPAM.ws> wrote in message
    news:SwEoc.111$kt2.36newssvr33.news.prodigy.com.. .
    > "Alistair Baillie (SE2002)" <abailliecis.strath.ac.uk> wrote in message
    > news:409f66c9nntphost.cis.strath.ac.uk...
    >
    > > Checkboxes and lists requie you to set the checked / selected property.
    > >
    > > So you would need to do something like (In Pseudocode):
    > >
    > > if (thisCheckBoxValueFromDatabase == Selected){
    > > echo "<input type=\"checkbox\" checked name=\"theName\">\n";
    > > }else{
    > > echo "<input type=\"checkbox\" name=\"theName\">\n";
    > > }
    >
    > Yes, but, syntactically, I would do...
    >
    > The field in the DB that represents the Checkbox field on the form would
    > contain a 1 (one) or a ZERO (0). 1 = selected, 0 = not selected
    >
    > Then in the PHP code...
    >
    > (at the top of the FORM)
    > // This makes sure that this variable name is still passed and has a
    > value
    > // Remember, an unSELECTed checkbox is NOT passed back, therefore
    > // any existing value in the DB field would NOT be overwritten if this
    > // SELECTion is changed.
    > echo '<input type="hidden" name="theName" id="theName" value="0">
    >
    > (wherever you need this in the form)
    > // Create Check box, if this is SELECTed, the HIDDEN object is
    discarded
    > // if this Checkbox in NOT SELECTed, then the HIDDEN object will be
    sent
    > echo '<input type="checkbox" ';
    >
    > // If there is a TRUE value (1) in the field, then SELECT this checkbox
    > if (thisCheckBoxValueFromDatabase)
    > echo ' checked '; // don't forget the space!
    >
    > // define name and close checkbox tag
    > echo ' name="theName" id="theName" value="1">';
    >
    > Hope this helps.
    >
    > Walter
    >
    >

    Xerxes Guest

Similar Threads

  1. Dynamically Adding Form Fields to CF Flash Form
    By craigkaminsky in forum Coldfusion Flash Integration
    Replies: 0
    Last Post: December 15th, 01:07 AM
  2. Populate form values based on previous same form fields
    By Rizyak in forum PHP Development
    Replies: 2
    Last Post: June 11th, 04:27 PM
  3. dynamically setting text in fields
    By mazzamazza webforumsuser@macromedia.com in forum Macromedia Director Lingo
    Replies: 1
    Last Post: October 10th, 02:13 PM
  4. Replies: 1
    Last Post: August 6th, 03:33 AM
  5. How can I update network ( proxy) setting automatically ?
    By Jigar Rasalawala in forum Mac Programming
    Replies: 1
    Last Post: July 16th, 06:11 PM

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
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139