Is there any way one can link a checkbox to a textbox. For example
<form>
<table>
<tr>
<td><input name="databaseIDs[]" type="checkbox" value="49"/></td>
<td><input type="text" name="first" value="First Input"/></td>
</tr>
<tr>
<td><input name="databaseIDs[]" type="checkbox" value="50"/></td>
<td><input type="text" name="first" value="Second Input"/></td>
</tr>
<tr>
<td><input name="databaseIDs[]" type="checkbox" value="60"/></td>
<td><input type="text" name="first" value="Third Input"/></td>
</tr>
<tr>
<td><input name="databaseIDs[]" type="checkbox" value="38"/></td>
<td><input type="text" name="first" value="Fourth Input"/></td>
</tr>
</table>
</form>
As you can see, the checkbox is an array and each of the checkbox value is database id. What I am trying to do achieve is to update the database value where id is equal to the id of the checkbox which the user has ticked. For example if the user tick the first checkbox(with the value of 49), and the form is submitted, the database will update the value of in the database where the id is 49.
This problem doesn't require any jQuery (or javascript for that matter). Since you are using a checkbox instead of a radio button I am assuming you would like to be able to update more than one of your entities at a time.
The simplest way to achieve this is to setup your form as follows:
<table>
<tr>
<td><input name="selected_ids[]" type="checkbox" value="49"/></td>
<td><input type="text" name="data[49][name]" value="First Input"/></td>
</tr>
<tr>
<td><input name="selected_ids[]" type="checkbox" value="50"/></td>
<td><input type="text" name="data[50][name]" value="Second Input"/></td>
</tr>
<tr>
<td><input name="selected_ids[]" type="checkbox" value="60"/></td>
<td><input type="text" name="data[60][name]" value="Third Input"/></td>
</tr>
<tr>
<td><input name="selected_ids[]" type="checkbox" value="38"/></td>
<td><input type="text" name="data[38][name]" value="Fourth Input"/></td>
</tr>
</table>
As you can see the checkboxes will represent entities that you would like to update. When the form submits your will end up with an array of 0 or more IDs, each corresponding with a checked checkbox. You can then loop through this array of IDs, grabbing it's matching name value in the data associative array. This loop will look something like this:
foreach($_POST['selected_ids'] as $id){
$name = $_POST['data'][$id]['name'];
//TODO: update the database
}
This setup allows you to very easily add more data for each entity by creating <input>s following the convention <input name="data[<entity id>][<database field>]">
This solution will work for your condition without any modification in your html markup.
If you get the id of check box then you can get value of input field related to that checkbox as follows:
<script>
var id = 49;//you will get this id after form submission, i assumed it 49 for example
$('input[type="checkbox"]').each(function(){
if($(this).attr('id')===id){
var desired_value = $(this).parent().find('input[type="text"]').val();
}
});
console.log(desired_value);//First Input// use desired value as you like
</script>
I hope it helps
Related
I am working in a small project and I have a code to do a multiple post of fields values when clicking over a check box. I have a table which lists classes and time. I need to know how to click over a checkbox and send class and time values at same time using a loop.
This is driving me crazy. I have spent hours and I can't figure out how to send multiple values at same time. When I check all the values, it works perfect but if I click one or two values, I always get a wrong time value shown.
Here is the PHP code, I am using:
if(isset($_POST['submit'])){
if((!empty($_POST['class'])) && (!empty($_POST['time']))) {
$checked_count = count($_POST['class']);
echo "You have selected following ".$checked_count." option(s): <br/>";
// Loop to store and display values of individual checked checkbox.
for ($i=0; $i < $checked_count; $i++) {
echo $_POST['class'][$i]. " ";
echo $_POST['time'][$i]. "<br>";
}
}
}
else{
echo "<b>Please Select At least One Option.</b>";
}
Here is my HTML CODE:
<form action="checkclass.php" method="post">
<label class="heading">Select Your Technical Exposure:</label><br />
<tr>
<td><input type="checkbox" name="class[]" value="C/C++"><label>C/C++</label></td>
<td><input type="text" name="time[]" value="1" width="15"><br /></td>
</tr>
<tr>
<td><input type="checkbox" name="class[]" value="Java"><label>Java</label></td>
<td><input type="text" name="time[]" value="2" width="15"><br /></td>
</tr>
<tr>
<td><input type="checkbox" name="class[]" value="PHP"><label>PHP</label></td>
<td><input type="text" name="time[]" value="3" width="15"><br /></td>
</tr>
<tr>
<td><input type="checkbox" name="class[]" value="HTML/CSS"><label>HTML/CSS</label></td>
<td><input type="text" name="time[]" value="2" width="15"><br /></td>
</tr>
<tr>
<td><input type="checkbox" name="class[]" value="LINUX"><label>UNIX/LINUX</label></td>
<td><input type="text" name="time[]" value="3" width="15"><br /></td>
</tr>
<input type="submit" name="submit" Value="Submit"/>
</form>
If I check all the values. I get the following result:
Class Time
C/C++, 1
Java, 2
PHP, 3
HTML/CSS, 2
UNIX/LINUX, 3
If I only check the UNIX/LINUX class I get:
Class Time
UNIX/LINUX, 1
I get 1 for LInux time when it should be 3. I think the time field takes the first position in the array when click just one class.
How do I relate time and class values so that whole values in a row are posted at same time. I really want to do a multiple insert but I think solving this will help me.
I will appreciate any help with this matter.
Try this:
PHP Code:
<?php
if(isset($_POST['submit'])){
if((count($_POST['class']) > 0) && (count($_POST['time']) > 0)) {
$checked_count = count($_POST['class']);
echo "You have selected following ".$checked_count." option(s): <br/>";
// Loop to store and display values of individual checked checkbox.
foreach ($_POST['class'] as $k => $v) {
echo $_POST['class'][$k]. " ";
echo $_POST['time'][$k]. "<br>";
}
} else {
echo "<b>Please Select At least One Option and the Number of Years of Technical Exposure.</b>";
}
}
?>
HTML Code:
<form action="checkclass.php" method="post">
<label class="heading">Select Your Technical Exposure:</label><br />
<tr>
<td><input type="checkbox" name="class[0]" value="C/C++"><label>C/C++</label></td>
<td><input type="text" name="time[0]" value="1" width="15"><br /></td>
</tr>
<tr>
<td><input type="checkbox" name="class[1]" value="Java"><label>Java</label></td>
<td><input type="text" name="time[1]" value="2" width="15"><br /></td>
</tr>
<tr>
<td><input type="checkbox" name="class[2]" value="PHP"><label>PHP</label></td>
<td><input type="text" name="time[2]" value="3" width="15"><br /></td>
</tr>
<tr>
<td><input type="checkbox" name="class[3]" value="HTML/CSS"><label>HTML/CSS</label></td>
<td><input type="text" name="time[3]" value="2" width="15"><br /></td>
</tr>
<tr>
<td><input type="checkbox" name="class[4]" value="LINUX"><label>UNIX/LINUX</label></td>
<td><input type="text" name="time[4]" value="3" width="15"><br /></td>
</tr>
<input type="submit" name="submit" Value="Submit"/>
</form>
It worked for me. Let me know if it worked for you.
Documents Original Xerox
-------------------------------------------------
ABC <checkbox> <checkbox>
DEF <checkbox> <checkbox>
XYZ <checkbox> <checkbox>
I have a checkbox structure, as you see it above.
But I am not sure, how am I suppose to implement it.
I know the working of a normal checkbox, as it has only one checkbox.
But in this case, for every document, there are 2 checkbox (original copy and xerox copy)
What I have in mind like, after the submit is clicked, I need to somehow get an array in a form like below
ABC -> [1][1]
DEF -> [0][1]
XYZ -> [1][0]
Where first [] is orginal, and second [] is xerox.
Where 1 denotes that it was checked, and 0 denotes unchecked.
I am not getting an idea how should I implement it.
This is my code...(It may be horribly wrong, i agree.. but i still i made an attempt.. but couldn't figure it out.)
<table>
<tr>
<td>Aadhar</td>
<td><input type="checkbox" name="document[]" value="1"/></td>
</tr>
<tr>
<td>Pan Card</td>
<td><input type="checkbox" name="document[]" value="1"/></td>
</tr>
<tr>
<td>Address</td>
<td><input type="checkbox" name="document[]" value="1"/></td>
</tr>
<tr>
<td>Light Bill</td>
<td><input type="checkbox" name="document[]" value="1"/></td>
</tr>
<tr>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
PHP
$documents = $_POST['document'];
$a = implode($documents);
echo $a;
So far, I am getting 1 (value) correctly.. But I also need the document (key) to which the '1'(value) belongs.
Can anyone help me out please.
Checkboxes only submit WHEN CHECKED, so you'll NEVER get a checkbox submitting a "0" value.
That said, you can know it's NOT checked by simply asking PHP if it was set, like so: isset($_POST['document']['aadhar'][1]) -- if that returns true, it was checked, if not, it was not checked.
One thing to note: You're going to want to specify indexes for your second dimension of the array, so this:
<td><input type="checkbox" name="document[aadhar][]" value="1"/></td>
Becomes this:
<td><input type="checkbox" name="document[aadhar][original]" value="1"/></td>
That way you can track the "column" of which checkbox was selected by asking isset().
Here is a working example: http://myingling.com/random/random/44476276/
Here is the HTML from that example:
<form action='post.php' method='post'>
<table>
<tr>
<td>Aadhar</td>
<td><input type="checkbox" name="document[aadhar][col1]" value="1"/></td>
<td><input type="checkbox" name="document[aadhar][col2]" value="1"/></td>
</tr>
<tr>
<td>Address</td>
<td><input type="checkbox" name="document[light][col1]" value="1"/></td>
<td><input type="checkbox" name="document[light][col2]" value="1"/></td>
</tr>
<tr>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
</form>
Here is the PHP from the post.php:
<?php
echo "<pre>";
var_dump($_POST);
echo "</pre>";
I have two input box in a row one for style id and other for style name. but these boxes can be added dynamically to form. So can i say save these two array to code igniter db.
<table>
<tr>
<td><input type="text" name="key[]" placeholder="Style ID"></td>
<td><input type="text" name="value[]" placeholder="style Name"></td>
</tr>
<tr>
<td><input type="text" name="key[]" placeholder="Style ID"></td>
<td><input type="text" name="value[]" placeholder="style Name"></td>
</tr>
</table>
My Table structure is like
u_id | Style_id | style_name
How can i save them to db?
I'm making check list for a form that will send an email with to myself letting me know what items have been checked.
I have successful done this so far with just single inputs:
HTML:
<input type="text" name="visitorfirst" size="35" />
PHP:
From: $visitorfirst \n
This sends their name from the form to my email fine.
What I can't manage to do is have x amount of check boxes that all have the same name e.g. col with seperate values and send each one selected to my email if selected.
This is what I've tried:
HTML:
<form action="">
<table class="checks">
<tr>
<td><input type="checkbox" name="col" value="Desktop">Desktop</td>
<td><input type="checkbox" name="col" value="Laptop">laptop</td>
<td><input type="checkbox" name="col" value="Monitor">Monitor</td>
</tr>
<tr>
<td><input type="checkbox" name="col" value="Server">Server</td>
<td><input type="checkbox" name="col" value="Networking">Networking</td>
<td><input type="checkbox" name="col" value="Smartphone">Smartphone</td>
</tr>
<tr>
<td><input type="checkbox" name="col" value="Tablet">Tablet</td>
<td><input type="checkbox" name="col" value="Printer">Printer</td>
<td><input type="checkbox" name="col" value="Telephone">Telephone</td>
</tr>
</table>
</form>
PHP:
What hardware to collect: $col \n
How can I get the form to send multiple "checks"?
you need to create a checkbox group you do this by adding []
<td><input type="checkbox" name="col[]" value="Server">Server</td>
<td><input type="checkbox" name="col[]" value="Networking">Networking</td>
<td><input type="checkbox" name="col[]" value="Smartphone">Smartphone</td>
Your post return now returns an array so $_POST['col'] is an array you can then foreach loop through it or implode it to a csv or serialise it or json encode it or do whatever you like with it.
(In your case probably for each to generate your email code)
can someone give me help, please.
here's my basic html
<form action="addSomething.php" method="POST">
<table>
<tr>
<th>Add Data</th>
<th>Description</th>
<th>Quantity</th>
</tr>
<tr>
<td><input type="checkbox" name="data[]" value="sample1" /> </td>
<td class="desc">Newbie</td>
<td>2</td>
</tr>
<tr>
<td><input type="checkbox" name="data[]" value="sample1" /> </td>
<td class="desc">Pro</td>
<td>1</td>
</tr>
<tr>
<td><input type="checkbox" name="data[]" value="sample1"/> </td>
<td class="desc" > Master </td>
<td>1</td>
</tr>
<br/>
<input type="submit" name="add" value="SUBMIT"/>
.....
how can I get the one with the class "desc" and the column for quantity that is checked after submitting the form
because the only I can add when querying in mysql is the value of checkbox but I want also the value of the data in "Description" column and "Quantity" column
In my addSomething.php the code I have is
if(isset($_POST['add']))
{
foreach($_POST['data'] as $value)
{
$sql = "INSERT INTO tablename (column1) VALUES('$value');"
//query stuff
}
}
what I will do , Any hints guys?
you can give your checkboxes different values
<input type="checkbox" name="data[]" value="newbie" />
<input type="checkbox" name="data[]" value="pro" />
<input type="checkbox" name="data[]" value="master" />
and then in addSomething.php define an array
$names = array('newbie'=>'Newbie', 'pro'=>'Pro', 'master'=>'Master');
and use it in your sql
if(isset($_POST['add']))
{
foreach($_POST['data'] as $value)
{
$sql = "INSERT INTO tablename (column1) VALUES ('".$names[$value]."');";
}
}
this applies only if you don't want users to edit the decription and quantity on the frontend. if you do, you need to put inputs in there and give them unique names.
I'd advise something along the lines of:
<form action="addSomething.php" method="POST">
<table>
<tr>
<th>Add Data</th>
<th>Description</th>
<th>Quantity</th>
</tr>
<tr>
<td><input type="checkbox" name="data[0]" value="sample1" /> </td>
<td class="desc">Newbie<input type="checkbox" name="desc[0]" value="Newbie" /></td>
<td>2<input type="checkbox" name="quan[0]" value="2" /></td>
</tr>
<tr>
<td><input type="checkbox" name="data[1]" value="sample1" /> </td>
<td class="desc">Pro<input type="checkbox" name="desc[1]" value="Pro" /></td>
<td>1<input type="checkbox" name="quan[1]" value="1" /></td>
</tr>
<tr>
<td><input type="checkbox" name="data[2]" value="sample1"/> </td>
<td class="desc" > Master <input type="checkbox" name="desc[2]" value="Master" /></td>
<td>1<input type="checkbox" name="quan[2]" value="1" /></td>
</tr>
<br/>
<input type="submit" name="add" value="SUBMIT"/>
...
with php
if(isset($_POST['add']))
{
foreach($_POST['data'] as $i => $value) {
$desc = mysql_real_escape_string($_POST['desc'][$i]);
$quan = mysql_real_escape_string($_POST['quan'][$i]);
$sql = "INSERT INTO tablename (desc,quan) VALUES('$desc','$quan');"
//query stuff
}
}
You have to explicitly number your fields in html to preserve associacion of data with checkboxes.
What you could do is write Javascript that triggers an AJAX request to your server.
Otherwise, you won't be able to get values in prior submit.
That, is by far, very inefficient. Executing a query for each field in your table.
You will either have to put the description in a hidden field or read the description from the database.
I suggest the later option.
You'll need to include these values in a hidden input
<input type="hidden" name="whatever" value="whatever>"
as an example
I think the best way for your code is you create a javascript function and call it on submit of form :
<form action="addSomething.php" method="POST" onsubmit="return postVars()">
when you submit the form, this function will be called. create a 2 hidden forms like this :
<input type="hidden" name="description" id="description">
<input type="hidden" name="quantity" id="quantity">
give a unique id to your td s and pass the id to your js function. fill these inputs in your js function :
document.getElementById('description').value = document.getElementById('td_' + id + '_description').innerHTML;
document.getElementById('quantity').value = document.getElementById('td_' + id + '_quantity').innerHTML;
and in your server side just get description and quantity from $_POST.
hope it's been helpful ;)