PHP : insert multiple check boxes values into one MySQL column - javascript

I want to write a simple PHP function to insert values of 10 and 20 check boxes. Now, the issue is: should I insert all values in a single column of MySQL table or should I go with the separate table?
My main goal is to insert the values of multiple checkboxes into MySQL and then update them. If I checked 7 checkboxes, and after some time I want to update from 7 to 5, how it will remove values from table column?
Please help me with some kind simple PHP example and what type of MySQL fields should I add, becuase I want to insert checkbox value which should in digital and the label of check boxes in other field.
Here is the HTML I have
<form method="post" action="">
Games You Like: <br/>
<input type="checkbox" name="games[]" value="1"><label>Football</label><br>
<input type="checkbox" name="games[]" value="2"><label>Basket Ball</label><br>
<input type="checkbox" name="games[]" value="3"><label>Pool</label><br>
<input type="checkbox" name="games[]" value="4"><label>Rugby</label><br>
<input type="checkbox" name="games[]" value="5"><label>Tennis</label><br>
<input type="checkbox" name="games[]" value="6"><label>Cricket</label><br>
<input type="checkbox" name="games[]" value="7"><label>Table Tennis</label><br>
<input type="checkbox" name="games[]" value="8"><label>Hockey</label><br>
<input type="submit" name="submit" value="submit">
</form>

