I have the following code. I want to get only selected checkbox values but I get only last checkbox value. see what's wrong in my code.
<div class="box-body table-responsive no-padding">
<h4>Select Jobwork</h4>
<table id="data-table" class="table table-hover jobworks-table table-responsive">
<thead>
<tr style="background-color: #DDDD; color:firebrick;">
<th><input type='checkbox' value="1" name="select_all" /></th>
<th>JobWork Name</th>
<th>Description</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<?php foreach ($item as $key=>$value){
$decoded = json_decode($value['jobWorkJsonString']); ?>
<?php foreach($decoded as $row){ ?>
<tr class="odd gradeX">
<td><input type='checkbox' name="<?php echo $row->jobwork; ?>" /></td>
<td style="padding-left:0px;"><?php echo $row->jobwork; ?><input type="hidden" name="jobwork_name" value="<?php echo $row->jobwork; ?>"></td>
<td style="padding-left:0px;"><?php echo $row->description; ?><input type="hidden" name="jobwork_description" value="<?php echo $row->description; ?>"></td>
<td style="padding-left:0px;"><?php echo $row->jobPrice; ?><input type="hidden" name="price" value="<?php echo $row->jobPrice; ?>"></td>
<?php } ?>
</tr>
<?php } ?>
</tbody>
</table>
<br>
</div>
If you submit multiple fields with the same name, then PHP will only put the last one into $_POST / $_GET.
The exception is when you end the name with [] in which case it will generate an array instead.
However, checkboxes are only successful controls when checked, so simply adding [] to the names will cause the association between each set of date to be lost.
Instead of submitting all the data about each jobwork (you aren't trying to change it, otherwise you wouldn't be using hidden inputs!), put all the data you need into the checkbox input. Look up the rest of the data on the server.
<td><input type='checkbox' name="jobwork[]" value="<?= htmlspecialchars($row->id); ?>"/></td>
I've used $row->id as an example. I don't know how your data is structured on the server.
You will then be able to do:
foreach ($_POST['jobwork'] as $jobwork_id) {
$row = look_up_jobwork_by_id($jobwork_id);
}
… only the checked checkboxes values will appear in that array.
You have issue in the first checkbox code.
Name is given as value, both names should be same for the checkbox,
and get the request as an array
<?php foreach ($item as $key=>$value){
$decoded = json_decode($value['jobWorkJsonString']); ?>
<?php foreach($decoded as $row){ ?>
<tr class="odd gradeX">
<td><input type='checkbox' name="jobwork" value="<?php echo $row->jobwork; ?>" /></td>
<td style="padding-left:0px;"><?php echo $row->jobwork; ?><input type="hidden" name="jobwork_name" value="<?php echo $row->jobwork; ?>"></td>
<td style="padding-left:0px;"><?php echo $row->description; ?><input type="hidden" name="jobwork_description" value="<?php echo $row->description; ?>"></td>
<td style="padding-left:0px;"><?php echo $row->jobPrice; ?><input type="hidden" name="price" value="<?php echo $row->jobPrice; ?>"></td>
<?php } ?>
</tr>
<?php } ?>
get the value of job_work as an array, you will get the selected job_work
Related
I have this table in my View that contains the single form.
like below . in this table , I show all rows of one of my table of database and showing it to the user . then user must fill empty inputs of this form and then hit the submit button to submit the form and then I update the table in my controller with new data that user filled recently.
<table id="myTable" class="display">
<form action="" method="POST">
<thead>
<tr>
<th>آیدی</th>
<th>کد پرسنلی</th>
<th>نام</th>
<th>نام خانوادگی</th>
<th>روزانه</th>
<th>لیست</th>
<th>سرویس</th>
<th>سایر</th>
<th>جمع</th>
<th>آسفالت</th>
<th>توقف شب</th>
<th>غیبت</th>
<th>کمک هزینه تست</th>
<th>توضیحات</th>
</tr>
</thead>
<tbody>
<?php if (isset($allReadyTasks) and $allReadyTasks) { ?>
<?php foreach ($allReadyTasks as $task): ?>
<tr>
<td><?php echo $task->id;?></td>
<td><?php echo $task->personalCode;?></td>
<td><?php echo $task->fname;?></td>
<td><?php echo $task->lname;?></td>
<td><input class="form-control taskInputs" value="<?php echo $task->daily; ?>"</td>
<td><input class="form-control taskInputs" value="<?php echo $task->list; ?>"</td>
<td><input class="form-control taskInputs" value="<?php echo $task->service; ?>"</td>
<td><input class="form-control taskInputs" value="<?php echo $task->other; ?>"</td>
<td><input class="form-control taskInputs" value="<?php echo $task->sum; ?>"</td>
<td><input class="form-control taskInputs" value="<?php echo $task->asphalt; ?>"</td>
<td><input class="form-control taskInputs" value="<?php echo $task->nightPause; ?>"</td>
<td><input class="form-control taskInputs" value="<?php echo $task->absent; ?>"</td>
<td><input class="form-control taskInputs" value="<?php echo $task->allowance; ?>"</td>
<td><textarea class="form-control"></textarea></td>
</tr>
<?php endforeach; ?>
<?php } ?>
</tbody>
</form>
</table>
my question is : all the inputs have the save name . how can I update the all rows of my table of database with one form and inputs that have same name .!?
Try something like this
<input type="number" class="form-control" name="inputs[]"/>
After that,when you get it with POST, you will have an array of inputs and you can loop throw it with a foreach.
i want to make a cart and my product is pizza.
so i'm listing all the menu to the table, and add a button to submit menu to cart.
here's my index.php
<?php
require("connect.php");
$result = mysqli_query($con,'select * from menu');
?>
<form action="cart.php" method="get">
<table border="1" style="width:100%">
<tr>
<th>Pizza Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Buy</th>
</tr>
<?php while($product = mysqli_fetch_object($result)){ ?>
<tr>
<td><?php echo $product->nama_menu; ?></td>
<td><?php echo $product->harga_menu; ?></td>
<td><input type="number" name="quantity" min="1" max="20"></td>
<td><button type="submit" name="id" value="<?php echo $product->id_menu; ?>">Add To Cart</button></td>
</tr>
<?php } ?>
</table>
</form>
But when i pressed the button "Add To Cart", it sends all the quantity from the menu listing and can't be read in my cart.php
can anyone help me how to get the right quantity value when i pressed the button add to cart.
Make separate form for each of the item. Try below code.
<?php
require("connect.php");
$result = mysqli_query($con,'select * from menu');
?>
<table border="1" style="width:100%">
<tr>
<th>Pizza Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Buy</th>
</tr>
<?php while($product = mysqli_fetch_object($result)){ ?>
<form action="cart.php" method="get">
<tr>
<td><?php echo $product->nama_menu; ?></td>
<td><?php echo $product->harga_menu; ?></td>
<td><input type="number" name="quantity" min="1" max="20"></td>
<td><button type="submit" name="id" value="<?php echo $product->id_menu; ?>">Add To Cart</button></td>
</tr>
</form>
<?php } ?>
</table>
Try to use array in name then submit it will give you seperate quantity.
<td><input type="number" name="quantity[<?php echo $product->nama_menu; ?>]" min="1" max="20">
Output:
quantity['pizza1'] = 10
quantity['pizza2'] = 20
....
Another Option is use dynamic name for number.
<td><input type="number" name="quantity_<?php echo $product->nama_menu; ?>" min="1" max="20">
Output:
quantity_pizza1 = 10
quantity_pizza2 = 20
....
So the below code grabs the server_id from the row that the user selects. However, I am wondering if it is possible to grab both the server_id and server_name from the row without the user having to do anymore than he already does (select a radio button and push submit). Thanks for any help.
<form method="post" name="server_information" id="server_information" action="test.php">
<label>Server Information</label><br><br>
<table border='1'>
<tr>
<th>Selection</th>
<th>User Name</th>
<th>Date Created</th>
<th>Server Name</th>
<th>Server Id</th>
<th>Public DNS</th>
<th>Server Status</th>
</tr>
<?php while($row = mysql_fetch_assoc($result)): ?>
<tr>
<td><input type="radio" name="server" value="<?php echo $row['serverId']; ?>" /></td>
<td><?php echo $row['userName']; ?></td>
<td><?php echo $row['dateCreated']; ?></td>
<td><?php echo $row['serverName']; ?>"</td>
<td><?php echo $row['serverId']; ?></td>
<td><?php echo $row['publicDNS'] ?></td>
<td><?php echo $row['serverStatus']; ?></td>
</tr>
<?php endwhile; ?>
</table>
<br>
<?php endif; ?>
<input type="submit" name="server_stop" value="Stop Server"/>
<input type="submit" name="server_terminate" value="Terminate Server"/>
</form>
You can put a pipe | or any other character to concatenate both in the radio value, then explode your $_POST['server'] on pipe, you'll get id in index 0 and name in index 1.
<td><input type="radio" name="server" value="<?php echo $row['serverId'].'|'.$row['serverName']; ?>" /></td>
Another solution would be to add a hidden input and name it with the server id like this:
<td>
<input type="radio" name="server" value="<?php echo $row['serverId']; ?>" />
<input type="hidden" name="name_<?php echo $row['serverId'];?>" value="<?php echo $row['serverName'];?>" />
</td>
then in PHP
$server_id=(int)$_POST['server'];
$server_name=$_POST['name_'.$server_id];
You can use an array:
name="server[$row['serverId']]" value="$row['serverName']"
Then get it with:
list($id, $name) = $_POST['server'];
Sure, just concatenate the two values with a comma (or other delimiter that won't appear in the values):
<tr>
<td><input type="radio" name="server"
value="<?php echo $row['serverId'] . ',' . $row['serverName']; ?>" /></td>
<td><?php echo $row['userName']; ?></td>
<td><?php echo $row['dateCreated']; ?></td>
<td><?php echo $row['serverName']; ?>"</td>
<td><?php echo $row['serverId']; ?></td>
<td><?php echo $row['publicDNS'] ?></td>
<td><?php echo $row['serverStatus']; ?></td>
</tr>
Then when you process the form use explode() to separate the results:
$server_info = explode(",", $_POST["server"]); // server_info is an array
$server_selection = $server_info[0];
$server_name = $server_info[1];
Im having a page that shows monthly subscriptions of a user which is created using codeigniter. what i want to do is when a the user clicks on make payment pass the values in the hidden files to the controller.
<?php echo form_open('options/done');?>
<table class="tables">
<thead>
<tr>
<th>Ref Code</th>
<th>Month</th>
<th>Year</th>
<th>action/th>
</tr>
</thead>
<tbody>
<?php foreach ($payments as $s =>$payment):?>
<?php $month = $payment['month'];?>
<input type="hidden" value="<?php echo $month;?>" name="month_<?php echo $s;?>" />
<input type="hidden" value="<?php echo $payment['ref_code'];?>" name="ref_<?php echo $s;?>" />
<tr>
<td><?php echo $payment['ref_code'];?></td>
<td><?php echo $month;?></td>
<td><?php echo $payment['year'];?></td>
<td><input type="submit" value="MAKE PAYMENT" class="red" /></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php echo form_close();?>
so when someone hits the submit button how can i pass only the hidden values which are relevant to that table row?
add form just inside the <tr> elements inside your loop (see below with your code)
<?php foreach ($payments as $s =>$payment):?>
<?php $month = $payment['month'];
?>
<tr>
<form action="target.php" method="post" name="formName_<?php echo $s;?>" >
<input type="hidden" value="<?php echo $month;?>" name="month_<?php echo $s;?>" />
<input type="hidden" value="<?php echo $payment['ref_code'];?>" name="ref_<?php echo $s;?>" />
<td><?php echo $payment['ref_code'];?></td>
<td><?php echo $month;?></td>
<td><?php echo $payment['year'];?></td>
<td><input type="submit" value="MAKE PAYMENT" class="red" /></td>
</form>
</tr>
<?php endforeach; ?>
The script below computes the subtotal and total of given items.
<script type="text/javascript">
$(function(){
$('input[type=button]').click(function(){
});
$('input[id^=qty]').keyup(function(){
var gtotal=0;
var parentDiv = $(this).closest('#korokor');
var curprice=parentDiv.find('input[id^=price]').val();
var curqty= this.value;
var curtotal=curprice * curqty;
parentDiv.find('input[id^=subtotal]').val(curtotal);
$('input[id^=subtotal]').each(function(index) {
var currentnum= $(this).val();
var intnum =parseInt(currentnum);
gtotal=gtotal + intnum;
});
$('input[name=supertotal]').val(gtotal);
$('input[name=payment]').val(gtotal);
});
});
</script>
<?php
$viewcarter=mysql_query("SELECT * FROM prod_table WHERE CURRP=1");
?>
</head>
<body>
<table border="1">
<tr>
<th>ID</th>
<th>PRODUCT</th>
<th>QA</th>
<th>PRICE</th>
<th>QTY BUY</th>
<th>SUBTOTAL</th>
<th>REMOVE</th>
</tr>
</table>
<?php
if(mysql_num_rows($viewcarter)==0){
//no products
}else{
while($row=mysql_fetch_assoc($viewcarter)){
?>
<form name="blabber" method="get" action="expander.php">
<div id="korokor">
<table border="1">
<tr>
<td><?php echo $row['PID']; ?></td>
<td><?php echo $row['PRODUCT']; ?></td>
<td><?php echo $row['QTYHAND']; ?></td>
<td><?php echo $row['S_PRICE']; ?></td>
<input type="hidden" id="pids" name="ids[]" value="<?php echo $row['PID']; ?>">
<input type="hidden" id="pname" name="product[]" value="<?php echo $row['PRODUCT']; ?>">
<input type="hidden" name="dprice[]" value="<?php echo $row['B_PRICE']; ?>">
<input type="hidden" name="sprice[]" id="<?php echo 'price'.$row['PID']; ?>" value="<?php echo $row['S_PRICE']; ?>"/>
<input type="hidden" name="qoh[]" value="<?php echo $row['QTYHAND']; ?>"/>
<td><input type="text" name="qbuys[]" id="<?php echo 'qty'.$row['PID']; ?>"/></td>
<td><input type="text" name="subtotal[]" id="<?php echo 'subtotal'.$row['PID']; ?>"/></td>
<td><img src="delete-icon.png"></img></td>
</tr>
</table>
</div>
<?php
}
?>
Grand Total:<input type="text" name="supertotal" value=""/><br/>
Customer:<input type="text" name="customer" value=""/><br/>
Payment:<input type="text" name="payment" value=""/><br/>
<input type="submit" value="sell"/>
</form>
<?php
}
?>
It works, but I have a little problem with its appearance:
I tried to place them all in one table to make it more pleasing but the script which computes the values at keyup no longer works. I need a little help in modifying the appearance of the interface.
Use a single table...
<form name="blabber" method="get" action="expander.php">
<div id="korokor">
<table border="1">
<tr>
<th>ID</th>
<th>PRODUCT</th>
<th>QA</th>
<th>PRICE</th>
<th>QTY BUY</th>
<th>SUBTOTAL</th>
<th>REMOVE</th>
</tr>
<?php
if(mysql_num_rows($viewcarter)==0){
//no products
}else{
while($row=mysql_fetch_assoc($viewcarter)){
?>
//now the loop adds a new row rather than new table for each item in the cart.
<tr class="cartItem">
<td><?php echo $row['PID']; ?></td>
<td><?php echo $row['PRODUCT']; ?></td>
<td class="qnty"><?php echo $row['QTYHAND']; ?></td>
<td class="unitPrice"><?php echo $row['S_PRICE']; ?></td>
<td><input type="text" name="qbuys[]" id="<?php echo 'qty'.$row['PID']; ?>"/></td>
<td><input type="text" name="subtotal[]" id="<?php echo 'subtotal'.$row['PID']; ?>"/></td>
<td><img src="delete-icon.png"></img>
<input type="hidden" id="pids" name="ids[]" value="<?php echo $row['PID']; ?>">
<input type="hidden" id="pname" name="product[]" value="<?php echo $row['PRODUCT']; ?>">
<input type="hidden" name="dprice[]" value="<?php echo $row['B_PRICE']; ?>">
<input type="hidden" name="sprice[]" id="<?php echo 'price'.$row['PID']; ?>" value="<?php echo $row['S_PRICE']; ?>"/>
<input type="hidden" name="qoh[]" value="<?php echo $row['QTYHAND']; ?>"/>
</td>
</tr>
<?php
}
?>
</table>
</div>
That should fix much of the layout, having loads of tables as you were trying to do is just not a good idea. If the javascript is not working with a single table then it should be adapted so that it does.
I will take a look at that now...
This is only very rough im afraid (contact lenses dried out all of a sudden).. but
var runningSub = 0;
var total = 0;
$('.cartItem').each(function(){
var qnty = $('this .qnty').html();
var unitPrice = $('this .unitPrice').html();
runningSub + (qnty * unitPrice);
)};
// the cart items have been processed so..
if(runningSub >0){
total = (calc for the total price)
}
// Now you can set your values..
If you run the above with the additions needed to work out the final total and to put the values onto your page you might have something that works.. or at least a good idea of a direction to go in to get there..
please look at the HTML for the table as I have amended it to make the javascript simpler to write.