Multiple checkboxes to variable logic [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am looking to create a simple page using HTML and (probably) PHP or JQuery. The page should allow for multiple checkbox selection, and give a variable a value based on which boxes are checked. This can be on submit or can render live on the page.
i.e.
Checkbox 1
Checkbox 2
Checkbox 3
If checkbox 1 is checked, variable = X
If checkbox 1 & 2 are checked, variable = Y
If no checkboxes are checked, variable = Z
What is the best way to approach doing this?

The best way would be using Javascript (because it'd be done live) but if you are doing this with PHP. The form would be something like
<form action='phplogic.php' method='post>
<input type="checkbox" name="checkbox1" value="Yes" />
<input type="checkbox" name="checkbox2" value="Yes" />
<input type="checkbox" name="checkbox3" value="Yes" />
<input type="submit" name="formSubmit" value="Submit" />
</form>
phplogic would would look like this
<?php
if(isset($_POST[checkbox1']) && (isset($_POST['checkbox2'])) {
$variable = 'Y';
} else (isset($_POST[checkbox1'])) {
$variable = 'X';
} else {
$variable = 'Z';
}
Although this doesn't address if checkbox 2 alone is checked but you get the point. You can do something very similar with javascript. https://www.w3schools.com/jsref/prop_checkbox_value.asp

Related

Is there any way that I can show a input bar in html? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I made a website for a restaurant, the checkout page has 2 option, pay by cash and pay by card. Now if the user chooses to pay by card, then there will be no more input required. If the user chooses to pay by cash, then I need to let them enter their name. Is there any method I can show the input bar when the user chooses to pay by cash? I'm using python flask
You can use JS to do it, since it is supported on nearly all browsers.
function validate(){
var cashpay = document.getElementById("cash-pay");
var cardpay = document.getElementById("card-pay");
var name = document.getElementById("name");
if(cashpay.checked==true){
name.style.display='block';
}else{
name.style.display='none';
}
}
.hidden{
display:none;
}
<h1>What would you like to pay by?</h1>
<form>
<input type="radio" name="pay-method" id="cash-pay" oninput="validate()"><label for="cash-pay">Cash</label><input type="radio" name="pay-method" id="card-pay" oninput="validate()"><label for="card-pay">Card</label>
<input type="text" name="name" id="name" class="hidden" placeholder="Please enter your name:">
</form>
We can use the oninput attribute in the input types to make sure that our JS function validate() is run every time the user modifies their choice. We can use the .checked DOM element to check whether the input is checked. If it is, we can use .style.display='block' to display the input.

If checkbox "A" and "C" is checked make checkbox "B" checked to [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Hi there i have a problem and dont know if there's a nice solution to do that.
<input type="checkbox" name="a" value="a">a<br>
<input type="checkbox" name="b" value="b">b<br>
<input type="checkbox" name="c" value="c">c<br>
<input type="checkbox" name="d" value="c">d<br>
<input type="checkbox" name="e" value="c">e<br>
I like to make it work more as a "range" so if a user click "a" and then "d" everything between that will be selected to.
I can make if statements with all the scenarios but maybe there's a better way to do that?
thanks
This is really simple with jQuery - use prevAll like so:
$('input[type="checkbox"]').on("change", function() {
if ($(this).is(":checked")) this.prevAll('input[type="checkbox"]').prop("checked", true);
});

PHP - How to save the exact time and date when [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
How can I save, in a database, the exact moment (time and date) when an user "tick" checkboxes?
I have 3 checkboxes to tick before proceeding with registration, and I need to save in the database the time and date when the user tick each checkbox (so 3 different times).
Use jquery, when the checkbox is clicked change the value of that checkbox and submit your form data..
function changeValue(id)
{
$('#checkbox'+id).val(new Date($.now()));
$('#span'+id).html('Clicked at '+new Date($.now()));
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type ='checkbox' id="checkbox1" name="a" onClick="changeValue('1');" >Checkbox 1
<span id="span1"></span><br>
<input type ='checkbox' id="checkbox2" name="b" onClick="changeValue('2');" >Checkbox 2
<span id="span2"></span><br>
<input type ='checkbox' id="checkbox3" name="c" onClick="changeValue('3');" >Checkbox 3
<span id="span3"></span>
The 'now()' keyword save the actual time...
If someone ticks the first box at 11:50AM and the second at 11:51AM, in the database must be inserted both different hour
at the time data save use "now()" which is mysql function.
you can set database table's column default value CURRENT_TIMESTAMP
I'm writing answer because i can't comment. You would need to use a javascript function to populate the value of the checkbox to the current time

javascript with checkbox to count and hide/show [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
i need help to make one script
The script I'm trying to do is the following:
I have a page with multiple checkbox's corresponding products and they all have an associated value.
  then I have a variable that is the value I have in the account, and what I wanted to do with that as I start choosing products by clicking on the checkbox, javascript would have to count the value that has already been chosen and see how and lacking and hide all products that have a higher value or have available
<?php
$totalmoney = '50';
?>
<div class="tags">
<label><input type="checkbox" id="arts" value="20" /> Arts </label>
<label><input type="checkbox" id="computers" value="40" /> Computers </label>
<label><input type="checkbox" id="phone" value="15"/> Phone </label>
<label><input type="checkbox" id="video-games" value="5" /> Video Games </label>
</div>
Help me please, i need this to my work :S
Start with calculating the values of selected checkboxes with JS something like this:
var $inputs = $('#arts,#computers,#phone,#video-games');
$inputs.on('click', function (event) {
$checked = $inputs.is(':checked');
totalChecked = 0;
$checked.each(function () {
totalChecked += $(this).val();
});
});
Now you can easily check the rest:
var toBeSpend = totalmoney - totalChecked;
$inputs.not(':checked').each(function () {
$(this).toggle($(this).val() > toBeSpend);
});

the conditions radio button to another page after submit [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm making an application with PHP and javascript
There are 2 choices Yes and No.
When he chose NO option and submits, then going to the page "Thank You" and its data into the database. When selecting the YES option it will go to another page and its data into the database
<label class="radio inline"><input type="radio" name="choose" id="optionsRadios1" value="Yes">YES</label>
<label class="radio inline"><input type="radio" name="usia" id="optionsRadios2" value="No">NO</label>
<label class="control-label" for="">Nama</label>
<div class="controls"><input type="text" name="nama" ></div><input type="submit" name="submit" value="" class="btn next">
Try this,
function submitPage(url){
window.location.href=url;
}
$('input:radio').on('click',function(){
var url=this.value=='Yes' ? 'xyz.php' : 'abc.php';
submitPage(url); // or you can use ajax for post
});
Create a javascript function and fire it on the onclick event of the radiobutton passing the radio value in the parameter
And then in the function put condition for redirect and form submit
or you can call a ajax to insert data in the database

Categories

Resources