Looking for some help with checkbox(switch type) that I have put in PHP.
These checkbox are created dynamically with data received from a MySQL Database.
The checkbox is loaded on the page depending on the condition of the checkbox placed on the database.
Now, I need help to build a code that perform some action like writing back into another database table everytime this checkbox is actioned; i.e. its inital state can be :checked or left blank(unchecked) and each of these checkbox have an ID or value on them from database. But any action that takes place either from blank to :checked or from :checked to blank, it needs to trigger an action for that particular actioned checkbox only.
Would need help on this as PHP please.
My Code Looks like this for a checkbox:
echo "<input type='checkbox' class='input-swtoggle' name='switch-box' ";
$lightStatus = $row->light_status;
if ($lightStatus == "1") {
echo "checked>";
}
else{
echo ">";
}
PS: I did try alot Googling and on Stackoverflow, but none of them are helping for the above condition.
You can bind click event to the checkbox using .on()
$(function(){
$(document).on('click',"input:checkbox.input-swtoggle",function() {
alert($(this).is(':checked'));
});
});
<?php
// HTML code
$checkced = ($row->light_status == '1') ? 'checked="checked"' : '';
echo '<input type="checkbox" class="input-swtoggle" name="switchbox[]"' . $checkced . '/>';
// Code after form posted:
if (! empty($_POST['switchbox'])) {
foreach ($_POST['switchbox'] as $switch) {
// Apply your logic here.
}
}
?>
Related
I am new to Php. I got a checkbox array and button(they are not <form>). Once the checkbox have checked and the user click the button. It will update the value of the session. Which display how many checkbox it clicked. My problem is the code is not really working.
It is my code:
<?php
session_start();
$add = 0;
$_SESSION["add"] = $add;
if(isset($_POST['checker']) && (isset($_POST['click']))){
if (is_array($_POST['checker'])){
foreach ($_POST['checker'] as $value) {
echo $value;
$add++;
}
}
echo "<b>You have clicked".$add." box.</b>";
echo "<tr><td><input type='checkbox' name='checker[]' value=''/></td></tr>";
}
P.S I use mysql to retrieve the row count into the table so there will be 10 checkbox appear.
when you click on button what happens? You have to use javaScript to capture the button click event or if the php code is in same file use this <form action="#"> it will load again and check if the $_POST['checker'] isset.
I might be on the wrong path (tiredness..) for the moment but i have passed 4 hours or something similar to debug my code. I have a form that is auto submitted when I click on a radio button, once clicked the next form appear and let me input the customer information. But when the page reload for displaying the other form, my variable $CustomerType is set and correct and, when i complete the input form (the second one) the php check if everything in it seems correct and it does, but it says my variable $CustomerType is missing then reload the page and ask me again to set the type.
I can't paste all my code out here (~300 lines) but here is the core :
<?php $_POST['CustomerType']="SOMEONE"; ?> // Ok so this was the trick, it solved the main bug but it now fix my choice to SOMEONE only. Can't change to another type
<form method="post" action="<?php echo escaped_html($_SERVER['PHP_SELF'],'URL');?>">
<?php
$array=show_enum("customer", "CustomerType");
$checked="";
foreach($array as $CustomerType)
{
$checked="";
if(isset($_POST['CustomerType']))
{
if($_POST['CustomerType']==$CustomerType) $checked="checked";
}
echo "<input type='radio' name='CustomerType' value=".$CustomerType." ".$checked." onClick='this.form.submit()'>".$CustomerType."</input>";
}
?> </form>
EDIT Ok there is some news : by modifying the top who was : <?php $_POST['CustomerType']="SOMEONE"; ?>
TO
if(!isset($_POST['CustomerType'])) $_POST['CustomerType']="SOMEONE";
It seems to solve the second problem of the form, which couldn't let me change the type (auto-rollback to SOMEONE). But now, on form submit my choices are always rolling back to [CustomerType] => SOMEONE instead of SOMEBODY (and i checked SOMEBODY).
It means that i can't hold the value $_POST['CustomerType'] on page reload for submitting.
For example : This one which seems identical except that it's submitted with "save" button instead of onsubmit is working fine.
$array=show_enum("customer", "Language");
foreach($array as $Language)
{
$checked="";
if(isset($_POST['Language']))
{
if($_POST['Language']==$Language) $checked="checked";
}
else if($Language=="FR") $checked="checked";
echo "<input type='radio' name='Language' value=".$Language." ".$checked." />";
string2languagepic($Language);
}
Picture of the problem *OnSubmit = onClick='this.form.submit()
After looking at your code a little more I think I have spotted your problem, try the following and see if it works.
echo "<input type='radio' name='CustomerType' id='CustomerType' value='$CustomerType' $checked onClick='this.form.submit();' >"
If that fails you could always add a hidden field, and when clicking on the radio button it adds a value to it and then submits the form.
Hello I'm having some problem.
I want it to be checked by default, and if you unselect it and press submit I want it to be unchecked
<input type="checkbox" name="show_signature" value="1"<?php echo isset($_POST['show_signature'])) ? ' checked=""' : '' ?>>
This works good unchecking > submitting and checkbox is unchecked and same if you check it and send the form, it stays checked.
But, I want it to be checked by default. Should'nt this work?
if (isset($_POST['show_signature'])) {
echo ' checked=""';
} else {
echo '';
}
Tried this to
if (isset($_POST['show_signature']) || !isset($_POST['show_signature'])) {
echo ' checked=""';
} else {
echo '';
}
Okay; from the question, I got this process
When the page loads the first time, you want the checkbox to be checked.
When the page is submitted, if the checkbox is unchecked, let it remain unchecked; otherwise, let it be checked.
Try
/*making the assumption that the submission process starts when a submit button
with name **submit** is present, use this
*/
if (isset($_POST["submit"])){
value = '<input type="checkbox" name="show_signature"';
value .= (isset($_POST["show_signature"]))? 'checked="checked"': "";
value .= ' />';
print value;
}
else{ //when the page is initially loaded
print '<input type="checkbox" name="show_signature" checked="checked" />';
}
The reason the post depends on the submit button (or any other field)t is because if the user unchecks the box, the $_POST["show_signature"] variable will not be found, and the form will not be processed at all.
That should resolve the issue.
Hope the explanation is clear and this helps.
I am using Jquery UI Selectable. The user has the option to dynamically add new list items to the original setup.
I would like to include a 'clear' button that will give the user the ability to clear each individual item they created by clicking on an X input submit (img).
HTML (php)
if ($this->session->userdata('inactivefilter') == true) {
$inactivefilter = $this->session->userdata('inactivefilter');
$i=0;
foreach ($inactivefilter as $filter)
{
$filterdash = implode('-', explode(' ', $filter));
echo "<li class='ui-state-default' id='$filterdash'>$filter</li>";
echo "<div id=clear>
<form method='POST' action='".base_url()."main/clear_filter'>
<input type='image' name='filtervalue' value='$i' src='".base_url()."img/board/icons/clear.png'></input>
</form>
</div>";
$i++;
}
}
This is where the list is created. specifically the clear_filter action form.
Clear_filter currently 'attempts' to grab the value of '$i' but I don't know how to pass that correctly.
here is the controller:
public function clear_filter()
{
$i = $_POST['filtervalue'];
$this->thread_model->clear_filter($i);
}
I'll omit the clear_filter model due to its irrelevance to the problem.
Essentially, I just need $i to be picked up based on whatever value is clicked on in the dynamic form on the actual page.
Any help would be appreciated!
Well, it seems like I just had things a bit backwards.
The code was more or less correct.
For Codeigniter, you catch the passed input value=$i by using the name ="filtervalue"
Change the controller code to :
$i = $this->input->post('filtervalue');
and $i is set to whatever value was clicked on.
I am trying to create a php file that would scan a directory, make a list of dynamic radio buttons and have the user select one and submit it and it shall pass it into another php file that would send it off, but I am having trouble with the submit button. Was wondering if I could get some answers, been looking around the net and haven't been able to find anything.
Here is my code:
<?php
ini_set('display_errors',true);
//Scan directory (ripped from another site)
$directory = opendir("download");
while($entry = readdir($directory)) {
$dirArray[] = $entry;
}
closedir($directory);
$indexCount = count($dirArray);
sort($dirArray);
//Make the Radio Button list
$RList = "";
for($i=2; $i<count($dirArray); $i++) {
$value = $dirArray[$i];
$RList .= "<input type=\"radio\" name=\"Files\" value=".$value." />".$value."<br />";
}
//Submit button
$RButton = '<input type="button" value="Submit"
onclick="location.href=\'http://localhost/appClient/load.php?file='.$value.'\'">';
echo $RList.$RButton;
#echo $_POST["Files"];
?>
Thanks
It looks like you're passing $value to the onclick event. Since that's outside of the PHP loop, it will end up always being the last $value.
To fix this, use javascript to get the selected value from the radio and append that to the onclick redirect.
Better yet, your load.php file should just simple check the 'Files' key in your $_POST and use that, instead of using GET. That's the point of submitting the radio button, after all.
I don't see the <form> tag, try to add it because the all buttons and inputs must be within the <form>
Does your script is calling the file load.php already or the button just does not work?