SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2010
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Populating fields and php

    Hi,

    Apologies if this sound ammature.

    I am creating a form that has radio buttons and select tags.

    I want some fields to be populated on the previous selection. For example if a radio button is selected, populate fields related to that selection and so on. I have scratched my head a lot on php and MySQL. I m nt a computer person. My question is this :

    Do I have to create the form with all the fields added already and hide them and show then only when th respective selection is done? Is there an alternative to this ? Kindly describe any readings / tutorials for this.

    Regarding the php part, il post a question in the php section.

    I read simply JavaScript by Kevin Yank and Cameron Adams but as I said m nt a computer person so m doin al stupid things ..

    Help appreciated !!

    Thanks

  2. #2
    SitePoint Addict
    Join Date
    Jul 2006
    Location
    Fionnphort, Isle of Mull, Scotland
    Posts
    334
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    The usual way to pre-populate a form field is to set the value to be a PHP variable.
    HTML Code:
    <input type='text' name='name' id='name' value='<?php echo $value; ?>' />
    Where you create the variable is up to you. It can be from a previous showing of the form, or a default value generated in your PHP, etc.

    For <select> and radio buttons you can pre-select a default using 'selected' like this:
    HTML Code:
    <select name='status' id='status' >
    	<option value='prov' selected='selected'>Provisional</option>
    	<option value='canc'>Cancel</option>
    	<option value='closed'>Closed</option>
    </select>
    (I have read that it's no longer necessary to use "selected='selected'", you can just use "selected", but I've not proved this for myself).
    If you want to change the default selection of <select> and radio buttons you have to deal with the selectedIndex property (using JavaScript).

    It's not clear to me whether you want to pre-set the selects and radio buttons, or take the user selections and use them to put values into other form fields (such as text inputs or text areas) ? If you can tell me a bit more I'll try to answer more fully.
    Tim Dawson
    Isle of Mull, Scotland

  3. #3
    Barefoot on the Moon! silver trophy
    Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,516
    Mentioned
    51 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by ramasaig View Post
    (I have read that it's no longer necessary to use "selected='selected'", you can just use "selected", but I've not proved this for myself).
    Yep, you can safely use the selected attribute without a value.
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  4. #4
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,313
    Mentioned
    178 Post(s)
    Tagged
    8 Thread(s)
    Quote Originally Posted by thecoder777 View Post
    I want some fields to be populated on the previous selection. For example if a radio button is selected, populate fields related to that selection and so on. I have scratched my head a lot on php and MySQL. I m nt a computer person. My question is this :

    Do I have to create the form with all the fields added already and hide them and show then only when th respective selection is done? Is there an alternative to this ? Kindly describe any readings / tutorials for this.
    If you want stuff to happen dynamically when you click a radio button (for example), you'll need to use JavaScript for that.
    PHP is interpreted on the server and will just serve up HTML to your browser, so cannot by definition, cause anything dynamic to happen on your page.

    If this is what you want, it would be great if you could go into a little more detail.
    If this is not what you want, then just forget I spoke

  5. #5
    SitePoint Addict
    Join Date
    Jul 2006
    Location
    Fionnphort, Isle of Mull, Scotland
    Posts
    334
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks, Force Flow. I never seem to get around to testing it.
    Tim Dawson
    Isle of Mull, Scotland

  6. #6
    SitePoint Evangelist bronze trophy
    Join Date
    Feb 2013
    Posts
    500
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    You can do a little fake-out where you just use javascript to submit the form when a radio is selected but only do full processing if button is submitted. Here's a rough sample.
    PHP Code:
    <?php

    if (isset($_POST['submit_choice'])){
        echo 
    "<pre>";
        
    print_r($_POST);
        echo 
    "</pre>";



    //Set vategory as Checked
    $cat_car = (isset($_POST['category']) && $_POST['category'] == "car" " checked=\"checked\"" '');
    $cat_truck = (isset($_POST['category']) && $_POST['category'] == "truck" " checked=\"checked\"" '');
    $cat_van = (isset($_POST['category']) && $_POST['category'] == "van" " checked=\"checked\"" ''); 
    //
    $category = (isset($_POST['category']) ? "{$_POST['category']}''); 
    // 
    $vehicles = array(
    "car" => array("Select","Acura","Buick","Cadillac","Chevrolet","Dodge","Ferrari","Ford","Jaguar","Jeep","Mercedes","Pontiac"),
    "truck" => array("Select","Chevrolet","Dodge","Ford","Toyota"),
    "van" => array("Select","Mazda Mazda5","Chrysler Town &amp; Country","Toyota Sienna","Honda Odyssey")
    );

    $content "";
    ?>
    <html>
    <body>
    <?php
    $content 
    .= "<form method=\"post\" action=\"\" name=\"categoryform\">
    Car <input type=\"radio\" name=\"category\" value=\"car\" onclick=\"document.categoryform.submit ();\"
    $cat_car /> 
    Truck <input type=\"radio\" name=\"category\" value=\"truck\" onclick=\"document.categoryform.submit ();\"
    $cat_truck /> 
    Van <input type=\"radio\" name=\"category\" value=\"van\" onclick=\"document.categoryform.submit ();\"
    $cat_van /><br />

    <select name=\"vehicle\">\r"
    ;

    if (isset(
    $_POST['category'])){
        foreach(
    $vehicles[$category] as $vehicle){
            
    $content .= "<option value=\"$vehicle\">$vehicle</option><br />\r";
        }
    }else{
        
    $content .= "<option value=\"\">Choose Model</option><br />\r";
    }
    $content .= "</select>
    <input type=\"submit\" name=\"submit_choice\" value=\"Submit\" />
    </form>\r"

      
    if (isset(
    $content)){ echo "$content";} 
    ?>
    </body>
    </html>

  7. #7
    SitePoint Addict bronze trophy
    Join Date
    Apr 2013
    Location
    Ithaca
    Posts
    338
    Mentioned
    6 Post(s)
    Tagged
    1 Thread(s)
    I've made a PHP GUI API, you may want to try it out if you wish to implement your view layer in PHP rather than HTML. Note this version is not namespaced, so you may end up experiencing some naming conflicts with your own classes:
    http://www.phpclasses.org/package/78...matically.html

  8. #8
    SitePoint Enthusiast
    Join Date
    Nov 2010
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you all for all ur help !! I got it !!! Thanks


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
  •