undefined index from form post (untypical) - php

Here is the page with a table, which contains info rows that generates for every row from request.
<table class="table table-hover">
<thead>
<tr>
<th>ID</th>
<th>Account</th>
<th>Level</th>
<th class="text-center">Action</th>
</tr>
</thead>
<tbody>
<?php
$mysqli=connect();
$result=$mysqli->query("request is here");
while($rows=$result->fetch_assoc()) {
echo "<form action='script.php' method='post'>";
if ($rows['admin']>$_SESSION['admin']||$_SESSION['admin']<6||$rows['online']==1)
$levelinput='<td><input type="text" class="input-sm
form-control" value="'.$rows['admin'].'" disabled></td>
<td class="text-center"><input type="submit" class="btn btn-
simple btn-sm" value="Change" disabled></td>';
else $levelinput='<td><input type="number" class="input-sm
form-control" name="level" value="'.$rows['admin'].'"></td> ----- LEVEL IS HERE
<td class="text-center"><input type="submit" class="btn btn-round
btn-simple btn-sm btn-warning" value="Change"></td>';
if ($rows['ucp_online']>time()-60*5) $name=$rows['login']." <span
class='label label-success'>ONLINE</span>";
else $name=$rows['login'];
echo '
<tr>
<td>'.$rows['id'].'</td>
<td><a href="/admin/user.php?id='.$rows['id'].'" class="btn
btn-simple btn-sm">'.$name.'</a></td>
'.$levelinput.'
</tr>
<input type="hidden" name="admin" value="'.$rows['id'].'">
</form>';
}
$mysqli->close();
?>
</tbody>
</table>
Form moves us to script.php, which takes only $_POST['admin'] index and don't takes $_POST['level'] index. And of course, at the page level value displays correctly.

Related

PHP/MYSQL: Saving array data in table rows to mysql database using checkbox

I have an order table in my project. I am pulling the products from the database to this table using foreach. I show the quantity and unit price calculation information on the table instantly with jquery. I want to save the data in this table from the row selected with the checkbox to the database. I tried a few things with foreach but it looped so it created a new record for each row in the database. I want to print arrays with implode using commas between them. For example, the data in the rows entered in the quantity field in the table should be entered in the quantity field in the database as 1,2,3,4. If I have to explain briefly, I want to make an insert in one go.
Table:
Table & Form Code:
<form action="" method="POST">
<table class="table table-sm mb-3 text-center align-middle">
<thead>
<tr>
<th>Choose</th>
<th>Product Name</th>
<th width="137px">Quantity</th>
<th>Unit Price</th>
<th>Total Price</th>
</tr>
</thead>
<tbody>
<?php foreach($ProductTbl as $product){ ?>
<tr>
<th scope="row">
<div class="form-check form-check-success">
<input class="form-check-input" name="check[]" type="checkbox" value="
<?= $product-> productid">
</div>
</th>
<td>
<?= $product->productname ?>
</td>
<td>
<input class=" w-25 text-center quantity" type="text" name="quantity[]" value="0">
</td>
<td>
<input class="w-25 text-center unitprice" type="text" name="unitprice[]" value="
<?= $product->unitprice ?>" disabled="disabled"> €
</td>
<td>
<input class="w-25 text-center totalprice" type="text" name="totalprice[]" value="" disabled="disabled"> €
</td>
</tr>
<?php } ?>
</tbody>
</table>
<div class="text-center">
<button class="btn btn-success col-md-2" type="submit" name="add">Offer Preview</button>
<button class="btn btn-warning col-md-1" type="reset">Reset</button>
<a href="#">
<button class="btn btn-danger col-md-1" type="button">Cancel</button>
</a>
</div>
</form>
//The code structure I tried
if(isset($_POST['add'])){
$check = $_POST['check'];
$quantity = implode(',', $_POST['quantity']);
$totalprice = implode(',', $_POST['totalprice']);
if (!empty($check)) {
foreach ($check as $chk => $value){
// I printed it for testing.
echo $value.' - product id in the checked checkbox<br>';
print_r($quantity);
print_r($totalprice);
}
}
}
How does this code work the way I want?

Conditional Form Fields in Dynamic Data Table

