erinod Posted September 29, 2011 Share Posted September 29, 2011 Hi all, I am trying to replicate/modify this checkbox for a different script and I don't understand what the php code is doing in this form? Could someone explain to me whats happening by including that php code in the form? <input type="checkbox" onclick="showArchive()" NAME="showarchive" id="showarchive" <?php echo ($showarchive ? 'checked="checked"' : '');?> /> Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/248123-beginner-what-is-this-php-code-doing-inside-this-form/ Share on other sites More sharing options...
AyKay47 Posted September 29, 2011 Share Posted September 29, 2011 if the variable $showarchive exists, then the checkbox will be checked by default, else it will not be chekced Quote Link to comment https://forums.phpfreaks.com/topic/248123-beginner-what-is-this-php-code-doing-inside-this-form/#findComment-1274107 Share on other sites More sharing options...
AbraCadaver Posted September 29, 2011 Share Posted September 29, 2011 Ternary operator: http://us2.php.net/ternary (expr1) ? (expr2) : (expr3) evaluates to expr2 if expr1 evaluates to TRUE, and expr3 if expr1 evaluates to FALSE. Quote Link to comment https://forums.phpfreaks.com/topic/248123-beginner-what-is-this-php-code-doing-inside-this-form/#findComment-1274113 Share on other sites More sharing options...
Maq Posted September 29, 2011 Share Posted September 29, 2011 It's equivalent to: if($showarchive) { echo "checked='checked'"; } else { echo ""; } There's also an example in the manual link AbraCadaver provided in Example #2. Quote Link to comment https://forums.phpfreaks.com/topic/248123-beginner-what-is-this-php-code-doing-inside-this-form/#findComment-1274120 Share on other sites More sharing options...
erinod Posted September 29, 2011 Author Share Posted September 29, 2011 So would this piece of code cause the checkbox to remain checked on page reloads? Is it possible that the user checks the box once, thereby creating the $showarchive variable, and that this piece of code then keeps the checkbox selected during the rest of their session? I also am trying to figure out how these pieces of scripts are interacting with one another: Here's the form: <FORM ACTION="?m=quotes" METHOD="post" NAME="quoteForm" ID="quoteForm"> <B> Show Archived: </B><input type="checkbox" onclick="showArchive()" NAME="showarchive" id="showarchive" <?php echo ($showarchive ? 'checked="checked"' : '');?> /> And this seems to be where $showarchive is defined: if (isset( $_GET['showarchive'] )) { if($_GET['showarchive'] == "noshow"){ $showarchive = ''; $show = "noshow"; $AppUI->setState( 'QuoteIdxCond', 'noshow'); } else{ $showarchive = "show"; $show = "show"; $AppUI->setState( 'QuoteIdxCond', 'show'); } } $show = $AppUI->getState( 'QuoteIdxCond' ) ? $AppUI->getState( 'QuoteIdxCond' ) : ''; if($show == "show") $showarchive = "show"; else $showarchive = ''; Even though the form uses post, can this script use $_GET to call the data from the form? That is my understanding of what is happening, but I wasn't sure you could do that or why it was written this way. Then the same script that holds the form also has a Javascript function that is called to with onClick: function showArchive() { f = document.quoteForm.showarchive.checked; if(f == true) window.location =('./index.php?m=quotes&showarchive=show'); else // window.location =('./index.php?m=quotes&showarchive=noshow'); window.location =('./index.php?m=quotes&showarchive=noshow'); } What I don't understand here is how the url's are defined. I'm trying to copy this whole process to have a checkbox like this one on a different page which filters a search so I'm just trying to understand how this works. Thanks for any further help Quote Link to comment https://forums.phpfreaks.com/topic/248123-beginner-what-is-this-php-code-doing-inside-this-form/#findComment-1274203 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.