How to allow only 2 selection checkbox in a form with? - php

I want to put a form on my website for giving away 3 prizes. Each member to be able to select only 2 item on the list that they wish to win.
I have created the form and with JavaScript it works fine, but it's client side and can't be trusted.
What is the best way to implement such thing and be sure that each user can ONLY select 2 item? best way to do it with PHP?
Appreciate any help.

jQuery is boss, have a look here.
http://gravitywiz.com/2012/06/11/limiting-how-many-checkboxes-can-be-checked/

<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
Price 1<input value="price1" type="checkbox" name="price[]">
Price 2<input value="price2" type="checkbox" value="price[]">
Price 3<input value="price3" type="checkbox" value="price[]">
<input type="submit" name="send" value="Send" />
</form>
Then check the size of the array $_POST['price']:
<?
if(isset($_POST['send'])) {
if(sizeof($_POST['price']) == 2) {
//here you go
} else {
echo "error, pick exactly two prices!";
}
}
?>

Why won't you use radiobuttons?
<form method="post">
Price 1 <input type="radio" value="price 1" />
Price 2 <input type="radio" value="price 2" />
Price 3 <input type="radio" value="price 3" />
</form>
And if you want to allow multiple prizes just switch to checkboxes
<form method="post">
Price 1 <input type="checkbox" name="price[]" value="price 1" />
Price 2 <input type="checkbox" name="price[]" value="price 2" />
Price 3 <input type="checkbox" name="price[]" value="price 3" />
</form>

Related

how-to submit form with multiple groups of radio buttons?

goal: post several groups of radio buttons to array (as a message, to database, etc.)
Hi there,
I was wondering how i can post my selections of several groups of radio buttons.
Here is the code:
<body>
<form action="" method="post">
<select name="module 1">
<input type="radio" name="radioA" value="Radio 1">Radio 1
<input type="radio" name="radioA" value="Radio 2">Radio 2
<input type="radio" name="radioA" value="Radio 3">Radio 3
<input type="submit" name="submit" value="Get Selected Values" />
</select>
<br>
<select name="module 2">
<input type="radio" name="radioB" value="Radio 21">Radio 21
<input type="radio" name="radioB" value="Radio 22">Radio 22
<input type="radio" name="radioB" value="Radio 23">Radio 23
<input type="submit" name="submit" value="Get Selected Values" />
</select>
<br>
<select name="module 3">
<input type="radio" name="radioC" value="Radio 31">Radio 31
<input type="radio" name="radioC" value="Radio 32">Radio 32
<input type="radio" name="radioC" value="Radio 33">Radio 33
<input type="submit" name="submitC" value="Get Selected Values" />
</select>
<br>
</form>
<?php
if (isset($_POST['submit'])) {
if(isset($_POST['radioA']))
{
echo "You have selected :".$_POST['radioA']; // Displaying Selected Value
}
}
?>
</body>
...But I can't make it display the list of my selections, but only one instructions, instead of 3x (one for each).
Long story short... How can I treat this as a 1 global set of 3 groups, having only 1 "submit" button to process all 3 groups at once?
Txs a lot, and have a nice weekend.
You just need to remove the other two submit buttons and only display one. As long as it is inside the <form> it will submit all the radio button "groups" to the $_POST.
Also, remove the select tags as you are using them incorrectly. Only an option or optgroup should be nested inside a select element.
You will need to reference each one individually to see the selected response:
echo $_POST['radioA'];
echo $_POST['radioB'];
echo $_POST['radioC'];

Multiple actions for checkbox list

I have a list of multiple items with having a checkbox for each row (like gmail list of emails).
User selects checkboxes and then press an action button to perform some action on selected items ( delete for example). Then the list of selected items will be sent to php file for background processing.
But what is the solution for performing multiple actions on selected checkboxes ( For instance, in gmail, we do delete, mark as spam, mark as read/unread etc..).
Here is what I have:
Use different "submit buttons" for each actions. Give same name but different values. Then check it to backend
For example,
HTML
<form action="backend.php" method="post" >
<input type="submit" name="action" value="Delete" />
<input type="submit" name="action" value="Move" />
<input type="submit" name="action" value="Copy" />
<input type="checkbox" name="selection[]" value="value 1" >
<input type="checkbox" name="selection[]" value="value 2" >
<input type="checkbox" name="selection[]" value="value 3" >
<input type="checkbox" name="selection[]" value="value 4" >
<input type="checkbox" name="selection[]" value="value 5" >
</form>
PHP
..................
..................
if($_POST['action'] == "Delete")
.......
else if($_POST['action'] == "Move")
.......
else if($_POST['action'] == "Copy")
.......
I would give the checkboxes a name that would result in a php item array after a post the form.
For example, name="item[]"
When the form is posted you can then loop through the items in the array with,
foreach($_POST['item'] as $item) {
/* do action */
}

