darkfreaks Posted February 24, 2011 Share Posted February 24, 2011 hey i have a swap JS function i wanna use to swap a hidden div with a non hidden div but the problem is i only want to do that if a certain option has been selected from the list like "pick 3" option . any way i would go about this Quote Link to comment https://forums.phpfreaks.com/topic/228729-form-help/ Share on other sites More sharing options...
AbraCadaver Posted February 24, 2011 Share Posted February 24, 2011 No offense, but with over 4,000 posts I would have thought you would have learned to be more descriptive You didn't say what would trigger this action or if you are talking about a dropdown or checkboxes or what. In short though, you would wrap your swap code in an if that checks the value of the control (assuming a dropdown): var e = document.getElementById("idOfControl"); var v = e.options[e.selectedIndex].value; if(v == "pick 3") { // do swap code } As far as I can tell this is a JavaScript question. Quote Link to comment https://forums.phpfreaks.com/topic/228729-form-help/#findComment-1179241 Share on other sites More sharing options...
darkfreaks Posted February 24, 2011 Author Share Posted February 24, 2011 well actually i am looking for a way to do so in PHP. then use the swap function i have in javascript to switch out the div's. Quote Link to comment https://forums.phpfreaks.com/topic/228729-form-help/#findComment-1179245 Share on other sites More sharing options...
AbraCadaver Posted February 24, 2011 Share Posted February 24, 2011 That doesn't make sense. PHP will only see the value of a form control when you submit the form to the PHP script. So then you would just have a PHP if that determines whether to show the DIV or the hidden DIV: if($_POST['myFormField'] == 'pick 3') { //echo div } else { //echo hidden div } Quote Link to comment https://forums.phpfreaks.com/topic/228729-form-help/#findComment-1179249 Share on other sites More sharing options...
darkfreaks Posted February 24, 2011 Author Share Posted February 24, 2011 my swap thing isn't working if($v==" Pick 3 $50 Special ") { swap('hiddenpick3','this.selected','style.display', 'DIV') } Quote Link to comment https://forums.phpfreaks.com/topic/228729-form-help/#findComment-1179262 Share on other sites More sharing options...
KevinM1 Posted February 24, 2011 Share Posted February 24, 2011 Can you show more code? Specifically your swap function? It's hard to tell exactly what you're trying to do. Quote Link to comment https://forums.phpfreaks.com/topic/228729-form-help/#findComment-1179269 Share on other sites More sharing options...
darkfreaks Posted February 24, 2011 Author Share Posted February 24, 2011 i know this works if i use it as a check box. function MM_findObj(n, d) { //v4.01 var p,i,x; if(!d) { d=document; } if((p=n.indexOf("?"))>0&&parent.frames.length) { d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p); } if(!(x=d[n])&&d.all) { x=d.all[n]; } for (i=0;!x&&i<d.forms.length;i++) { x=d.forms[i][n]; } for(i=0;!x&&d.layers&&i<d.layers.length;i++) { x=MM_findObj(n,d.layers[i].document); } if(!x && d.getElementById) { x=d.getElementById(n); } return x; } function MM_changeProp(objName,x,theProp,theValue) { //v6.0 var obj = MM_findObj(objName); if (obj && (theProp.indexOf("style.")==-1 || obj.style)) { if (theValue == true || theValue == false) { eval("obj."+theProp+"="+theValue); } else { eval("obj."+theProp+"='"+theValue+"'"); } } } function swap(name, mark,theProp){ if (mark) { var theValue = 'inline'; } else { var theValue = 'none'; } var obj = MM_findObj(name); if (obj && (theProp.indexOf("style.")==-1 || obj.style)) { eval("obj."+theProp+"='"+theValue+"'"); } } Quote Link to comment https://forums.phpfreaks.com/topic/228729-form-help/#findComment-1179272 Share on other sites More sharing options...
KevinM1 Posted February 24, 2011 Share Posted February 24, 2011 Ugh, Dreamweaver code. Yuck. I'm still not 100% certain how you're expecting PHP to do this. It would take either a page refresh or Ajax to have PHP determine which div to show. Remember, when your page is rendered on the screen, PHP is done running. It doesn't have interactive capabilities like JavaScript. Quote Link to comment https://forums.phpfreaks.com/topic/228729-form-help/#findComment-1179277 Share on other sites More sharing options...
darkfreaks Posted February 24, 2011 Author Share Posted February 24, 2011 yeah i found a thing in jquery that is ammost identical but it doesnt work. the idea is to get something that on select if a certain value will show a hidden div. Quote Link to comment https://forums.phpfreaks.com/topic/228729-form-help/#findComment-1179278 Share on other sites More sharing options...
KevinM1 Posted February 24, 2011 Share Posted February 24, 2011 Which is what AbraCadaver described.... Are you wanting to do this in PHP because you don't know JavaScript? I ask because I'm getting a strong "I don't know JavaScript, so I'm going to throw a bunch of JS scripts at the wall and pray one of them sticks" vibe from this whole thing. To flesh out AC's example, it should be as simple as: var selectElem = document.getElementById('mySelect'); selectElem.onchange = function(){ var value = this.options[this.selectedIndex].value; if(value === /* something */){ // toggle div } } Assuming you're using an actual HTML select element/drop down. Quote Link to comment https://forums.phpfreaks.com/topic/228729-form-help/#findComment-1179282 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.