Try this whole example,
Table Structure
CREATE TABLE IF NOT EXISTS `games` (
`id` int(12) NOT NULL AUTO_INCREMENT,
`game_name` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
<?php
include_once("yourconfig.php"); //include your db config file
extract($_POST);
$check_exist_qry="select * from games";
$run_qry=mysql_query($check_exist_qry);
$total_found=mysql_num_rows($run_qry);
if($total_found >0)
{
$my_value=mysql_fetch_assoc($run_qry);
$my_stored_game=explode(',',$my_value['game_name']);
}
if(isset($submit))
{
$all_game_value = implode(",",$_POST['games']);
if($total_found >0)
{
//update
$upd_qry="UPDATE games SET game_name='".$all_game_value."'";
mysql_query($upd_qry);
}
else
{
//insert
$ins_qry="INSERT INTO games(game_name) VALUES('".$all_game_value."')";
mysql_query($ins_qry);
}
}
?>
<form method="post" action="">
Games You Like: <br/>
<input type="checkbox" name="games[]" value="1" <?php if(in_array(1,$my_stored_game)){echo "checked";}?>><label>Football</label><br>
<input type="checkbox" name="games[]" value="2" <?php if(in_array(2,$my_stored_game)){echo "checked";}?>><label>Basket Ball</label><br>
<input type="checkbox" name="games[]" value="3" <?php if(in_array(3,$my_stored_game)){echo "checked";}?>><label>Pool</label><br>
<input type="checkbox" name="games[]" value="4" <?php if(in_array(4,$my_stored_game)){echo "checked";}?>><label>Rugby</label><br>
<input type="checkbox" name="games[]" value="5" <?php if(in_array(5,$my_stored_game)){echo "checked";}?>><label>Tennis</label><br>
<input type="checkbox" name="games[]" value="6" <?php if(in_array(6,$my_stored_game)){echo "checked";}?>><label>Cricket</label><br>
<input type="checkbox" name="games[]" value="7" <?php if(in_array(7,$my_stored_game)){echo "checked";}?>><label>Table Tennis</label><br>
<input type="checkbox" name="games[]" value="8" <?php if(in_array(8,$my_stored_game)){echo "checked";}?>><label>Hockey</label><br>
<input type="submit" name="submit" value="submit">
</form>
this is just basic example and query i have added in this example, you can learn from this basic example and i think this is very useful for you... if useful than give correct answer for this solution

use implode function to convert returned array into a string an then insert it into database as normal
if(isset($_POST['submit'])){
$result = implode(",",$_POST['games']);
}
hopefully this will help you.
Regards
Imran Qasim

I put the game name into the brackets and changed their values to true:
<form method="post" action="">
Games You Like: <br/>
<input type="checkbox" name="games[Football]" value="true"><label>Football</label><br>
<input type="checkbox" name="games[Basket]" value="true"><label>Basket Ball</label><br>
<input type="checkbox" name="games[Pool]" value="true"><label>Pool</label><br>
<input type="checkbox" name="games[Rugby]" value="true"><label>Rugby</label><br>
<input type="checkbox" name="games[Tennis]" value="true"><label>Tennis</label><br>
<input type="checkbox" name="games[Cricket]" value="true"><label>Cricket</label><br>
<input type="checkbox" name="games[Table]" value="true"><label>Table Tennis</label><br>
<input type="checkbox" name="games[Hockey]" value="true"><label>Hockey</label><br>
<input type="submit" name="submit" value="submit">
</form>
After submission this will result in an array like this:
$_POST["games"] = Array ( [Tennis] => true [Cricket] => true [Table] => true )
// all other values false
You can easily store and later manipulate this array if you store it as an JSON string in your database. For that you need a single varchar or text column.
json_encode($_POST["games"])
// {"Tennis":"true","Cricket":"true","Table":"true"}

Please you try this
<?php
if(isset($_POST['submit']))
{
//in here you get games array
$mygames = $_POST['games'];
}
?>
<form method="post" action="">
Games You Like: <br/>
<input type="checkbox" name="games[]" value="1"><label>Football</label><br>
<input type="checkbox" name="games[]" value="2"><label>Basket Ball</label><br>
<input type="checkbox" name="games[]" value="3"><label>Pool</label><br>
<input type="checkbox" name="games[]" value="4"><label>Rugby</label><br>
<input type="checkbox" name="games[]" value="5"><label>Tennis</label><br>
<input type="checkbox" name="games[]" value="6"><label>Cricket</label><br>
<input type="checkbox" name="games[]" value="7"><label>Table Tennis</label><br>
<input type="checkbox" name="games[]" value="8"><label>Hockey</label><br>
<input type="submit" name="submit" value="submit">
</form>

Firstly very straight to point, I prefer doing names like this from different checkboxes
BasketBall;Cricket;Tennis
You can do that by
implode(";",$array);
and then write that in mySQL table...
To get those values Just use
$array = explode(";",$urVariable);
For different values use
numberOne = $array[1]; // returns Cricket
to change any value use Just get the values and rewrite in ";" form..
I like this as it is simple to implement and do job perfectly!

Related

Modify Url from Search Form

I've search form that have part like this :
<input type="checkbox" id="checkboxlist-field-[29]" name="CategoryField[9][]" checked="checked" value="bed">
<input type="checkbox" id="checkboxlist-field-[30]" name="CategoryField[9][]" checked="checked" value="almari pakaian">
<input type="checkbox" id="checkboxlist-field-[31]" name="CategoryField[9][]" value="tv">
<input type="checkbox" id="checkboxlist-field-[32]" name="CategoryField[9][]" checked="checked" value="ac">
<input type="checkbox" id="checkboxlist-field-[33]" name="CategoryField[9][]" checked="checked" value="wifi-internet">
<input type="checkbox" id="checkboxlist-field-[34]" name="CategoryField[9][]" checked="checked" value="meja cermin rias">
<input type="checkbox" id="checkboxlist-field-[36]" name="CategoryField[9][]" checked="checked" value="sekamar berdua">
When they're checked and form is submited, it generates url into something like this :
http://localhost/index.php/category/akomodasi?CategoryField[9][]=bed&CategoryField[9][]=almari pakaian&CategoryField[9][]=ac&CategoryField[9][]=wifi-internet&CategoryField[9][]=meja cermin rias&CategoryField[9][]=bisa pasutri&CategoryField[9][]=sekamar berdua&CategoryField[10]=1
The search works fine, but I want to modify the url into something like this :
http://localhost/index.php/category/akomodasi?CategoryField[9]=bed,almari pakaian,ac,wifi-internet,meja cermin rias,bisa pasutri,sekamar berdua&CategoryField[10]=1
So values of CategoryField[9] are separated with ",".
How to modify the url? btw, I'm using Yii2 Framework.
Thank you.
Do you just want to get the category fields separated with ","? or actually need to change the url? If it's the first option you can do that with:
$categoryField = implode(', ', $_GET['CategoryField'][9]);

Checkbox option to produce an SQL result

what function should I use to obtain a "Y" if the checkbox has been selected or "N" if it has not. This is to add into a SQL statement?
I'm guessing it is an if/else but I can't figure it out.
Here is my checkboxes which some may be selected individually:
<label class="timetable-q">Data Projector</label>
<input type="hidden" id="proj_check" name="proj_check" value="Y"/>
<input type="checkbox" id="proj_check" name="proj_check" value="N"/>
<label class="timetable-q">Whiteboard</label>
<input type="hidden" id="white_check" name="white_check" value="Y"/>
<input type="checkbox" id="white_check" name="white_check" value="N"/>
<label class="timetable-q">OHP</label>
<input type="hidden" id="ohp_check" name="ohp_check" value="Y"/>
<input type="checkbox" id="ohp_check" name="ohp_check" value="N"/>
<label class="timetable-q">Wheelchair Access</label>
<input type="hidden" id="wheel_check" name="wheel_check" value="Y"/>
<input type="checkbox" id="wheel_check" name="wheel_check" value="N"/>
<label class="timetable-q">Lecture Capture</label>
<input type="hidden" id="cap_check" name="cap_check" value="Y"/>
<input type="checkbox" id="cap_check" name="cap_check" value="N"/>
<input type="submit" value="submit">
</p>
</form>
Here is my PHP:
<?php
$numeroOption= $_POST['numero'];
$selectOption = $_POST['parkname'];
$query = "SELECT * FROM `ROOMS` WHERE `Capacity` < '$numeroOption' AND `Park` LIKE '$selectOption%'";
$result = mysql_query($query);
echo $result;
if ($result == FALSE) die ("could not execute statement $query<br />");
</php>
Thanks for your help!
You do not need to create a hidden lable. Check box does not post value if it is not selected. So you just need to check in php weather $_POST is set or not for example
<input type="checkbox" id="proj_check" name="proj_check" value="Y"/>
<input type="checkbox" id="white_check" name="white_check" value="Y"/>
and On php page you have to check like
if(isset($_POST['proj_check']))
$proj_check = $_POST['proj_check'];
else
$proj_check = "N";
then you can use $proj_check as per your need.

Must check last three check boxes to submit

Below is my list of check boxes that is part of my php form. I need to write a script where only the last three check boxes must be checked for the form to be successfully submitted. I have looked at other similar questions but can't seem to find what I am truly looking for and I am having trouble to write the script.
<input type="checkbox" name="checkbox" value="Send email request">Would you like to receive weekly new via email?<br>
<input type="checkbox" name="checkbox" value="Agree to T&C">I agree to the Terms and Conditions<br>
<input type="checkbox" name="checkbox" value="No Criminal Records">I have no criminal associations<br>
<input type="checkbox" name="checkbox" value="No Business with any other company">I have no personal or business relationship with any other company<br>
You do not need to use any JavaScript.
Simply add a required attribute to the input elements.
<input type="checkbox" required="required" name="checkbox" value="Send email request">Would you like to receive weekly new via email?<br>
Take a look here for more info: http://www.the-art-of-web.com/html/html5-checkbox-required/
If you want a custom message or response, then you will need to use JavaScript to handle this.
Alternatively, you could use jQuery to check if it is indeed that the last 3 checkboxes or are checked (required) before submission. Consider this example:
<form method="POST" action="index.php" id="form">
<input type="checkbox" name="checkbox[]" value="Send email request">Would you like to receive weekly new via email?<br>
<input type="checkbox" name="checkbox[]" value="Agree to T&C">I agree to the Terms and Conditions<br>
<input type="checkbox" name="checkbox[]" value="No Criminal Records">I have no criminal associations<br>
<input type="checkbox" name="checkbox[]" value="No Business with any other company">I have no personal or business relationship with any other company<br>
<input type="submit" name="submit" />
</form>
<script src="jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#form').submit(function(e){
var count = 0;
var required_indeces = [1,2,3];
$('input[type=checkbox]').each(function(index, element){
if($.inArray(index, required_indeces) !== -1 && $(this).is(':checked')) {
count++;
}
});
if(count < 3) {
e.preventDefault();
}
});
});
</script>
The checkboxes need to have different names. Using the same name means you are only storing the value of the last checked box. Try one of these two options (for PHP):
<input type="checkbox" name="checkbox[]" value="Send email request">Would you like to receive weekly new via email?<br>
<input type="checkbox" name="checkbox[]" value="Agree to T&C">I agree to the Terms and Conditions<br>
<input type="checkbox" name="checkbox[]" value="No Criminal Records">I have no criminal associations<br>
<input type="checkbox" name="checkbox[]" value="No Business with any other company">I have no personal or business relationship with any other company<br>
Then, to check that the last three are checked:
if (!isset($_POST['checkbox'][1], $_POST['checkbox'][2], $_POST['checkbox'][3])) {
echo 'One of the last 3 NOT checked!';
}
OR:
<input type="checkbox" name="checkbox1" value="Send email request">Would you like to receive weekly new via email?<br>
<input type="checkbox" name="checkbox2" value="Agree to T&C">I agree to the Terms and Conditions<br>
<input type="checkbox" name="checkbox3" value="No Criminal Records">I have no criminal associations<br>
<input type="checkbox" name="checkbox4" value="No Business with any other company">I have no personal or business relationship with any other company<br>
Then:
if (!isset($_POST['checkbox2'], $_POST['checkbox3'], $_POST['checkbox4'])) {
echo 'One of the last 3 NOT checked!';
}

