SitePoint Sponsor

User Tag List

Results 1 to 11 of 11

Thread: If true?

  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    If true?

    How would i make it so that if i have this following bit of form:

    Code:
    <form>
    <input type="radio" name="sex" value="male"> Male
    <br>
    <input type="radio" name="sex" value="female"> Female
    </form>
    how can i put in PHP that if the male radio button is selected then a something will show else if the female is selected then the thing won't show..

    any help?

  2. #2
    Wadge! F4nat1c's Avatar
    Join Date
    Oct 2005
    Location
    South Wales, UK
    Posts
    1,134
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is Javascript, because the actions are script-side. In other words, no data needs to be submitted and processed by another server (server-side).Post in the Javascript forums if you're failing to get a response.
    OMFG SitePoint ROXORZ TEH BIG ONE111!
    Wish you were invisible?

  3. #3
    SitePoint Wizard silver trophy Jelena's Avatar
    Join Date
    Feb 2005
    Location
    Universum, 3rd Corner
    Posts
    3,000
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Little bit of javascript maybe?
    Here it is:

    Code:
    <form>
    <input type="radio" name="sex" id="malecheck" value="male" onclick="showHide('show_me', 1);" checked="checked"> Male
    <br>
    <input type="radio" name="sex" value="female" onclick="showHide('show_me', 0);"> Female
    </form>
    
    <div id="show_me">
    Hello world
    </div>
    
    <script>
    if(document.getElementById('malecheck').checked)
    {
    	showHide('show_me', 1);	
    }
    else
    {
    	showHide('show_me', 0);	
    }
    </script>
    
    <script>
    function showHide(elementID, show)
    {
    	var el = document.getElementById(elementID);
    
    	if(show)
    	{
    		el.style.display = '';
    	}
    	else
    	{
    		el.style.display = 'none';
    	}
    
    	return false;
    }
    </script>
    -- Jelena --

  4. #4
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what if i had a link i wanted to show if one was checked and didn't want to show it if the other was checked.. how would i have this in a PHP file?

  5. #5
    SitePoint Enthusiast BarnYard's Avatar
    Join Date
    Feb 2006
    Location
    Istanbul
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This maybe just a bit off topic, but you might find it useful for error msging and/or validating. As well, you can modify it to accomplish what you need in terms of functionality. You can achieve what you request by implementing an if/else block.

    Code:
    in the form header:
    if(isset($Submit)){
    	$sex = $_POST['sex'];
    	
    	if (trim($sex)=='' || trim($sex) == 'male')
            $arrErrors['sex'] = 'Do something, maybe write this error to page';
    
    	//process form
    	if (count($arrErrors) == 0) {
    		header("location: nextpage.php");
    	} else if (count($arrErrors) == 0) {
    		//do something here on current page
    	}
    }
    
    
    in the form body:
    <form id="form" name="form" method="post" action="<?php echo $PHP_SELF; ?>">
    
    	<div class="whatever">
    	<span class="sexQuestion">3. Would you be willing to relocate to our area?</span><br/>
        <input type="radio" class="radio" id="sex" name="sex" value="male" <?php if ($_POST["sex"]=="male") { echo 'checked'; } ?> >male
        <input type="radio" class="radio" id="sex" name="sex" value="female" <?php if ($_POST["sex"]=="female") { echo 'checked'; } ?> >female<br/>
    	<?php if (!empty($arrErrors['sex'])) echo '<span class="error">'.$arrErrors['sex'].'</span>'; ?>
    	</div>
    	
    	<input type="submit" id="Submit" name="Submit" value="Submit"/>
    </form>

  6. #6
    SitePoint Wizard silver trophy Jelena's Avatar
    Join Date
    Feb 2005
    Location
    Universum, 3rd Corner
    Posts
    3,000
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    As same as in the example I posted above, just give id to the link you want to show/hide and change the rest of the code accordingly.
    As F4nat1c said, this is not a php issue, but javascript. You can do it with php also, but then you will have to submit the form on every change of radios.
    -- Jelena --

  7. #7
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well i have a phpbb forum and on that the radios in preferences change and stay changed until you come back and change them... but when i added radio buttons once i submitted once.. and returned to page was selection had gone and both radios were blank.. why is this?

  8. #8
    SitePoint Wizard silver trophy Jelena's Avatar
    Join Date
    Feb 2005
    Location
    Universum, 3rd Corner
    Posts
    3,000
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Well, you have to save the selection somewhere, in session or db..
    If you save it in session for example: $_SESSION['sex']

    PHP Code:
    <?php
    session_start
    ();
    if(isset(
    $_SESSION['sex']))
    {
        
    $sex $_SESSION['sex'];
        unset(
    $_SESSION['sex']);
    }
    ?>
    <form>
    <input type="radio" name="sex" id="malecheck" value="male" onclick="showHide('show_me', 1);" <?php echo((!isset($sex) || (isset($sex) && $sex == "male")) ? "checked""");?>> Male
    <br>
    <input type="radio" name="sex" value="female" onclick="showHide('show_me', 0);" <?php echo((isset($sex) && $sex == "female") ? "checked""");?>> Female
    </form>
    This is just an example..
    -- Jelena --

  9. #9
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    would it be different if i had a checkbox?

  10. #10
    SitePoint Wizard silver trophy Jelena's Avatar
    Join Date
    Feb 2005
    Location
    Universum, 3rd Corner
    Posts
    3,000
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Not really, no.
    Btw, with a checkbox, user would be able to select both options, male and female, which is a nonsense, so, radio is perfect for this kind of question.
    -- Jelena --

  11. #11
    SitePoint Enthusiast
    Join Date
    Sep 2005
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jelena
    Btw, with a checkbox, user would be able to select both options, male and female, which is a nonsense, so, radio is perfect for this kind of question.
    Well i know that but i would just have the one option so either its selected or it isnt


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
  •