Jump to content

Get checkbox status onload


NotSureILikePHP
Go to solution Solved by Ch0cu3r,

Recommended Posts

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?

Link to comment
Share on other sites

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 by Ch0cu3r
Link to comment
Share on other sites

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....

Link to comment
Share on other sites

 

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • Solution

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>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.