changed dom when added input field form php - php

code for order.html.php:
<body>
<p> Place order</p>
<table>
<thead>
<tr>
<th>Item No.</th>
<th>Name</th>
<!--<th>Mrp</th>-->
<th>Price</th>
<th>Quantity</th>
</tr>
</thead>
<form id="form1" action="" method="post" >
<tbody>
<?php $id = 0 ;foreach ($dataProduct as $productData): ?>
<tr>
<td><?php echo ++$id ; ?></td>
<td><?php echo $productData['productName']; ?></td>
<td><?php echo $productData['productPrice'];?></td>
<td><input type="number" name="<?php echo $productData['productId']; ?>"/></td>
</tr>
<?php endforeach; ?>
<div><input type="hidden" name="add" value="placeorder"/>
<input type="submit" value="Place Order"/>
<div>
</tbody>
</form>
</table>
</body>
and dom presented in developer tool is:
<body>
<p> Place order</p>
<div><input type="hidden" name="add" value="placeorder">
<input type="submit" value="Place Order">
<div>
</div></div>
<table>
<thead>
<tr>
<th>Item No.</th>
<th>Name</th>
<!--<th>Mrp</th>-->
<th>Price</th>
<th>Quantity</th>
</tr>
</thead>
<form id="form1" action="" method="post" ></form>
<tbody>
<tr>
<td>1</td>
<td>hp keyboard old</td>
<td>400</td>
<td><input type="number" name="1"></td>
</tr>
<tr>
<td>2</td>
<td>lenovo keyboard old</td>
<td>450</td>
<td><input type="number" name="2"></td>
</tr>
<tr>
<td>3</td>
<td>kaspersky antivirus</td>
<td>430</td>
<td><input type="number" name="9"></td>
</tr>
</tbody>
</table>
</body>
see the position of submit and in order.html.php and the resulted dom.
because of this dom structure order.html.php is not able to submit value that resides in input filed.
here is the post with related issue but not exactly what is represent here,
Retrieve data from HTML table to PHP

Try proper html structure :
<body>
<p> Place order</p>
<form id="form1" action="" method="post" >
<table>
<thead>
<tr>
<th>Item No.</th>
<th>Name</th>
<!--<th>Mrp</th>-->
<th>Price</th>
<th>Quantity</th>
</tr>
</thead>
<tbody>
<?php $id = 0 ;foreach ($dataProduct as $productData): ?>
<tr>
<td><?php echo ++$id ; ?></td>
<td><?php echo $productData['productName']; ?></td>
<!--<td><?php echo $productData['mrp']; ?></td>-->
<td><?php echo $productData['productPrice'];?></td>
<td><input type="number" name="<?php echo $productData['productId']; ?>"/></td>
</tr>
<?php endforeach; ?>
<tr>
<td colspan="4">
<input type="hidden" name="add" value="placeorder"/>
<input type="submit" value="Place Order"/>
</td></tr>
</tbody>
</table>
</form>
</body>

