Saves Posted August 27, 2013 Share Posted August 27, 2013 Hello everyone, I am currently coding what I thought would be a simple PHP button that would alternate each time you click it to provide a different action. I've spent almost 2 hours trying to figure this out but I'm stumped at this point. I need your help.. Here is what I have so far: the PHP: <?php session_start(); $pause = 1; $btnname = "Pause"; $btntype = "pause"; if (isset($_POST['button'])) { if (isset($pause)) { unset($pause, $btnname, $btntype); $play = 1; $btnname = "Play"; $btntype = "play"; } else if (isset($play)) { unset($play, $btnname, $btntype); $pause = 1; $btnname = "Pause"; $btntype = "pause"; } ?> The HTML: <a href="#" onclick="var myPlayer = document.getElementById('playerid'); myPlayer.<?php echo $btntype; ?>Video();"><input name="button" type="submit" id="button" value="<?php echo $btnname; ?>" /></a> The Goal is to simple create a button where it will alternate between a "Play" and "Pause" command. Link to comment https://forums.phpfreaks.com/topic/281615-php-button-so-simple-yet-so-complicated-please-help/ Share on other sites More sharing options...
PaulRyan Posted August 27, 2013 Share Posted August 27, 2013 <?PHP session_start(); $pause = 1; $btnname = "Pause"; $btntype = "pause"; if($_SERVER['REQUEST_METHOD'] == 'POST') { //### Debug Data //echo '<pre>'; //print_r($_POST); //echo '</pre>'; $postedButton = isset($_POST['button']) ? strtolower($_POST['button']) : '' ; if($postedButton == 'pause') { $play = 1; $btnname = "Play"; $btntype = "play"; } else if($postedButton == 'play') { $pause = 1; $btnname = "Pause"; $btntype = "pause"; } } ?> <form method="POST"> <a href="#" onclick="var myPlayer = document.getElementById('playerid'); myPlayer.<?php echo $btntype; ?>Video();"> <input name="button" type="submit" id="button" value="<?php echo $btnname; ?>" /> </a> </form> Link to comment https://forums.phpfreaks.com/topic/281615-php-button-so-simple-yet-so-complicated-please-help/#findComment-1447080 Share on other sites More sharing options...
Saves Posted August 28, 2013 Author Share Posted August 28, 2013 <?PHP session_start(); $pause = 1; $btnname = "Pause"; $btntype = "pause"; if($_SERVER['REQUEST_METHOD'] == 'POST') { //### Debug Data //echo '<pre>'; //print_r($_POST); //echo '</pre>'; $postedButton = isset($_POST['button']) ? strtolower($_POST['button']) : '' ; if($postedButton == 'pause') { $play = 1; $btnname = "Play"; $btntype = "play"; } else if($postedButton == 'play') { $pause = 1; $btnname = "Pause"; $btntype = "pause"; } } ?> <form method="POST"> <a href="#" onclick="var myPlayer = document.getElementById('playerid'); myPlayer.<?php echo $btntype; ?>Video();"> <input name="button" type="submit" id="button" value="<?php echo $btnname; ?>" /> </a> </form> Hey I really appreciate your help! I tried imputing this code and it didn't work. The button didn't even have a Value in it and when I clicked it, it would just reload the page and the song would play again from the beginning. Link to comment https://forums.phpfreaks.com/topic/281615-php-button-so-simple-yet-so-complicated-please-help/#findComment-1447082 Share on other sites More sharing options...
jcbones Posted August 28, 2013 Share Posted August 28, 2013 I see no reason why that code would not work. Un-comment the debugging section, and then run the code, and/or look at the page source to see if it holds what it is suppose to. Link to comment https://forums.phpfreaks.com/topic/281615-php-button-so-simple-yet-so-complicated-please-help/#findComment-1447083 Share on other sites More sharing options...
PaulRyan Posted August 28, 2013 Share Posted August 28, 2013 I did exactly what your previous code looked like it was attempting to do. It seems as though you're trying to change a client side action with a server side output. You need to look into a javascript version of the above code. <script type="text/javascript"> function performAction(){ var button = document.getElementById('actionButton'); var myPlayer = document.getElementById('playerid'); if(button.value.toLowerCase() == 'pause') { button.value = 'Play'; myPlayer.pauseVideo(); } else { button.value = 'Pause'; myPlayer.playVideo(); } } </script> <input type="button" id="actionButton" onClick="performAction();" value="Pause"> Link to comment https://forums.phpfreaks.com/topic/281615-php-button-so-simple-yet-so-complicated-please-help/#findComment-1447134 Share on other sites More sharing options...
Saves Posted August 30, 2013 Author Share Posted August 30, 2013 I did exactly what your previous code looked like it was attempting to do. It seems as though you're trying to change a client side action with a server side output. You need to look into a javascript version of the above code. <script type="text/javascript"> function performAction(){ var button = document.getElementById('actionButton'); var myPlayer = document.getElementById('playerid'); if(button.value.toLowerCase() == 'pause') { button.value = 'Play'; myPlayer.pauseVideo(); } else { button.value = 'Pause'; myPlayer.playVideo(); } } </script> <input type="button" id="actionButton" onClick="performAction();" value="Pause"> Oh my god you are a freaking genius! I cannot express the amount of gratitude and appreciation I have for you right now. I hope you are given everything you want in life! Link to comment https://forums.phpfreaks.com/topic/281615-php-button-so-simple-yet-so-complicated-please-help/#findComment-1447423 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.