Jump to content

Practicing with IF statements to answer my own question - question


boblan66

Recommended Posts

when in a form, I wish to build a conditional that if the response to a radio button is a value of 2 (female), it will display an input requesting for users maiden name. If not 2 goes to the next input statement.

 

Here is code I was experimenting with:

 

<html>
<body>
<form action="" name="test"  method='POST'>
<input type="radio" id="sex" value=1 checked><label>Male</label>
<input type="radio" id="sex" value=2><label>Femaleale</label>

    <?php
    $result = "value";
    if ($result == 2)
        echo "<input type='int' id='gradYear' size='3' required>";
        else
        echo "Not a female!"
    ?>

    <input type="submit" value="GO">
</form>
</body>
</html>

 

The code passes debug, however, Not a Female is displayed.

 

My question is - Can I do this and if so, what value do I test against id='sex' or value. I tried each one but gave the same results. I realize that $_POST[sex] would be used after the submit button is clicked. But this has me stumped.

 

Thanks for the assis in advance.

Link to comment
Share on other sites

You should be using name in the html, not id.  id is a unique identifier for a single HTML tag.  Name can be shared between multiple tags.  Then you can address the value using the name in php.

 

But there's another issue - once the radio button is selected, are you going to wait until the form is submitted before displaying the additional input tag?  If you want it to appear right way you will need to use javascript.  It can only be done in php if you don't mind requiring the form to be submitted first.

Link to comment
Share on other sites

Ok I broke out the books on javascript and found a little help on the issue.

 

!. I wish the new field to appear when the user clicks on the Female radio button. Not after the submit button is clicked,

 

2. How does one capture the button being clicked?

 

3. The javascript code below gives me the same results as with the previous php.

<doctype html>
<html>
<body>

<label>Male: </label><input type="radio" name="sex" value=1><label>Male</label>
<label>Female: </label><input type="radio" name="sex" value=2><label>Female</label>

    <script type="text/javascript">
    <!--
     var result = value;

    if(result == 2){
document.write("<label>Madian Name: </label><input type="madianname" name="sex" size="14" autofocus>");
    }
    //-->
</script>

<br>
<label>City:</label><input type="varchar" id="city" placeholder="City" size="14">
<br><br>
<input type="submit" value="GO">
</form>

</body>
</html>

 

Link to comment
Share on other sites

1. you will have to use javascript

 

2. use the onclick or onchange event

 

3.  the javascript code must be triggered by the event and cannot be run "inline" when the page loads.

<html>
<body>
<form name="form1">
<label>Male: </label><input type="radio" name="sex" value=1><label>Male</label>
<label>Female: </label><input type="radio" name="sex" value=2><label>Female</label>
</form>
<div id="maiden_name" style="display:none"><label>Madian Name: </label><input type="madianname" name="sex" size="14" autofocus></div>
<script type="text/javascript">
function maidenNameClick()
{
var checked;
var radio = document.forms['form1'].elements['sex'];
var i;
for(i=0; i<radio.length;i++)
if(radio[i].checked)
	break;

if(radio[i].value==1)
document.getElementById("maiden_name").style.display = "none";
else
document.getElementById("maiden_name").style.display = "block";
}

for(var i=0;document.forms['form1'].elements['sex'].length;i++)
document.forms['form1'].elements['sex'][i].onclick = maidenNameClick;

document.getElementById("maiden_name").style.display = "none";
</script>

<br>
<label>City:</label><input type="varchar" id="city" placeholder="City" size="14">
<br><br>
<input type="submit" value="GO">
</form>

</body>
</html>

 

 

*just realised this is more suited for the javascript forum

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.