First off, just wrap the table with the <form> tag.
Second, it's not really good to use productId as your name="$productId", instead, use something like name="products[$productId]" so that after form submission, they will be grouped on the same variable as an array.
Sample Code: Sample Demo
$dataProduct = array(
array('productId' => 1, 'productName' =>'hp keyboard old', 'productPrice' => 400),
array('productId' => 2, 'productName' =>'lenovo keyboard old', 'productPrice' => 430),
array('productId' => 9, 'productName' => 'kaspersky antivirus', 'productPrice' => 430),
);
if(isset($_POST['submit'])) {
$values = $_POST['products']; // get all the grouped values
$total = array();
foreach($values as $productId => $quantity) { // loop values
foreach($dataProduct as $productData) {
if($productData['productId'] == $productId) {
// simple multiplication, quantitiy times price for each item
$total[$productId] = $productData['productPrice'] * $quantity;
}
}
}
echo '<pre>';
print_r($total);
echo '</pre>';
}
?>
<form method="POST">
<table cellpadding="10">
<thead>
<tr>
<th>Item No.</th>
<th>Name</th>
<!--<th>Mrp</th>-->
<th>Price</th>
<th>Quantity</th>
</tr>
</thead>
<tbody>
<?php $id = 0 ; foreach($dataProduct as $productData): ?>
<tr>
<td><?php echo ++$id ; ?></td>
<td><?php echo $productData['productName']; ?></td>
<td><?php echo $productData['productPrice'];?></td>
<td><input type="number" name="products[<?php echo $productData['productId']; ?>]"/></td>
</tr>
<?php endforeach; ?>
<tr>
<td><input type="submit" name="submit" /></td>
</tr>
</tbody>
</table>
</form>
So for example, in the form, I inputted in all of textboxes a quantity of 2. print_r() should yield like this:
Array
(
[1] => 800
[2] => 860
[9] => 860
)
Indices as the keys, the values are the totals for each quantity times price

Related

PHP: Show <td> elements if certain criteria is true

Basically I need to have the columns with my delete and edit buttons visible only if the admin logs in, I know how to determine if an admin is logged in. Is there a way to echo the tags that need to be shown? Thanks in advance!
<table>
<tr>
<th>ID</th>
<th>Full Name</th>
<th>Email Address</th>
<th>Address</th>
<th>City</th>
<th>State</th>
<th>Zip Code</th>
<th>Status</th>
<th>Edit?</th>
<th>Delete?</th>
</tr>
<?php foreach ($users as $user) : ?>
<tr>
<td><?php echo $user->getUser_id();?></td>
<td><?php echo $user->getFirstName().' '.$user->getLastName();?></td>
<td><?php echo $user->getEmailAddress(); ?></td>
<td><?php echo $user->getAddress();?></td>
<td><?php echo $user->getCity(); ?></td>
<td><?php echo $user->getState(); ?></td>
<td><?php echo $user->getZipCode(); ?></td>
<td><?php echo $user->getActiveDescription();?></td>
these two <td> should only be shown if x is true:
<td>
<form action ="" method="post">
<input type="hidden" name="controllerRequest"
value="show_user_edit">
<input type="hidden" name="user_id"
value="<?php echo $user->getUser_id(); ?>"/>
<input type="submit" value="Edit"></form>
</td>
<td>
<form action="" method="post">
<input type="hidden" name="controllerRequest"
value="delete_user">
<input type="hidden" name="user_id"
value="<?php echo $user->getUser_id(); ?>"/>
<input type="submit" value="Delete" id="red">
</form>
</td>"
;
}
}
?>
</tr>
<?php endforeach; ?>
</table>
You need to use <?php if(x): ?> and <?php endif ?>see: https://www.php.net/manual/en/control-structures.if.php#112231

Both buttons decrementing same value

