JudgementDay Posted February 1, 2012 Share Posted February 1, 2012 I am trying to make it so if a PHP session entity exists, then JavaScript will enable a button. I am having trouble getting this to work and wondering if anyone knows what I am doing wrong? function purchaseEnable() { if ($_SESSION['order']['paymentmethod']) { document.getElementById("purchase").disabled = false; } } <INPUT disabled id="purchase" type="submit" value="PURCHASE"> Quote Link to comment https://forums.phpfreaks.com/topic/256167-button-wont-enable/ Share on other sites More sharing options...
UrbanDweller Posted February 1, 2012 Share Posted February 1, 2012 Try AJAX, Im not totaly sure but js and php dont work hand in hand like that. Quote Link to comment https://forums.phpfreaks.com/topic/256167-button-wont-enable/#findComment-1313219 Share on other sites More sharing options...
JudgementDay Posted February 1, 2012 Author Share Posted February 1, 2012 Ow I C... Then I'm going to have to get tricky arn't I LOL! function purchaseEnable() { <?php if ($_SESSION['order']['paymentmethod']) { echo 'document.getElementById("purchase").disabled = false;'; ?> } } Not test, but will do now until I get it working. lol if I'm not back in an hour, this is solved. Thanks for your information. Quote Link to comment https://forums.phpfreaks.com/topic/256167-button-wont-enable/#findComment-1313225 Share on other sites More sharing options...
JudgementDay Posted February 1, 2012 Author Share Posted February 1, 2012 I am now using this: <?php if ($_SESSION['order']['content']) { echo ' <SCRIPT type="text/javascript"> function purchaseEnable() { if (document.getElementById("agree") == "Yes") { document.getElementById("purchase").disabled = false; } } </SCRIPT> '; } ?> <SELECT id="agree"> <OPTION></OPTION> <OPTION>Yes</OPTION> </SELECT> <INPUT disabled id="purchase" type="submit" value="PURCHASE"> Its still not working. I take it I have stuffed up with JavaScript some where and trying to do something thats not allowed. Can anyone see a mistake? Quote Link to comment https://forums.phpfreaks.com/topic/256167-button-wont-enable/#findComment-1313229 Share on other sites More sharing options...
UrbanDweller Posted February 1, 2012 Share Posted February 1, 2012 are u sure that "disabled" attribute works correctly? Quote Link to comment https://forums.phpfreaks.com/topic/256167-button-wont-enable/#findComment-1313235 Share on other sites More sharing options...
UrbanDweller Posted February 1, 2012 Share Posted February 1, 2012 Heres a way to check select options and only alerts if yes is selected <html> <head> <title>Untitled Document</title> <script type="text/javascript"> window.onload = function(){ var s1 = document.getElementById("s1") s1.onchange = function(){ for(var i = 0; i < s1.options.length; i++){ if(s1.options[i].selected && s1.options[i].value == "yes"){ value = s1.options[i].value; alert(value); } } } } </script> </head> <body> <form> <select id="s1"> <option>---</option> <option value="yes">yes</option> <option>no</option> </select> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/256167-button-wont-enable/#findComment-1313247 Share on other sites More sharing options...
JudgementDay Posted February 1, 2012 Author Share Posted February 1, 2012 Thanks, but that won't be necessary. I am now 100% the problem lays with this line: if (document.getElementById("agree").value == "Yes") { When I remove the above piece of code, the button is no longer disabled, and everything else is work so this must be it. Now the big question... why isn't it working? Here is all the code it works in conjunction with. Maybe someone who is more experienced than me can spot something I can't. <SCRIPT type="text/javascript"> if (document.getElementById("agree").value == "Yes") { document.getElementById("purchase").disabled = false; } </SCRIPT> <FORM action="processorder/" method="post"> <DIV> Do you agree to the terms of sale? <SELECT id="agree"> <OPTION></OPTION> <OPTION>Yes</OPTION> </SELECT> <INPUT disabled id="purchase" type="submit" value="PURCHASE"> </DIV> </FORM> Quote Link to comment https://forums.phpfreaks.com/topic/256167-button-wont-enable/#findComment-1313252 Share on other sites More sharing options...
UrbanDweller Posted February 1, 2012 Share Posted February 1, 2012 Your issue is that the statement below the element doesnt have a value as you have targetted the select tag and not the options tags inside it do. if (document.getElementById("agree").value == "Yes") just implement my snippet of code into your script and it will work. Basically it runs through all the option tags inside the select with a for loop and IF the option is selected and the value is value yes only then will the code go on or in my case alert u. Quote Link to comment https://forums.phpfreaks.com/topic/256167-button-wont-enable/#findComment-1313255 Share on other sites More sharing options...
JudgementDay Posted February 1, 2012 Author Share Posted February 1, 2012 You are correct in why it is not working! How can I get it working? Thanks for taking the time to show me that snippet, but its function is not what I am wanting. Quote Link to comment https://forums.phpfreaks.com/topic/256167-button-wont-enable/#findComment-1313257 Share on other sites More sharing options...
UrbanDweller Posted February 1, 2012 Share Posted February 1, 2012 Ok, i assumed all you wanted to do now was retrieve a value from a option tag and check the value is yes? I just gave you a dynamic way of check but you can make it more direct by removing the for loop. PS, More direct approach by selecting option from its array. var s1 = document.getElementById("s1") s1.onchange = function(){ if (s1.options[1].selected){ var optionVar = s1.options[1].value; }else{ var optionVar = null; } } The 1 corresponds the position of the option inside the select tag starting a 0. Quote Link to comment https://forums.phpfreaks.com/topic/256167-button-wont-enable/#findComment-1313266 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.