Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Aug 2009
    Location
    McKinleyville, CA
    Posts
    40
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Can radio buttons be added to a form dynamically?

    I am working a webpage which uses MySQL and PHP. On one page vendors could add there business to a list of available firms. On another page a customer could make changes to their account and select a vendor to handle some of their business.

    I could do this with a drop down list. Each interested vendor could go through the steps to have his name stored in the database. But I there will not be a long list of posible vendors, so I would rather use radio buttons.

    The customer can complete the other portions of the form and then select using a radio button to indicate which vendor he would like to use.

    Can radio buttons be added to a from dynamically, along with a label, so as the list of vendors increases, the number of displayed radio buttons would increase also?

  • #2
    bdl
    bdl is offline
    Regular Coder
    Join Date
    Apr 2007
    Location
    Camarillo, CA US
    Posts
    590
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Anything like this must be done using JavaScript in the browser, you can't do it with PHP (or it would be rather difficult to implement and require multiple page requests).

  • #3
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    This form will add radio buttons as the database increases, and only output what has been selected.


    PHP Code:
    <?php

    if($_POST['venders']) {

    echo 
    "You have chosen:<br>";

    $post_result $_POST['venders'];

    foreach (
    $post_result as $company) {
    echo 
    "<br>$company";
    }
    }
    ?>

    <form method="post" action="index.php">
    <hr>

    <?php

    //db and query
    $venders_list = array('Burger King','Fatburger','A&W Burgers','Cheese Burger','In and Out Burgers');
    sort($venders_list);

    foreach (
    $venders_list as $value) {
    echo 
    "<input type=\"checkbox\" name=\"venders[]\" value=\"$value\">$value</input><br>\n";
    }
    ?>
    <br><input type="submit" name="submit" value="Submit">
    </form>
    Last edited by Len Whistler; 01-20-2010 at 07:18 AM.
    Leonard Whistler

  • Users who have thanked Len Whistler for this post:

    Fishhead (01-20-2010)

  • #4
    New Coder
    Join Date
    Aug 2009
    Location
    McKinleyville, CA
    Posts
    40
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Len

    Thank you. I was about to turn the lights outs and go to bed, but check the forum instead. I cut and pasted your code into a blank form and it seems to do what I need. I will work on accessing the database tomorrow, but it sure looks like what I was after. Thanks again.

  • #5
    New Coder
    Join Date
    Aug 2009
    Location
    McKinleyville, CA
    Posts
    40
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Len

    If I can call on you with a follow-up question, it would be greatly appreciated.

    Using the code you provided, I was able to modify the fix set of values in your burger joint array with a variable containing an array from my database. It looks and work great. I removed the sort and I modified the check boxes to radio buttons of the same group, and created the first entry in the database and labeled it "NONE". That way the first in the list is always a choice of "none".

    My question - is there a way to force a checked = "checked" for this value of "none", thus making it the default selection? Right now, none of the buttons are checked, so unless the user checks one, it is the same as having "none" checked, but it would remove any uncertainty about what happens if nothing is done.

    Thanks again for your help.

    Pete

  • #6
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    I would put the first check box outside the loop, and delete that database entry. The syntax might be incorrect below, but it should give you an idea what to do.


    PHP Code:
    <form method="post" action="index.php">
    <input type="checkbox" name="venders" value="none" checked>

    <?php

    // db query loop
    EDIT: This applies to the radio buttons also.



    ------------------
    Last edited by Len Whistler; 01-22-2010 at 08:02 AM.
    Leonard Whistler

  • Users who have thanked Len Whistler for this post:

    Fishhead (01-22-2010)


  •  

    Posting Permissions

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