boblan66 Posted November 25, 2010 Share Posted November 25, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/219778-practicing-with-if-statements-to-answer-my-own-question-question/ Share on other sites More sharing options...
btherl Posted November 25, 2010 Share Posted November 25, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/219778-practicing-with-if-statements-to-answer-my-own-question-question/#findComment-1139353 Share on other sites More sharing options...
boblan66 Posted November 25, 2010 Author Share Posted November 25, 2010 Javascript -- Well that's something else I need to learn. Can you provide an example for me that would address this issue. I guess I'm getting a good learning experience out of this! Quote Link to comment https://forums.phpfreaks.com/topic/219778-practicing-with-if-statements-to-answer-my-own-question-question/#findComment-1139363 Share on other sites More sharing options...
boblan66 Posted November 25, 2010 Author Share Posted November 25, 2010 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> Quote Link to comment https://forums.phpfreaks.com/topic/219778-practicing-with-if-statements-to-answer-my-own-question-question/#findComment-1139371 Share on other sites More sharing options...
seanlim Posted November 25, 2010 Share Posted November 25, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/219778-practicing-with-if-statements-to-answer-my-own-question-question/#findComment-1139405 Share on other sites More sharing options...
boblan66 Posted November 25, 2010 Author Share Posted November 25, 2010 Thank you very much for your example. I'm going to try it right now. I learn better by example. Always have. Again thanks. Quote Link to comment https://forums.phpfreaks.com/topic/219778-practicing-with-if-statements-to-answer-my-own-question-question/#findComment-1139575 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.