I have table with a repeating recordset area. I am using Data Tables for the table design. For each record, there is a form with a drop-down menu. Based on the drop down menu choice, I need it to possibly display a text area to add more info. I have this working for the first record but, it doesn't show for the remaining records. Any advice would be helpful. The text area should only show if thy choose Not Met or Partially Met from the drop down menu. When they hit the save button, it will get inserted into the database.
Here is my code:
<table id="example" class="table table-responsive table-striped table-bordered" width="100%" border="0" cellspacing="0">
<thead>
<tr>
<th align="left" valign="top">Select a Status</th>
<th align="left" valign="top">Clause #</th>
<th align="left" valign="top">Clause</th>
</tr>
</thead><tbody>
<?php
$wa_startindex = 0;
while(!$pod_certify->atEnd()) {
$wa_startindex = $pod_certify->Index;
?>
<tr>
<td width="15%" valign="top"><form name="form2" method="post"><?php if ($pod_certify->getColumnVal("thegeps_clauseID") != $pod_certify->getColumnVal("thegeps_clauseID2")) {?><input type="hidden" name="geps_clauseID" id="geps_clauseID" value="<?php echo($pod_certify->getColumnVal("thegeps_clauseID")); ?>" required><input type="hidden" name="userID" id="userID" value="<?php echo($logged_user->getColumnVal("userID")); ?>" required><input type="hidden" name="dataID" id="dataID" value="<?php echo $_GET['dataID']; ?>" required>
<select name="status2" id="status2" class="form-control" required>
<option></option>
<option value="Met">Met</option>
<option value="Partially Met">Partially Met</option>
<option value="Not Met">Not Met</option>
</select>
<script>
$('#status2').on('change',function(){
if( $(this).val()=='Partially Met'){
$("#otherType4").show()
}
else{
$("#otherType4").hide()
}
});
</script>
<script>
$('#status2').on('change',function(){
if( $(this).val()=='Not Met'){
$("#otherType5").show()
}
else{
$("#otherType5").hide()
}
});
</script> <div style="display:none;" id="otherType5"><br><label><strong>Please Explain:</strong></label><br><textarea name="explanation" id="explanation" class="form-control"></textarea></div><div style="display:none;" id="otherType4"><br><label><strong>Please Explain:</strong></label><br><textarea name="explanation" id="explanation" class="form-control"></textarea></div><button type="submit" name="submit" id="submit" class="btn btn-warning btn-sm mb-2">Save</button><?php }?><?php if ($pod_certify->getColumnVal("thegeps_clauseID") == $pod_certify->getColumnVal("thegeps_clauseID2")) {?><input id="certifyID" name="certifyID" type="hidden" value="<?php echo($pod_certify->getColumnVal("certifyID")); ?>"><button type="submit" name="submit2" id="submit2" class="btn btn-danger btn-sm mb-2">Delete <?php echo($pod_certify->getColumnVal("status")); ?> Certification</button><?php }?></form></td>
<td valign="top"><?php echo($pod_certify->getColumnVal("article_number")); ?></td>
<td valign="top"><?php echo($pod_certify->getColumnVal("geps_clause")); ?></td>
</tr>
<?php
$pod_certify->moveNext();
}
$pod_certify->moveFirst(); //return RS to first record
unset($wa_startindex);
unset($wa_repeatcount);
?></tbody>
</table>

accept and decline button in one form