I am buildingd a library management system in PHP and HTMl.
I have some problem with the borrowing system where user can borrow books.
Here is my emitere-carti.php:
<form action="imprumutare.php" method="POST">
<table class="table table-dark">
<thead>
<tr>
<th>Imprumutare</th>
<th scope="col">Titlu </th>
<th scope="col">Autor</th>
<th scope="col">Categorie</th>
<th scope="col">Stoc</th>
</tr>
</thead>
<tbody>
<?php
$selectare="SELECT * FROM carti";
$rezultat=mysqli_query($conn,$selectare);
if(mysqli_num_rows($rezultat)>0){
while($row=mysqli_fetch_assoc($rezultat)){
$carte_nume=$row['titlu'];
$disabled=$row['stoc']>0 ? "" :"disabled";
?>
<tr>
<td><input type="submit" name="test" class="btn btn-secondary"
value="Imprumutare" <?php echo $disabled;?>></input>
<td><input type="text" name="nume" value="<?php echo $row['titlu'];?
>"></input></td>
<td><?php echo $row['autor'];?></td>
<td><?php echo $row['categorie'];?></td>
<td><?php echo $row['stoc'];?></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
</form>
Here is my imprumutare.php(that means borrowing):
include('conexiune.php');
//sfarsit if
//Imprumutare
if(isset($_POST['test'])){
$id=$_POST['identificator'];
$nume_carte=$_POST['nume'];
$sql_rezervare="UPDATE carti SET stoc=stoc-1 WHERE
titlu='$nume_carte' ";
if( mysqli_query($conn,$sql_rezervare)){
header('location:emitere_carti.php');
}
else{
die(mysqli_error($conn));
}
}
Here is a screenshot about what I am talking:
So my problem is both buttons are decrementeing the same value(the value of stoc).
Can someone help me?

Call all posts from Wordpress database with category names

Our Wordpress site are infected and we are deleted all files. we have now only database and want to extract all information from db and afther this copy on other site.
so I am using this php class to connect db and call some information. I am calling now posts with data, but need categories also.
For example:
Post Title, Post Content, Post Category
here is my working code without category names
$posts = $db->get('c3624322676f_wp_posts');
but I have no idea how to call categories names here.
This is my html + foreach
<table class="table table-hover">
<thead class="thead-inverse">
<tr>
<th>#</th>
<th>Title</th>
<th>Type</th>
<th>Content</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php foreach ($posts as $post) : ?>
<tr>
<th scope="row"><?php echo $count++; ?><a href=""></th>
<td><?php echo $post['post_title']; ?></td>
<td><?php echo $post['post_type']; ?></td>
<td><?php echo strip_tags ($post['post_content']); ?></td>
<td>
<form action="" method="post">
<input type="hidden" name="id" value="<?php echo $post['ID']; ?>" />
<input style="cursor:pointer;" type="submit" class="btn btn-primary" value="Delete" />
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
This should work
<table class="table table-hover">
<thead class="thead-inverse">
<tr>
<th>#</th>
<th>Title</th>
<th>Type</th>
<th>Content</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php foreach ($posts as $post) : ?>
<tr>
<th scope="row"><?php echo $count++; ?><a href=""></th>
<td><?php echo $post['post_title']; ?></td>
<td><?php echo $post['post_type']; ?></td>
<td><?php echo strip_tags ($post['post_content']); ?></td>
<?php
$separator = ',';
$output = '';
$categories = get_the_category($post['post_title']);
if ($categories){
foreach($categories as $category) {
$output .= ''.$category->cat_name.''.$separator;
}
echo trim($output, $separator);
}
?>
<td>
<form action="" method="post">
<input type="hidden" name="id" value="<?php echo $post['ID']; ?>" />
<input style="cursor:pointer;" type="submit" class="btn btn-primary" value="Delete" />
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>

particular Data not getting inserted in a column

I am facing a unknown problem while I go insert data.Some data getting inserted in same table n same column but except one.
The following is my code.
<html>
<body>
<?php
include("index.php");
?>
<form action="addcenter.php" method="post">
<table align="center" border="9" width="600">
<tr>
<td align="center" colspan="5" bgcolor="yellow">
<h1>Add Your Center Details</h1></td>
</tr>
<tr>
<th>District Name</th>
<td><input type="text" name="district"></td>
</tr>
<tr>
<th>Phone No.</th>
<td><input type="text" name="phone"></td>
</tr>
<tr>
<th>Person's Name</th>
<td><input type="text" name="person"></td>
</tr>
<tr>
<th>Designation</th>
<td><input type="text" name="designation"></td>
</tr>
<tr>
<td align="center" colspan="5"><input type="submit" name="submit"
value="submit"></td>
</tr>
</table>
</form>
<?php
include("includes/connect.php");
if(isset($_POST['submit']))
{
$district=$_POST['district'];
$phone=$_POST['phone'];
$person=$_POST['person'];
$designation=$_POST['designation'];
if($district=='' or $phone=='' or $person=='' or $designation='')
{
echo"<script> alert('Please fill the fiels')</script> ";
exit();
}
$query="insert into centers (District,Phone,ContactPerson,Designation)
values('$district','$phone','$person','$designation') ";
if(mysql_query($query))
{
echo"<center><h1>Details Successfully added to your database</h1>
</center> ";
}
}
?>
<table width=" 900" align="center" border="3">
<tr>
<td align="center" colspan="9" bgcolor="orange"><h1>View all
Centers</h1></td>
</tr>
<tr align="center">
<th>SL No.</th>
<th>District</th>
<th>Phone No</th>
<th>Contact Person</th>
<th>Designation</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<?php
$query="select * from centers";
$run=mysql_query($query);
$i=1;
while($row=(mysql_fetch_array($run)))
{
$center_id=$row['center_id'];
$district=$row['District'];
$phone=$row['Phone'];
$contact_person=$row['ContactPerson'];
$designation=$row['Designation'];
?>
<tr align="center">
<td><?php echo $i++ ;?></td>
<td><?php echo $district;?></td>
<td><?php echo $phone ;?></td>
<td><?php echo $contact_person ;?></td>
<td><?php echo $designation ;?></td>
<td>Edit
</td>
<td><input type="button" onclick="deleteme1(<?php echo $center_id ?>)"
name="delete" value="Delete"></td>
</tr>
<script language="javascript">
function deleteme1(delid1)
{
if(confirm("are u sure"))
{
window.location.href='deletecenter.php?del_id1='+delid1+'';
return true;
}
}
</script>
<?php } ?>
</table>
</body>
</html>
The problem is that when I going to insert the Contactperson,district,designation and phone no.Then only the data of Contactperson,district and phone no get inserted but not the designation..I dont know why this is happening even the coding is also right..Please help me. Thankyou
Your "if" statement is assigning '' to $designation. Use '==' for comparison.
if($district=='' or $phone=='' or $person=='' or $designation**=**'')
The if statement says, "if $district is '' or $phone is '' or $person is '' or 'I can assign nothing to $designation (which always succeeds)' - at which point you have successfully assigned '' to $designation. The if statement finishes and you insert '' into designation.

sort data by field heading php mysql

I have a database
that have two tables.
The sql query fetching data correctly.
I want to sort fields data by typing table heading in text box and click on go that in table footer.
Many Thanks
<table border="0" cellpadding="0" cellspacing="0" width="50%">
<thead>
<tr>
<th class="capt" colspan="6">Available Projects</th>
</tr>
<tr>
<th>Select</th>
<th>Project</th>
<th>Crawler</th>
<th>Description</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
include(dirname(__file__)."/includes/dbConn.php");
$result = mysql_query("SELECT *, ( SELECT name FROM projects WHERE projects.id = crawlers.pid ) AS pname FROM `crawlers`", $appConn);
while($row = mysql_fetch_array($result))
{
?>
<tr id="crawler_<?php echo $row['id']; ?>">
<td>
<input value="" id="check" type="checkbox">
<input id="crawler_id_<?php echo $row['id']; ?>" type="hidden" value="<?php $row['id']; ?>" /> <!-- crawler id -->
</td>
<td><?php echo $row['pname']; ?></td>
<td><?php echo $row['name']; ?></td>
<td>username#domain.com</td>
<td>Enabled</td>
<td><a class="edit" href="#">edit</a><a class="add" href="#">run</a><a class="delete" href="#">delete</a></td>
</tr>
<?php } ?>
</tbody>
<tfoot>
<tr>
<th colspan="6"> Sort Fields by <input value="type here" id="field" type="text"> Go </th>
</tr>
</tfoot>
</table>
I think, you are looking for ORDER BY
Have a look at the MySQL Documentation about the SELECT syntax: http://dev.mysql.com/doc/refman/5.0/en/select.html

Categories