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>';
}
Related
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
I am having trouble in getting checked values checked in form. I was trying to use the same function as I have used to insert values to print all values in edit form, and which are in other table inserted to mark them checked.
Function to insert values in database table in insert form and it works.
function emarketing_usluge(){
$link = new mysqli("localhost", "xxx", "xxx", "xxx");
$link->set_charset("utf8");
$sql=mysqli_query($link, "SELECT * FROM `jos_ib_emarketing_oprema` order by OpremaId asc ");
while($record = mysqli_fetch_array($sql)) {
echo '<input type="checkbox" name="usluge[]" value="'.$record['OpremaId ']. '">' . $record['OpremaNaziv'] . ' <br/><br/> </input>';
}
}
In this function I get list of all services and place them in checkboxes.
Now I want to edit form, and display all values that are checked by using same function.
First I make query to get values, I am using here pdo but for funcion files I have used mysqli.
Form for editing!
$sql_oprema = "SELECT a.Partner, a.OpremaId, a.Oprema, b.OpremaNaziv
FROM jos_ib_emarketing_stavke_oprema a
join jos_ib_emarketing_oprema b
on OpremaId = b.Oprema
WHERE a.Partner= $id";
$oprema = $conn->query($sql_oprema);
$row = $oprema ->fetch();
<div class="col-xs-6">
<input type="checkbox" id="oprema" onclick="Exposeoprema()">Oprema<br>
<div id="Scrolloprema" style="height:150;width:200px;overflow:auto;border:1px solid blue;display:none">
<?php
while($row = $oprema ->fetch()) {
$data='<input type="checkbox" name="oprema[]" value="'.$row["Oprema"].'"';
if(isset($row['Oprema'])) {//field in the database
$data.=' checked="checked';
}
$data.='">'. $row["OpremaNaziv"] .'</br>';
}
emarketing_oprema($data);
?>
</div>
</div>
I am trying print all service values by using function, but the ones that are checked they need to have check mark. I am getting problem and could not figure it out how to solve it.
Looking back to your SQL query, I don't see an extraction of checked field, you are not selecting it. So there is never going to be a $row['checked'] element of your query.
You should add:
$sql_oprema = "SELECT a.checked, a.Partner, a.OpremaId, a.Oprema, b.OpremaNaziv
FROM jos_ib_emarketing_stavke_oprema a
join jos_ib_emarketing_oprema b
on OpremaId = b.Oprema
WHERE a.Partner= $id";
I am using function to display all values from tables but the ones that are checked in another table to be checked.
I am trying to print all values from one table, print them in checkbox. Then I am using this function to display all values and mark values checked if they are selected in another table.
function emarketing_oprema_checked(){
$id = test_input($_GET['id']);
$link = new mysqli("localhost", "xxx", "xxx", "xxx");
$link->set_charset("utf8");
$sql=mysqli_query($link, "SELECT a.OpremaId, a.OpremaNaziv, b.Oprema, b.Partner
FROM jos_ib_emarketing_oprema a
LEFT JOIN jos_ib_emarketing_stavke_oprema b ON a.OpremaId = b.Oprema where Partner = $id ");
while($record = mysqli_fetch_array($sql)) {
$data='<input type="checkbox" name="oprema[]" value="'.$record["OpremaId"].'"';
if(isset($record['Oprema'])) {//field in the database
$data.=' checked="checked';
}
$data.='">'. $record["OpremaNaziv"] .'</br>';
echo $data;
}
}
As a result I got only checked values, not the ones that are not checked.
Here is my form
<div class="col-xs-6">
<input type="checkbox" id="oprema" onclick="Exposeoprema()">Oprema<br>
<div id="Scrolloprema" style="height:150;width:200px;overflow:auto;border:1px solid blue;display:none">
<?php
emarketing_oprema_checked();
?>
</div>
</div>
The isset($record['Oprema'] will always be true, even if the value of it is null because the field is exists.
You need to check the value of it.
Try:
if ($record['Oprema']) {
$data.=' checked="checked';
}
I'm updating some countries values into db table. All countries fetch from TBL_COUNTRY table. Then few countries store to another table. I'm using implode function to store multiple values. it works fine. it stored like this in my db table Afghanistan,Argentina,Austria,Bangladesh.
I have tried this code
<?php
$exp_str = explode(',', $model_availability);
foreach($exp_str as $get_str)
{
echo $get_str;
}
?>
This above code return this output AfghanistanArgentinaAustriaBangladesh
How do I put tick on the checkbox based on this value?
<?php
$sql = "SELECT * FROM ".TBL_COUNTRY." ORDER BY country_name ASC";
$exe = mysql_query($sql, $CN);
while($r = mysql_fetch_array($exe))
{
?>
<input type="checkbox" name="model_availability[]" value="<?=$r['country_name']?>" id="<?=$r['country_name']?>" />
<label for="<?=$r['country_name']?>"><?=$r['country_name']?></label>
<?php } ?>
<input type="checkbox" name="model_availability[]" value="<?=$r['country_name']?>" id="<?=$r['country_name']?>"<?=(in_array($r['country_name'],$model_availability)?" checked":"")?> />
//In the input box just add a checked attribute, you will get.
" id="" checked = "true" />
I have the following code:
<?php
$allform = $_POST['allform'];
parse_str($allform, $output);
$allquery = "SELECT * FROM wp_users";
$names = array();
$allresult = mysql_query($allquery) or die(mysql_error()); ?>
...
<?php
while ($rows = mysql_fetch_array($allresult)) {
$names[] = $rows['user_email'];
}
?>
The allform variable is a jQuery serialize string:
var allform = $('form#all').serialize();
Basically, I want to put the values from the form in the front end into a mysql select query in the back end.
The form is a bunch of checkboxes so the idea is that the SELECT something will have different number of values depending on what the user checks. Any ideas?
Thanks
The best thing to do could be something like this. Your checkboxes should be like this
<input type="checkbox" name="checkboxes[]" value="cream" />
<input type="checkbox" name="checkboxes[]" value="choco" />
<input type="checkbox" name="checkboxes[]" value="lime" />
server side you receive an array
$flavours = $_POST["checkboxes"];
$sql = "SELECT ".implode(',', $flavours)." FROM FLAVOURTABLE";