I'm working on a form that uploads an image to a server. The form contains checkboxes and select boxes formatted to a <table>. Each table row contains two columns. The First column iterates through a mysql query that displays a list of items (24 items to be exact) that is paired to a checkbox. The 2nd column iterates through a mysql query that displays list of clients.
What needs to be done are as follows:
When the form gets uploaded (via POST or GET) the checkboxes which only have check gets passed.
convert the subs_id (name parameter for the select box) to an array for easy processing for mysql insert.
Code Below. Thanks in advanced!
<form method="post" enctype="multipart/form-data" action="upload2.php">
Name: <input type="text" name="item_desc" value="">
Show Date: <input type="text" name="upload_date" class="date" size="10"> to <input type="text" name="expiry_date" class="date" size="10">
Item: <input type="file" name="item"> Display Length: <input type="number" value="8" name="show_time" size="3" style="width:50px"> secs
<table class="data-list" width="100%">
<tr>
<td>Widget</td>
<td>Client</td>
</tr>
<?php foreach($instance as $item):?>
<tr>
<td><input type="checkbox" id="checkAll"/> <?php echo $item['name']; ?></td>
<td><select id="selectBox" name="subs_id[]">
<option selected="selected" value="none">Select Client</option>
<?php foreach($subs as $client): ?>
<option value="<?php echo $item['id'] . '-' . $client['id']; ?>" client="<?php echo $client['client_name']; ?>" ><?php echo $client['client_name']; ?></option>
<?php endforeach ?>
</select>
</td>
</tr>
<?php endforeach; ?>
</table>
<br>
<input type="submit" value="upload">
</form>
<input type="checkbox" id="checkAll" name='client[]' value ="1" /> <?php echo $item['name']; ?>
name and value is missing
1 .Change your checkbox code to
<input type="checkbox" id="checkAll" name="checkId[]" value="<?php echo $item['name']; ?>"/> <?php echo $item['name']; ?>
Here, I am taking name as array checkId[] and value of checkbox value="<?php echo $item['name']; ?>"
This should work for you.
Your select box is fine. There is no need to change it. you can find value of select box by checking index.
I figured it out. What I did was detect when there was a change on the select box and assign the value of the select box to the check box's value.
$(".selectBox").change(function() {
$(this).parent().siblings().children('input').attr('value',$(this).val());
});
then, on the upload2.php, I used the code below to get the value of only the boxes with check
if(!empty($_POST['checkId'])) {
$filename = $this->unique_filename($this->extract_ext( $_FILES["item"]["name"]));
$folder = dirname(__FILE__) . '/items';
move_uploaded_file($_FILES['item']['tmp_name'],"{$folder}/{$filename}");
foreach($_POST['checkId'] as $check) {
//save to db
$split = explode("-",$check);
$subs_id = $this->use_table('clients')->where('client_name like "' . $split[2] . '%"')->where('site_id=' . $split[1])->fetch();
//$subs_id = $this->query('select * from clients where client_name like "Trinoma%" and site_id=1');
//print_r($subs_id[0][id]);
$data = array(
'item_name'=> 'widgets/' . strtolower(get_class($this)) . '/items/' . $filename,
'show_time'=> $_POST['show_time'],
'instance_id'=> $split[0],
'item_desc'=>$_POST['item_desc'],
'subs_id'=> $subs_id[0][id]
);
$this->use_table(TBL_NAME)->insert($data)->execute();
$mid = $this->last_insert_id();
$this->use_table(TBL_SCHED)->insert(array(
'multirotator_id'=>$mid,
'show_from'=>'00:00',
'show_to'=>'23:59',
'upload_date'=>$_POST['upload_date'],
'expiry_date'=>$_POST['expiry_date']
))->execute();
} //end of foreach
} //end of if(!empty($_POST['checkId']))
and voila! working code for me.
Related
**How to select checkbox in database mysqli
what should i write in checkbox value
please kindly help
i also implode function which convert array to string
did not find values for checkbox values to print. .....
Also check the output on PICTURE...
**
OUTPUT PIC
<?php
while($fetch=mysqli_fetch_array($select))
{
?>
<tr>
<td><?php echo $fetch["tableno"]?></td>
<td><?php echo $fetch["customerid"]?></td>
<td><?php echo $fetch["item"]?></td>
<td><?php echo $fetch["money"]?></td>
<td><button>Delete</button></td>
<td><a href="update.php?tableno=<?php echo $fetch["tableno"]; ?>"/><button>Update</button></td>
<td><form method="POST">
<input type="checkbox" name="chk[]" value="???">
<input type="submit" name="sub1" value="Button for Checkbox">
</td> </form>
</tr>
<?php
}?>
</table>
<p style="font-size:30px">The Customer has TO Selected these Items from Menu List
and therefore submitted to<br> Kitchener to Make the Food and
then waiter will serve the Food to Customer.</p>
</body>
</html>
<?php
if(isset($_POST['sub1']))
{
$chk=implode(',', $_POST['chk']);
echo "<br>Customer has selected the following checkbox Item to be Prepared and Eat the Food:<br/>";
echo "<br/>".$chk."<br><br/><br/><br/><br/>";
}
?>
Try this:
In you form, method to insert multiple checkboxes
<form method="POST" action="test2.php">
<input type="checkbox" name="chk[]" value="InputText1">InputText1<br>
<input type="checkbox" name="chk[]" value="InputText2">InputText2<br>
<input type="submit" name="sub1">
</form>
if(isset($_POST['sub1'])) // check the submitted form and print the values
{
echo "You selected the following checkbox:<br/>";
foreach ($_POST['chk'] as $value) {
echo $value."<br>";
}
One method is the value of a checkbox can be concatenated with all data and after submitting, check for selected check boxes with for each loop and print all the selected rows. Like below
<input type="checkbox" name="chk[]" value="<?php echo "<td>".$fetch["tableno"]."</td><td>".$fetch["customerid"]."</td><td>".$fetch["item"]."</td><td>".$fetch["money"]."</td>"; ?>">
And after posting use this :
if(isset($_POST['sub1'])) // check the submitted form and print the values
{
echo "Below Customers have placed orders:<br/>";
echo "<table>";
foreach ($_POST['chk'] as $value) {
echo "<tr>".$value."</tr>";
}
echo "</table>";
}
I would like to know how we can store the value selected in an auto populated drop down (HTML5) into a variable, all in PHP. I want to obtain this value BEFORE clicking the 'Submit' button.
<?php
$i=1;
$j=0;
$result=pg_exec($pgsql_conn,"select * from crm.product_info order by 1");
while ($row = pg_fetch_assoc($result))
{
?>
<td><input type="checkbox"
id= <?php
echo $row['product_id'];
?>
value= <?php
echo $i;
?>
name= "prod[]">
<?php echo $row['product_name']; ?>
</td>
<td>
<input type="button" value=<?php echo "$".$row['product_value']; ?> id="but">
</td>
<td>
Quantity
<select class="select" name="qty[]">
<?php echo "qty".$j; ?>
</select>
</td>
<td>
Amount
<input
id="amt"
type="text"
readonly
value=<?php
echo $row['product_value']
?> >
</td>
<?php
}
?>
I require the SELECTED value of 'quantity'.
This is how i auto populate the quantity. It is from 0-100.
$(document).ready(function() {
for(i=0;i<=100;i++)
{
$(".select").append("<option value=\""+i+"\">"+i+"</option>");
}
});
First of all, PHP is server side, so it can't handle changes made in the browser until a request is send to it. You will have to use javascript to know selected value in real time before submitting the form. You can manipulate this value using javascript. But if you really need to do some PHP with the selected value, your solution is to use Ajax for this.
So i have a table, that I load checkbox values into. Once the checkboxes are clicked, I use a submit button to save them into a database. However at the moment I can only Save one row at a time? I need to be able to save all clicked checkboxes into the datbase
For example i have 5 rows. If the checkboxes on row 2 and 3 are checked then they should be saved into the database however right now only the last clicked(row 3) are being saved into the database.
Heres my code so far
Php connect code
<?php
// Get a db connection.
$db = JFactory::getDbo();
// Create a new query object.
$query = $db->getQuery(true);
$query->select($db->quoteName(array('CV_ID', 'Classifier', 'Value')));
$query->from($db->quoteName('classvalues'));
// Reset the query using our newly populated query object.
$db->setQuery($query);
// Load the results as a list of stdClass objects (see later for more options on retrieving data).
$results = $db->loadObjectList();
?>
Loading table with checkboxes code
<form name="names" id="names" action="<?php echo JURI::current(); ?>" method="post">
<table border="5",th,td, cellspacing="5", cellpadding="5", width="500", align="left">
<tr>
<th>CV_ID</th>
<th>Classifier</th>
<th>Level</th>
</tr>
<?php foreach ($results as $row): ?>
<tr>
<td> <input type="checkbox" id="chk113" name="CV_ID" value="<?php echo $row->CV_ID ?> "/>
<label for="chk113"><?php echo $row->CV_ID ?> </label> </td>
<td> <input type="checkbox" id="chk111" name="Classifier" value="<?php echo $row->Classifier ?>"/>
<label for="chk111"><?php echo $row->Classifier ?></label> </td>
<td> <input type="checkbox" id="chk112" name="Value" value="<?php echo $row->Value ?>"/>
<label for="chk112"><?php echo $row->Value ?></label> </td>
</tr>
<?php endforeach ?>
</table>
<p><input id="submit" name="submit" type="submit" value="Submit Names" /></p>
</form>
Saving
?>
<?
if( (isset($_POST['CV_ID'])) || (isset($_POST['Classifier'])) || (isset($_POST['Value'])) ) {
//first name or last name set, continue-->
$CV_ID = $_POST['CV_ID'];
$Classifier= $_POST['Classifier'];
$Value= $_POST['Value'];
$db =& JFactory::getDBO();
$query = "INSERT INTO SessionTa (CV_ID, Classifier, Value) VALUES ('".$CV_ID."','".$Classifier."','".$Value."');";
$db->setQuery( $query );
$db->query();
} else {
}
?>
each checkbox in your form appears to have the same name. Trying naming them as an array, e.g.
<input type="checkbox" id="chk113" name="CV_ID[]" value="<?php echo $row->CV_ID ?> "/>
Notice the extra '[]'. At that point, you can print_r($_POST) in your saving php file to see the differences. Your db update logic might need to be tweaked as a result
So i've created an administration page that creates X-number of forms based on how many users we have in our database, each of which have a submit button next to them to submit changes to the dates we have in our DBs. My problem is that when I want to get the value of what gets posted I can't extract exactly what I need from what gets posted. It gets saved into an array called and when I print_r the array I get exactly what I want, which is:
[1] => "whatever date they typed in"
(obviously the 1 changes depending on which item they changed the date of)
I need be able to query my datebase by:
UPDATE users SET subdate="whatever they typed in" WHERE id="the array reference number"
I know exactly what I need to do, I'm just not as familiar with SQL as i'd like to be, so any help would be greatly appreciated. Thanks in advance.
Code for reference:
<div class="form-section grid12" id="changedates">
<h1>Change Dates</h1>
<?php
$query = mysql_query("SELECT * FROM users WHERE admin='y'");
?>
<table>
<?php
while($row = mysql_fetch_assoc($query)) {
?>
<tr>
<td>
<h5><?php echo $row['displayname'];?></h5>
</td>
<td>
<form action="" method="POST">
<input type="text" name="subdate[<? echo $row['id'] ?>]" value="<?php echo $row['submissiondate'];?>">
<input type="text" name="nextupdate[<? echo $row['id'] ?>]" value="<?php echo $row['nextupdate'];?>">
</td>
<td>
<input type="submit" value="Set Date" name="setdate">
</form>
</td>
<?php
}
?>
</table>
</div>
You could use foreach...
foreach ($_POST[nextupdate] as $rowId => $time)
{
// do db update
}
Edit: Just realised you have more than one input per form.
Why not name each input with an array name:
<input type="text" name="form_data[<?= $row_id ?>][subdate]">
<input type="text" name="form_data[<?= $row_id ?>][nextupdate]">
In PHP:
foreach ($_POST[form_data] as $rowId => $values)
{
$subdate = $values[subdate];
$nextupdate = $values[nextupdate];
// do SQL stuff
}
I am setting up a form using this PHP that loops through all records a user may have:
<?php foreach ($items as $row): ?>
<tr>
<td>
<?php echo form_hidden('id', $row->id); ?>
</td>
<td>
<?php echo '<strong>' . $row->name . '</strong>'; ?>
</td>
<td>
<?php echo form_input('number', $number); ?>
</td>
<td>
<?php echo form_input('registry', $registry); ?>
</td>
<td>
<?php echo form_checkbox('OK', $ok, $ok); ?>
</td>
</tr>
<?php endforeach; ?>
This gives me a form with the following look:
The idea here is that each row belongs to a unique ID/row in the database, and I would like to allow the user to edit all on the same page/form, using a single submit button.
What would be the best way of implementing this?
When this data is submitted, there should be a way of looping through each packet of information (from each user) in my controller. Would this be done via ajax/json?
This does not use codeigntier, but you should be familiar with the general technique before attempting to use CI to shortcut this process. Codeigniter will help you with rendering the form elements, performing validation, escaping your input and performing your query - but it will only help you (do anything) if you understand the basic principles involved. Hope this helps
MARKUP
<form action="/process.php">
<div>
<h2>GORDON</h2>
<input type="text" name="user[1][number]" /> <!-- The number corresponds to the row id -->
<input type="text" name="user[1][registry]" />
<input type="checkbox" name="user[1][ok]" value="1" />
</div>
<div>
<h2>ANDY</h2>
<input type="text" name="user[242][number]" />
<input type="text" name="user[242][registry]" />
<input type="checkbox" name="user[242][ok]" value="1" />
</div>
<div>
<h2>STEWART</h2>
<input type="text" name="user[11][number]" />
<input type="text" name="user[11][registry]" />
<input type="checkbox" name="user[11][ok]" value="1" />
</div>
<input type="submit" />
PHP
$users = $_REQUEST['user'];
foreach ($users as $rowId => $info){
// YOU SHOULD MAKE SURE TO CLEAN YOUR INPUT - THIS IS A GUESS AT WHAT YOUR DATA TYPES MIGHT BE
$id = (int) $rowId;
$number = (int) $info['number'];
$registry = mysql_real_escape_string($info['registry']);
$ok = (int) ($info['ok']);
$q = "UPDATE user SET number = $number, registry = '$registry', ok = $ok WHERE id = $id";
mysql_query($q);
// You may want to check that the above query was sucessful and log any errors etc.
}
There's no need to use ajax mate.
For each put a hidden input with the ID of the row in this format:
<input type="hidden" name="id[<?= $row->id ?>]" value="<?= $row->id ?>" ?>
Do the same for each element in the tr, i.e. name them as
name="number[<?= $row->$id ?>]"
name="registry[<?=$row->$id ?>]"
name="ok[<?=$row->$id ?>]"
and once you post the FORM you can iterate each row with:
foreach ($_POST['id'] as $key => $value) {
echo $_POST['name'][$key];
}
You need to set up input-names as array-names, so you will send the whole form and may iterate over the entries.
e.g.
<?php
echo form_input('userdata[' . $row->id . '][number]', $number);
?>
which would possibly create an
<input name="userdata[1][number]" />
(I don't know where those form-functions came from…)
This will result in an array $_POST['userdata'] which may be iterated via:
foreach($_POST['userdata'] as $userId => $userInputFields)
{
$user = new User($userId);
$user->number = $userInputFields['number'];
// …
}