i want to select a lot row of my database and have checkbox to any of rows that the user can choose any of them.then i should update database according to user choice, but i can't understand who of this checkbox is selected.
thanks a lot...
the semicode is here:
$query = "Select * mytable limit 30;";
$res = $mysql->ExecuteStatement (array ());
echo '<form method="post" action=""> ';
$b=0;
while ($rec=$res -> fetch())
{
echo '<input type="checkbox" id="$b" name="checkboxName[]" value="yes"/>';
$checkboxID = "number_".$rec["ID"].":";
echo $checkboxID;
echo $rec["column1"]."</br>";
$b++;
}
if(isset($_POST['formSubmit'])) //this code just get us how many of checkbox are chosen not who are chosen
{
foreach($_POST['checkboxName'] as $selected)
{
echo $selected."</br>";
}
}
Try some thing like this:
Provide a class to all checkboxes:
<input type="checkbox" class="MyChkbox" id="$b" name="checkboxName[]" value="yes"/>';
In JS:
$('.MyChkbox').click(function(){
if($(this).is(':checked'))
{
var checkedId = $(this).attr('id');
// checkedId will contain the checked checkbox id in it. Use it as per your need
}
});
Hi In these case you should pass the Id of row as value of Checkbox and then get them when form is posted so you can try this
echo '<td><input type="checkbox" class="MyChkbox" id="$b" name="checkboxName[]" value="' . $rec["ID"] . '"></td>';
these will have value as row ID
Related
In insert form I am using check boxes to insert values in database. Now, I want to get checked values in edit form so I can check new values or uncheck checked values.
First I get values from database (I am using pdo, I will not post connection code to db- it works):
get oprmea from database
$sql_oprema = "SELECT Oprema FROM dbo_emarketing_stavke_oprema WHERE Partner='$id'";
$n = $conn->query($sql_oprema);
while ($r = $n -> fetch()) {
$oprema_sql = $r['Oprema'];
}
I get values when I dump variables, output is not NULL.
I am using function to store values in database. Now I want to use same function for editing if posssible.
function emarketing_oprema(){
$link = new mysqli("localhost", "root", "le30mu09", "websoft");
$link->set_charset("utf8");
$sql=mysqli_query($link, "SELECT * FROM `dbo_emarketing_oprema` order by OpremaId asc ");
while($record = mysqli_fetch_array($sql)) {
echo '<input type="checkbox" name="oprema[]" value="'.$record['OpremaId']. '">' . $record['OpremaNaziv'] . ' <br/><br/> </input>';
}
}
I was wondering is it possible to use this function to get checked values checked.
use checked attribute
<input type="checkbox" checked>
or like this
<input type="checkbox" checked="checked">
You php will look like this:
while($record = mysqli_fetch_array($sql)) {
data='<input type="checkbox" name="oprema[]" value="'.$record["OpremaId"];
if(isset($record['checked'])) {//field in the database
data+=' checked="checked';
}
data+='">'. $record["OpremaNaziv"].'</br>';
}
EDIT QUESTION:
In my WHILE loop, it creates an readonly input text box, and the values are whatever records it pulls from my Database. After each text box, there is an add button, which is suppose to append the value of the text box beside the button, to another div on my page. The only value being added is the first record. I know why, I just can't fix. Any ideas?
$query = "SELECT `$posResults` FROM test.departments ";
$result = mysql_query($query);
if ($result) {
//output results from database
echo 'Software';
while($row = mysql_fetch_array($result))
{
echo '<tr>';
echo '<td><input type="text" id="rSoft" size="50" name="reqSoft" value="'.$row[0].'" readonly/> <button id="toForm" value="Add" >Add</button>';
echo '</td>';
echo '</tr>';
}
echo "<script type='text/javascript'>$(document).ready(function(){ $('button#toForm').click(function(){ var req = document.getElementById('rSoft').value; $('<tr><td>' + req + '</td></tr>').appendTo('#empInfo');}); });</script>";
}
Here is an image. When I Hit Add, I want the value to go to the div. Right now, only the first value is being appended.
Found out the answer that covers everything:
On my main page:
$query = "SELECT `$posResults` FROM test.departments ";
$result = mysql_query($query);
if ($result) {
//output results from database
echo 'Software';
echo '<table>';
$i = 0;
while($row = mysql_fetch_array($result))
{
echo '<tr><td><input type="text" id="rSoft'.$i.'" value="'.$row[0].'" name="reqSoft" readonly/>';
echo '<button id="toForm'.$i.'" data="rSoft'.$i.'" >Add</button>';
$i++;
}
echo '</table>';
}
On my JS page:
/*Add software to form*/
$(document).ready(function(){
$('button').on('click' , function(){
var inputID = $(this).attr('data');
var theValue = $('#' + inputID).val();
$('#empInfo').append('<tr><td>' + theValue + '</td></tr>')
});
This will append the data of the input box to where I need it to go. Thank you OIS and Kristen Jukowski for getting me started on the resolution!
});
The IDs of your inputs need to be unique. You can pass the values, though, as an array if you change the ID to something like this:
echo '<td><input type="text" id="reqSoft[]" size="50" name="reqSoft[]" value="'.$row[0].'" readonly/> <button id="toForm" value="Add" >Add</button>';
Your passed values will be available as an array in $_POST['reqSoft']. (First textbox value would be $_POST['reqSoft'][0], etc.)
Similar question/answer here: Dynamically create unique ID for an array of input fields using Javascript
I have this code which is supposed to print me every possible pdv which has an id=super_selected_1.
The issue is, if I print the $super_selected_1 inside the while it works, it takes the value, but inside the checkbox it won't show it,
Am i doin smth wrong here?
Thanks.
$super=array();
while ($row = mysql_fetch_array($query3)) {
$super[]=$row['id_pdv'];
}
foreach($super as $super_selected_1){
$sqlsuper_1="SELECT * FROM pdv WHERE id_pdv='$super_selected_1' AND regione_pdv='$term1'";
$querysuper_1 = mysql_query($sqlsuper_1) or die (mysql_error());
while ($row = mysql_fetch_array($querysuper_1,MYSQL_ASSOC)) {
echo $super_selected_1;
echo '<input type="checkbox" name="checkbox[]" id="checkbox[]" class="pdv_super" value="'.$row['id_pdv'].'" >' . $row['nome_pdv'] . '<br>';
}
}
Your checkbox is wrong. The value attribute does have no effect if the type is "checkbox". You need to set checked="checked" in your <input> tag if it shold be shown as checked.
I'm making a website on which I have a set of data with checkbox. I need to delete multiple rows from database when I select multiple checkboxes. I don't know how to pass the id of the selected checkboxes to next page. Please help me.
Here is my code:
$select_qry="select * from data";
$result=mysql_query($select_qry);
$rows=mysql_num_rows($result);
if($rows>0)
{
?>
<form action="delete_submit.php" method="post">
<?php
for($i=0;$i<$rows;$i++)
{
$arr=mysql_fetch_assoc($result);
$id=$arr['id'];
$name=$arr['name'];
//echo $id;echo "<br>";echo $name;
?>
<input name="checkbox[]" type="checkbox" value="<?php echo $id;?>">
<?php
}
}
?>
How can I pass the selected checkboxes' id to the next page? Please help me.
Thanks
when form will be submitted then $_POST['checkbox'] will be set and contain the array of selected ids.
In your delete script
if(isset($_POST['checkbox']) && count($_POST['checkbox']) > 0){
$deleteIds = $_POST['checkbox']; // it will be an array
$sql = "DEFRE FROM tablename WHERE id in (".implode("," , $deleteIds).") ";
// run the query
}
I have a form with several checkboxes which values are pulled from a database.
I managed to display them in the form, assign an appropriate value to each, but cannot insert their values into other database.
Here's the code:
<form id="form1" name="form1" method="post" action="">
<?php
$info_id = $_GET['info_id'];
$kv_dodatoci = mysql_query("SELECT * FROM `dodatoci`") or die('ERROR DISPLAYING: ' . mysql_error());
while ($kol = mysql_fetch_array($kv_dodatoci)){
$id_dodatoci = $kol['id_dodatoci'];
$mk = $kol['mk'];
echo '<input type="checkbox" name="id_dodatoci[]" id="id_dodatoci" value="' . $id_dodatoci . '" />';
echo '<label for="' . $id_dodatoci.'">' . $mk . '</label><br />';
}
?>
<input type="hidden" value="<?=$info_id?>" name="info_id" />
<input name="insert_info" type="submit" value="Insert Additional info" />
</form>
<?php
if (isset($_POST['insert_info']) && is_array($id_dodatoci)) {
echo $id_dodatoci . '<br />';
echo $mk . '<br />';
// --- Guess here's the problem ----- //
foreach ($_POST['id_dodatoci'] as $dodatok) {
$dodatok_kv = mysql_query("INSERT INTO `dodatoci_hotel` (id_dodatoci, info_id) VALUES ('$dodatok', '$info_id')") or die('ERROR INSERTING: '.mysql_error());
}
}
My problem is to loop through all checkboxes, and for each checked, populate a separate record in a database.
Actually I don't know how to recognize the which box is checked, and put the appropriate value in db.
You can tell if a checkbox is selected because it will have a value. If it's not selected, it won't appear in the request/get/post in PHP at all.
What you may want to do is check for the value of it and work based on that. The value is the string 'on' by default, but can be changed by the value='' attribute in HTML.
Here are a couple snippets of code that may help (not exactly production quality, but it will help illustrate):
HTML:
<input type='checkbox' name='ShowCloseWindowLink' value='1'/> Show the 'Close Window' link at the bottom of the form.
PHP:
if (isset($_POST["ShowCloseWindowLink"])) {
$ShowCloseWindowLink=1;
} else {
$ShowCloseWindowLink=0;
}
.....
$sql = "update table set ShowCloseWindowLink = ".mysql_real_escape_string($ShowCloseWindowLink)." where ..."
(assuming a table with a ShowCloseWindowLink column that will accept a 1 or 0)
As an extra note: You're using the wrong HTML syntax for IDs and <label>. <label>'s "for" attribute should point to an ID, not a value. You also need unique IDs for each element. The code you have posted would not validate.
Also, you're not validating your code at all. At the very least, do a htmlspecialchars() or htmlentities() on the input before you output it and a mysql_real_escape_string() before you insert data into the DB.
2nd Answer:
You might do something like this:
HTML:
echo '<input type="checkbox" name="id_dodatoci[]" value="'.$id_dodatoci.'" />';
PHP:
if ( !empty($_POST["id_dodatoci"]) ) {
$id_dodatoci = $_POST["id_dodatoci"];
print_r($id_dodatoci);
// This should provide an array of all the checkboxes that were checked.
// Any not checked will not be present.
} else {
// None of the id_dodatoci checkboxes were checked.
}
This is because you are using the same name for all of the checkboxes, so their values will be passed to php as an array. If you used different names, then each would have it's own post key/value pair.
This might help too:
http://www.php-mysql-tutorial.com/php-tutorial/using-php-forms.php
This is the loop that I needed. I realized that I need a loop through each key with the $i variable.
if(isset($_POST['id_dodatoci'])){
$id_dodatoci=$_POST['id_dodatoci'];
$arr_num=count($id_dodatoci);
$i=0;
while ($i < $arr_num)
{
$query="INSERT INTO `dodatoci_hotel`(id_dodatoci,info_id)
VALUES ('$id_dodatoci[$i]','$info_id')";
$res=mysql_query($query) or die('ERROR INSERTING: '.mysql_error());
$i++;
}
}
Well, as Eli wrote, the POST is not set, when a checkbox is not checked.
I sometimes use an additional hidden field (-array) to make sure, I have a list of all checkboxes on the page.
Example:
<input type="checkbox" name="my_checkbox[<?=$id_of_checkbox?>]">
<input type="hidden" name="array_checkboxes[<?=$id_of_checkbox?>]" value="is_on_page">
So I get in the $_POST:
array(2){
array(1){"my_checkbox" => array(1){[123]=>"1"}}
array(1){"array_checkboxes" => array(1){[123]=>"is_on_page"}}
}
I even get the second line, when the checkbox is NOT checked and I can loop through all checkboxes with something like this:
foreach ($_POST["array_checkboxes"] as $key => $value)
{
if($value=="is_on_page")
{
$value_of_checkbox[$key] = $_POST["my_checkbox"][$key];
//Save this value
}
}
Also something that few people use but that is quite nice in HTML, is that you can have:
<input type="hidden" name="my_checkbox" value="N" />
<input type="checkbox" name="my_checkbox" value="Y" />
and voila! - default values for checkboxes...!