return state of a checkbox and return 0 if unchecked

I have been finding interesting possible solutions to this, none speak to me clearly unfortunately.
What I have is a series of checkboxes that store the users preferences, so when the page populates I need to know the state of the checkbox so it can display the preference. Also I noticed that HTML doesnt return a value if the checkbox is not checked, well I need to store a 0 if it is not checked. So what is a good way to accomplish this? I am using just HTML and PHP
I have seen a few solutions and have some questions. First the code
<form action="TradingRules.php" method="post">
<h2><legend>Entry Rules</legend></h2>
<p><label> <input type="checkbox" name="30minRule" value="1" <?php if(isset($_POST["30minRule"])) { echo 'checked="checked" '; } ?>>30 Min Rule</label></p>
<p><label> <input type="checkbox" name="buyOnPocketPivot" value="1">Buy on Pocket Pivot</label></p>
<p><label> <input type="checkbox" name="buyEODonPPday" value="1">Buy EOD on PP Day</label></p>
<p><label> <input type="checkbox" name="buyOn10WeekLineTest" value="1">Buy on 10 week line Test (within 3%)</label></p>
<p><label> <input type="checkbox" name="okBelow400k" value="1">Ok below 400k Average</label></p>
<p><label> <input type="checkbox" name="buyIfOthersAreBreakingOut" value="1">Buy if other are breaking out</label></p>
<p><label> <input type="checkbox" name="buyIfStockHasUnusualStrength" value="1">Buy if stock has unusual strength</label></p>
<p><label> <input type="checkbox" name="buyOnNon-breakoutVolume" value="1">Buy on non-breakout Volume</label></p>
<p><label> <input type="checkbox" name="buyOnGap" value="1">Buy on Gap</label></p>
<p><label> <input type="checkbox" name="enableGapRules" value="1">Enable Gap Rules</label></p>
<p><label> <input type="checkbox" name="buy<1WeekBeforeEarnings" value="1">Buy < 1 week before earning</label></p>
<p><label> <input type="checkbox" name="buyOn3WeeksTight" value="1">Buy on 3 weeks tight</label></p>
<p><label> <input type="checkbox" name="buyOnHTF" value="1">Buy on HTF</label></p>
<p><label> <input type="checkbox" name="buyOnPullbackToPivot" value="1">Buy on pullback to pivot </label></p>
<p><input type="submit" /></p>
</form>
Questions:
1) where do you put the hidden button? I have <p> tags because I want them on top of the other so do I put them all in the <p> tags? So would something like this work?
<p><label> <input type="hidden" name="30minRule" value="0">
<input type="checkbox" name="30minRule" value="1" <?php if(isset($_POST["30minRule"])) { echo 'checked="checked" '; } ?>>30 Min Rule</label></p>
<p><label> <input type="hidden" name="buyOnPocketPivot" value="0">
<input type="checkbox" name="buyOnPocketPivot" value="1">Buy on Pocket Pivot</label></p>
2) On the topic of getting the info back from the database for checkbox status, I have the form linked to my php script will that be adequate for getting those values? They all go to and from the same location, or do I need something else?
Thanks in advance for the help!
When you receive the value in PHP, it might be worth it to just make whatever is missing into a 0. This is easy enough with the array_merge function.
$post_form = array_merge(array(
'30minRule' => 0,
'buyOnPocketPivot' => 0 // etc
), $_POST);

Different checkboxes send the user to different pages when checked

