Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: Dynamic Content - Quick Script

  1. #1
    Join Date
    Feb 2007
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Dynamic Content - Quick Script

    All i need is a script to check a set of radio buttons and show content depending on what one is currently selected.

    Sounds Easy and i hope some one has or could make a script.


    Thanks Alot

  2. #2
    Join Date
    Feb 2007
    Posts
    116
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Could you be a little more specific? Do you want the radio buttons and the content that changes to be on the same page? Or do you want the radio buttons in a form, where the content is displayed after the form is submitted? Also, does your server support php?
    "Rock and roll ain't noise pollution." - AC/DC

    http://www.blake-foster.com

  3. #3
    Join Date
    Feb 2007
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Woops my bad,
    i want it to be able to show it on the same page without submitting the form.
    And yes my server support php.
    Thanks

  4. #4
    Join Date
    Feb 2007
    Posts
    116
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Here's one way of doing it.

    Code:
    <html>
    <head>
    <title>radio button example</title>
    </head>
    <body>
    
    <?php
    
    $numChoices = 6;
    $choices = array(6);
    $choices[0] = "choice 0";
    $choices[1] = "choice 1";
    $choices[2] = "choice 2";
    $choices[3] = "choice 3";
    $choices[4] = "choice 4";
    $choices[5] = "choice 5";
    
    echo '<form name="form1" action="' . $_SERVER['PHP_SELF'] . '" method="post">';
    
    $op = isset($_REQUEST['choice']) ? $_REQUEST['choice'] : "0";
    
    for ($i=0; $i<$numChoices; $i++)
    {
    	echo '<input type="radio" name="choice" value="' . $i . '"';
    	if ((int)$op == $i) echo ' checked';
    	echo ' onchange="document.form1.submit()"> ';
    	echo $choices[$i] . "<br>\r\n";
    }
    
    echo '</form>';
    
    switch ($op)
    {
    	case "0": echo 'You chose 0'; break;
    	case "1": echo 'You chose 1'; break;
    	case "2": echo 'You chose 2'; break;
    	case "3": echo 'You chose 3'; break;
    	case "4": echo 'You chose 4'; break;
    	case "5": echo 'You chose 5'; break;
    }
    
    ?>
    
    </body>
    </html>
    Last edited by Blake; 02-28-2007 at 09:45 PM.
    "Rock and roll ain't noise pollution." - AC/DC

    http://www.blake-foster.com

  5. #5
    Join Date
    Aug 2005
    Location
    Other Side of My Monitor
    Posts
    3,494
    Thanks
    5
    Thanked 105 Times in 104 Posts
    Blog Entries
    1

    Default

    There is also the Chained Selection

    It's not radio buttons, but can be adapted to do so.

    (nice looking script Blake, you just whip that up?)
    {CWoT - Riddle } {Freelance Copywriter} {Learn to Write}
    Follow Me on Twitter: @InkingHubris
    PHP Code:
    $result mysql_query("SELECT finger FROM hand WHERE id=3");
    echo 
    $result

  6. #6
    Join Date
    Feb 2007
    Posts
    116
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by BLiZZaRD View Post
    There is also the Chained Selection

    It's not radio buttons, but can be adapted to do so.

    (nice looking script Blake, you just whip that up?)
    Thanks. Yep, I just wrote it.
    "Rock and roll ain't noise pollution." - AC/DC

    http://www.blake-foster.com

  7. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    It isn't really dynamic... just automatically submitting the form.
    He said without submitting.

    However, the need depends on other variables, since now this might work in that the user does not need to click submit, if that's all that was desired.

    To load this into an existing page as part of the page, it would be more difficult.

    Seems like this might be more suited for ajax. Though, if possible to avoid ajax, it makes sense. I'm still not quite sure what is needed.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. #8
    Join Date
    Feb 2007
    Posts
    116
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by djr33 View Post
    It isn't really dynamic... just automatically submitting the form.
    He said without submitting.

    However, the need depends on other variables, since now this might work in that the user does not need to click submit, if that's all that was desired.

    To load this into an existing page as part of the page, it would be more difficult.

    Seems like this might be more suited for ajax. Though, if possible to avoid ajax, it makes sense. I'm still not quite sure what is needed.
    If it's really important that the content be truly dynamic, I would put the content to change in an iframe, and just change the source to point to one of several files when a button is clicked. Trying to change the html of the whole page would be a nightmare, I think.

    I could come up with a script that does that, Reion, if that's what you want.
    "Rock and roll ain't noise pollution." - AC/DC

    http://www.blake-foster.com

  9. #9
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Ajax changing the contents of a div is a very standard practice.
    I don't really know how to do it, but I've seen it plenty.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  10. #10
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    My script here can do it, although it would be better off not using <label> -- I should probably modify it at some point. Blake's idea is the best, though.
    Code:
            echo ' onchange="document.form1.submit()"> ';
    Don't access it as document.form1, access it as document.forms['form1']. The former is rather non-standard.
    Code:
    if ((int)$op == $i)
    If you're going to type-cast manually, you might as well take advantage of it by using ===
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

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
  •