How to select Multi Check Boxes in HTML using Meteor?

I need to know about selecting multi check boxes in html using meteor.I did a sample.In this sample selecting more than one check box.how to get the selected data and how to store the data in array.Can you please see the below code and suggest me what to do?
The storage of the details in array as suppose arrayname = Bike,car,Computer(these are selected items)
HTML Code:
<form id="cb-form" action="action">
<input type="checkbox" name="owns" value="Bike">Bike<br/>
<input type="checkbox" name="owns" value="Car">car<br/>
<input type="checkbox" name="owns" value="Refridgerator">Refridgerator<br/>
<input type="checkbox" name="owns" value="Mobile">Mobile<br/>
<input type="checkbox" name="owns" value="Tablet">Tablet<br/>
<input type="checkbox" name="owns" value="Computer">Computer<br/>
<input type="submit" value="send" />
</form>
JS Code:
Template.login.events
({
'submit #cb-form' : function (e,t)
{
// template data, if any, is available in 'this'
if (typeof console !== 'undefined')
console.log("You pressed LOGIN Button");
e.preventDefault();
//retrieve the input field values
//here write get multiple check boxes data logic same as above scenario
}
});
HTML:
<template name="login">
<form id="cb-form" action="action">
<input type="checkbox" name="owns" value="Bike">Bike<br/>
<input type="checkbox" name="owns" value="Car">car<br/>
<input type="checkbox" name="owns" value="Refridgerator">Refridgerator<br/>
<input type="checkbox" name="owns" value="Mobile">Mobile<br/>
<input type="checkbox" name="owns" value="Tablet">Tablet<br/>
<input type="checkbox" name="owns" value="Computer">Computer<br/>
<input type="submit" value="send" />
</form>
</template>
JS:
Template.login.events({
'submit #cb-form' : function (event, template) {
event.preventDefault();
var selected = template.findAll( "input[type=checkbox]:checked");
var array = _.map(selected, function(item) {
return item.defaultValue;
});
console.log(array);
}
});
This outputs ["Car", "Refridgerator"] if Car and Refridge are selected. Its just easy use of underscore. Check underscore documentation for further reading.