i've been stuck here i hours and now i don't know what to do. i have 2 buttons. Accept and Decline. i want to change a foreign key value whenever i clicked those.
this is in my blade
<table class="col-5 table table-bordered ml-3 mr-3">
<tbody>
<thead class="thead-dark">
<tr>
<th colspan="2">RENT REQUEST CARD</th>
</tr>
</thead>
<tr>
<td><p class="text-muted"><small>Borrower's Name:</small></p></td>
<td><h6>{{ $borrower_request->borrowers_name }}</h6></td>
</tr>
<tr>
<td><p class="text-muted"><small>Email:</small></p></td>
<td><h6>{{ $borrower_request->email }}</h6></td>
</tr>
<tr>
<td><p class="text-muted"><small>Contact number:</small></p></td>
<td><h6>{{ $borrower_request->contact_number }}</h6></td>
</tr>
<tr>
<td><p class="text-muted"><small>Date of Return:</small></p></td>
<td><h6>{{ $borrower_request->return_date }}</h6></td>
</tr>
<tr>
<td><p class="text-muted"><small>Request Status:</small></p></td>
<td>
<h5 class="text-danger"><em>{{ $borrower_request->requestStatus->request_status }}</em></h5>
</td>
</tr>
<tr>
<td colspan="2" class="text-right">
<button type="submit" class="btn btn-primary">Accept</button>
<button type="submit" class="btn btn-danger">Decline</buttonz>
</td>
</tr>
</tbody>
</table>
i want to change the value of the Request Status. should i put the form on the request status only or in the whole table?
and then here's in mu controller
public function getRequestsId(Request $request, $id)
{
$borrowersrequest = BorrowerRequest::find($id);
$requeststatus = RequestStatus::all();
return view('/borrowsmanager');
}
public function changeStatus(Request $request, $id)
{
$borrowersrequest = BorrowerRequest::find($id);
$borrowersrequest->request_status_id = 2;
$borrowersrequest->save();
return redirect('/borrowsmanager');
}
and then i am thinking how can i make the accept buttons throw value="2" and decline button value="3" if they are on the same form? should split then in 2 different function or maybe in 1? :(
Change your buttons to input
<input type="submit" name="accept" class="btn btn-primary" value="Accept" />
<input type="submit" name="decline" class="btn btn-danger" value="Decline" />
Next in $request check for existence of accept or decline property:
// Something like
$borrowersrequest->request_status_id = isset($request->accept) ? 2 : 3;
$borrowersrequest->save();
If you submit form via ajax - input type="submit" will not be passed to server. You'll have to change your js-script (if any) and pass some flag according to pressed button.

Why I only get the value of the last row in my table when I click the 'AddToCart' in other row

I have this code in php
<div style="width:75%;height: 600px;float: left;overflow-x: auto;">
<form method="POST" action="addtocart.php">
<table class="table" style="overflow-x: auto; width: 900px;">
<tr>
<th class="danger" id="th">Items</th>
<th class="danger" id="th">Price(PhP)</th>
<th class="danger" id="th">Quantity</th>
<th class="danger" id="th">Action</th>
</tr>
<?php
$item=mysqli_query($conn,"select * from store");
while($pqrow=mysqli_fetch_array($item)){
$iid=$pqrow['id_item'];
?>
<tr>
<td class="warning" id="td">
<?php echo $pqrow['item']; ?></td>
<td class="warning" id="td"><input type="hidden" name="price" value="<?php echo $pqrow['price']; ?>"><?php echo $pqrow['price']; ?></td>
<td class="warning" id="td"><input type="number" style="width: 70px;" name="qty" value="1"></td>
<td class="warning" id="td"><input type="hidden" name="item" value="<?php echo $iid; ?>"><input class="btn btn-success btn-md" type="submit" name="addtocart" value="AddToCart<?php echo $iid; ?>"></td>
</tr><?php } ?>
</table></form>
</div>
When I click AddToCart in first row, the value of the last row has been fetch. I want to get the id and the input quantity when I click the AddToCart in 1st row.
As you have only one form tag and multiple inputs with same name on this form, value of next input overwrites previous one. As a result you always have value of the last row.
You can fix it for example with creating a separate form for every row, e.g:
<div style="width:75%;height: 600px;float: left;overflow-x: auto;">
<table class="table" style="overflow-x: auto; width: 900px;">
<tr>
<th class="danger" id="th">Items</th>
<th class="danger" id="th">Price(PhP)</th>
<th class="danger" id="th">Quantity</th>
<th class="danger" id="th">Action</th>
</tr>
<?php
$item = mysqli_query($conn,"select * from store");
while ($pqrow=mysqli_fetch_array($item)) {
$iid=$pqrow['id_item'];?>
<tr>
<td class="warning" id="td">
<?php echo $pqrow['item']; ?></td>
<td class="warning" id="td"><?php echo $pqrow['price']; ?></td>
<td class="warning" id="td"><input type="number" style="width: 70px;" name="qty" value="1"></td>
<td class="warning" id="td">
<!-- Separate form for each record -->
<form method="POST" action="addtocart.php">
<input type="hidden" name="price" value="<?php echo $pqrow['price']; ?>">
<input type="hidden" name="item" value="<?php echo $iid; ?>">
<input class="btn btn-success btn-md" type="submit" name="addtocart" value="AddToCart<?php echo $iid; ?>">
</form>
</td>
</tr>
<?php } ?>
</table>
</div>
In this case you should track with javascript the value of quantity field. Of course, you can move <input type="number"> in a form too, but this will break your markup.
Also, if you're familiar with javascript, you can add some client preprocessing (getting values from inputs of a certain row) and use ajax-request to send these values to server.

how to add multiple arrays in form in mysql database php?

I have a form that is fetching database entries of a product. I have added multiple rows of the products in a form, and I need help in database insertion.
I want data to be added in a single row and I have multiple arrays.
<form id='students' method='post' name='students' action='add-
<div class="table-responsive">
<table class="table">
<tr>
<th><input class='check_all' type='checkbox' onclick="select_all()"/></th>
<th>S. No</th>
<th>BarCode</th>
<th>HSN</th>
<th>Product Name</th>
<th>MRP</th>
<th>CGST</th>
<th>SGST</th>
</tr>
<tr>
<td><input type='checkbox' class='case'/></td>
<td><span name='snum[]' id='snum'>1.</span></td>
<td><input class="form-control" type='text' id='countryname_1' name='countryname[]'/></td>
<td><input class="form-control" type='text' id='country_no_1' name='country_no[]'/></td>
<td><input class="form-control" type='text' id='phone_code_1' name='phone_code[]'/></td>
<td><input class="form-control" type='text' id='country_code_1' name='country_code[]'/> </td>
<td><input class="form-control" type='text' id='cgst_1' name='cgst1[]'/> </td>
<td><input class="form-control" type='text' id='sgst_1' name='sgst1[]'/> </td>
</tr>
</table>
<input type="submit" value="submit">
<button type="button" class='btn btn-danger delete'>- Delete</button>
<button type="button" class='btn btn-success addmore'>+ Add More</button>
</div>
</form>
If I understand your question correctly, you are trying to store arrays in database. It is not possible per se, but you can use the serialize() function and unserialize() when you fetch data.
PHP manual

Categories