I need a form in which the checkboxes would open different pages based on their selection when the form is submitted.
So, say I have this simple form:
<form action="" method="post">
<input value="1" type="checkbox" name="sign" />
<input value="2" type="checkbox" name="sign" />
<input value="3" type="checkbox" name="sign" />
<input value="4" type="checkbox" name="sign" />
<input value="5" type="checkbox" name="sign" />
<input type="submit" />
</form>
When an user checks value 1 and submits, he will be redirected to page A. If a user checks 1 and 4 he will be redirected to a different page (page F, for instance). If a user checks 2, 3 and 4 he will be redirected to page R, and so on... There would be 25 different combinations, and therefore, 25 different page results for this form when an user submits it.
In other words, when the form is submitted somehow the system would read which checkboxes were checked and associate each possible combination with a different URL.
Can it be done? If so, how? Anyone ever made something similar? I've searched a long time for solutions, but found only slightly similar ones, not exactly what I need, so any help would be appreciated.
HTML:
<form action="" method="post">
<input value="1" type="checkbox" name="sign[1]" />
<input value="2" type="checkbox" name="sign[2]" />
<input value="3" type="checkbox" name="sign[3]" />
<input value="4" type="checkbox" name="sign[4]" />
<input value="5" type="checkbox" name="sign[5]" />
<input type="submit" />
</form>
PHP:
if (isset($_POST['sign'][1]))
header("Location: a.php");
elseif(isset($_POST['sign'][2]) AND isset($_POST['sign'][3]))
header("Location: b.php");
This can be done in a couple of ways. One is that you can use javascript to intercept the form submission, change the value of the form "action", and then execute the submit.
A second approach might be to just send all this data to a single script and based on the selected values perform a redirect to the intended page.
From should look like:
<form action="" method="post">
<input value="1" type="checkbox" name="sign[]" />
<input value="2" type="checkbox" name="sign[]" />
<input value="3" type="checkbox" name="sign[]" />
<input value="4" type="checkbox" name="sign[]" />
<input value="5" type="checkbox" name="sign[]" />
<input type="submit" />
</form>
And the post like:
if(!empty($_POST['sign'])) {
if(in_array(1, $_POST['sign'])) {
// if just 1, go to page
} elseif(in_array(1, $_POST['sign']) && in_array(4, $_POST['sign'])) {
// if 1 and 4, go to page
}
}
You can just consider each of the inputs as a bit. So, you will always have 5 bits to consider.
Then define an associative array of 25 entries corresponding to each of the possible values:
00001
00010
...
var links = {
"00001" : "www.google.com",
];
Then, when you submit the form, just set the target attribute of the form based on the value.
If I were you and I had no choice than following this idea, I would use something like that in jQuery (assuming your post vars are treated in the page you want to reach):
<form action="" method="post" id="myForm">
<input value="1" type="checkbox" name="sign[]" />
<input value="2" type="checkbox" name="sign[]" />
<input value="3" type="checkbox" name="sign[]" />
<input value="4" type="checkbox" name="sign[]" />
<input value="5" type="checkbox" name="sign[]" />
<input type="submit" />
</form>
$("#myForm").submit(function() {
var checked_array = new Array();
$("#myForm input").each(function() {
if ($(this).is(":checked")
checked_array.push($(this).attr("value"));
});
if ( checked_array.indexOf(2) !== -1 && checked_array.indexOf(5) !== -1)
("#myForm").attr("action", "/url1.php") ;
else if etc...
});

Add hidden input types based on radio button selected

Is it possible to conditionally add hidden input fields into a form?
eg I have a php form that is adding values to a table and if the appleID = 1 or 2 then I want 1 added to the fruits column of my table and if appleID =3 I want 1 added to the sweets column of my table. I thought I might be able to do something like the below but it is adding all hidden values no matter what I select. Or should I approach this a different way?
<input type="radio" value="1" name="appleID" />
<input type="hidden" value="1" name="fruits" />
<input type="hidden" value="0" name="sweets" />
<input type="radio" value="2" name="appleID" />
<input type="hidden" value="1" name="fruits" />
<input type="hidden" value="0" name="sweets" />
<input type="radio" value="3" name="appleID" />
<input type="hidden" value="0" name="fruits" />
<input type="hidden" value="1" name="sweets" />
Thanks I haven't done much with php so I will need to explore that option further. Thanks for the feedback.
I was also looking at something like the below. But PHP sounds likes the better option.
change field value when select radio buttons
You can either use all values in the radio buttons (1,1,0 - 2,1,0 - 3,0,1) and split them after receiving them in your PHP script or add/delete the hidden fields via JavaScript.
Split Example:
HTML:
<input type="radio" value="1,1,0" name="appleID" />
<input type="radio" value="2,1,0" name="appleID" />
<input type="radio" value="3,0,1" name="appleID" />
PHP:
if (!empty($_POST['appleID']))
{
list($appleID, $fruits, $sweets) = explode(",", $_POST['appleID']);
}
It is better to put that logic into the PHP script instead of using Javascript - because you have to do the validation anyway.

Categories