retro Posted June 24, 2008 Share Posted June 24, 2008 Hi, I am a complete newbie at JavaScript, so apologies for this more than likely easy-to-answer question! I took a simple script that toggles a button's value based on whether an option box was enabled or disabled (the button also toggled this) and stripped the option box out. Here is what I came up with: <script type="text/javascript" language="javascript"> function toggle() { if (document.getElementById("btnToggle").value == "OFF") { document.getElementById("btnToggle").value="ON" return } if (document.getElementById("btnToggle").value == "ON") { document.getElementById("btnToggle").value="OFF" return } } </script> I then used a SWITCH statement to give $c a value of "OFF" or "ON" based on a field in a table. I then made the button: <input type="button" name="btnToggle" value="<?php echo $c ?>" onclick="toggle()" /> When the page is loaded, the button correctly displays ON or OFF depending on the field. However, nothing happens when I click the button. I am sure this is in the .value == "OFF" part. How can I remedy this? Thanks in advance for any help offered! Link to comment https://forums.phpfreaks.com/topic/111675-help-needed-with-javascript-toggle-button/ Share on other sites More sharing options...
retro Posted June 24, 2008 Author Share Posted June 24, 2008 Oops! I just noticed that I hadn't set the id in the HTML! :-\ It works now - id="btnToggle", same as name. However, when I use $b = $_POST['btnToggle'] after submitting the form, $b doesn't return anything. How can I get around this? *edited* think I'm half asleep today! Link to comment https://forums.phpfreaks.com/topic/111675-help-needed-with-javascript-toggle-button/#findComment-573247 Share on other sites More sharing options...
lemmin Posted June 24, 2008 Share Posted June 24, 2008 An input type=button doesn't send its value. I don't know how you are submitting your form, but I assume you are trying to tell which button has been toggled in the form? You will probably want to use a hidden input and add something to your toggle function like this: document.getElementById("hiddenInput").value = document.getElementById("btnToggle").value; Link to comment https://forums.phpfreaks.com/topic/111675-help-needed-with-javascript-toggle-button/#findComment-573645 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.