NotSureILikePHP Posted August 21, 2015 Share Posted August 21, 2015 Okay so I have a checkbox that by default I want to be checked and when it's checked it only searches for active customers. When you uncheck it it searches for all customers. How do I get the value of the checkbox? I found this snippet of code online: Active: <input type='checkbox' name='chkActive' value='1'> $active = isset($_POST['chkActive']) && $_POST['chkActive'] ? "1" : "0"; However this seems to only check the value on the postback. How do you get the value on load. Is there a document.getElementById equivilant in php or another simple way to just get the value of a checkbox? Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted August 21, 2015 Share Posted August 21, 2015 (edited) That is only way in PHP you can get the value of a checkbox (or any other type of form input). PHP only runs when a HTTP request is made to the server, ie when the form is submitted. PHP cannot respond to events happening in the browser. Edited August 21, 2015 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
NotSureILikePHP Posted August 21, 2015 Author Share Posted August 21, 2015 I also tried this: $active = '<script type="text/javascript">document.getElementByID("chkActive").checked;</script>'; Which almost worked. I got this response: Active Checked Checked Checked Checked Checked Checked Checked Checked Checked Checked Checked Checked Checked Checked Checked Checked Checked Checked Checked Checked But the page was greyed out and I had to refresh the page to do anything. It is in a loop so I would expect the mutiple Checked but I would expect this to be my resut. Active Checked Active Checked Active Checked Active Checked Active Checked Active Checked Active Checked Active Checked Active etc.... Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted August 21, 2015 Share Posted August 21, 2015 I also tried this: That code only defines a string which is assigned to a variable. The string is made up of characters , which only us humans will know is JavaScript code. PHP will not know this. The Javascript code will not be ran when that variable is defined. The javascript will only be ran when the browser receives the output from your PHP script PHP only runs server side. JavaScript runs client side (the browser). It is in a loop What loop? What is it are you trying to do. Quote Link to comment Share on other sites More sharing options...
NotSureILikePHP Posted August 21, 2015 Author Share Posted August 21, 2015 We have hundreds of customers but half of them aren't active. We even have test customers that show up in the result list. So I created a column in the database for isActive and set the active customers to 1 and non active to 0. I want to load the active customers by default because we rarely need to do anything with the inactve but sometimes they need to edit them or reactivate them so here's what I want to do: //obviously not real code but this is for simplicity. The actual code is much longer so I am simplifying. customers = getCustomers for (custs as cust) if chkActive = checked && cust.active = false //skip this record when check active continue Quote Link to comment Share on other sites More sharing options...
Solution Ch0cu3r Posted August 21, 2015 Solution Share Posted August 21, 2015 Then you need to submit the checkbox when it is unchecked/checked. When the checkbox is checked, then run the query which only returns customers that are active. Otherwise run a query which returns all customers Example script <?php // default value, set to 1 to search for active customers $searchActive = 1; // if a POST request is made if($_SERVER['REQUEST_METHOD'] == 'POST') { // override default value. If the checkbox is checked, then set to 1 otherwise set to 0 $searchActive = isset($_POST['isActive']) ? $_POST['isActive'] : 0; } echo 'Searching only active customers: ' . (($searchActive == 1) ? 'YES' : 'NO'); $query = 'SELECT * FROM customers'; if($searchActive) $query .= ' WHERE active = 1'; echo '<br/>Query:<pre>' . $query . '</pre>'; ?> <form method="post"> Active : <input type="checkbox" name="isActive" value="1" <?php echo $searchActive ? ' checked="checked"' : ''; ?> onClick="form.submit();" /> </form> Quote Link to comment 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.