Nested Form to disable text field with selection box HTML

I have code form.html and entry.php
First I want to disable textfield with selection box using javascript, then submit it to give an output.
If i not using this code <form name="form1" method="post" action="entry.php"> form.html is success to display in web browser, but how i can submit it with one submit button?
form.html
<html>
<head>
<script src="jquery.js"></script>
<script>
$(document).ready(function(){
$('.group').hide();
$('#option1').show();
$('#chooseForm').change(function() {
$('.group').hide();
$('#'+$(this).val()).show();
})
});
</script>
</head>
<body>
<form name="form1" method="post" action="entry.php">
<select id="chooseForm" name="select">
<option value="option1">Form1</option>
<option value="option2">Form2</option>
<option value="option3">Form3</option>
</select>
<form id="option1" class="group">
<input name="a" value="form A"><br>
</form>
<form id="option2" class="group">
<input name="a" value="form A"><br>
<input name="b" value="form B"><br>
</form>
<form id="option3" class="group">
<input name="a" value="form A"><br>
<input name="b" value="form B"><br>
<input name="c" value="form C"><br>
</form>
<input value="Save" name="submit" type="submit"><br>
</form>
</body>
</html>
entry.php
<?php
$select = $_POST['select'];
$a = $_POST['a'];
$b = $_POST['b'];
$c = $_POST['c'];
echo $select;
echo "<br>";
echo $a;
echo "<br>";
echo $b;
echo "<br>";
echo $c;
?>
Can anyone solve this code without nested form? thanks :)
First of all, you can't nest forms.
As I understand the idea is to group some elements. If so, then replace the interior <form>tags with <div>s.
The other problem is that you want to use the same input elements names through different sections. Basically, if name is not unique, it will get updated with the last occurrence's value. For example:
<input type="text" name="a" value="val 1" />
<input type="text" name="a" value="val 2" />
When the above is posted, $_POST['a'] will contain val 2 value. Even, if the second text-box is hidden. So either you make text-boxes names unique or you disable the ones, which are hidden. disabled attribute will make the control disabled for user input and also not present in $_POST array. So, in this case:
<input type="text" name="a" value="val 1" />
<input type="text" name="a" value="val 2" disabled />
$_POST['a'] will contain val 1 value, because the second text-box is disabled.
In your case, every time you hide a section you should disable all controls within the group. Here's how to do it: disable all form elements inside div.

Categories

Resources