I have a table something like this:
Product SellingPrice Cost Profit
Product 1 49 45 4
Product 2 54 50 4
When i put value in the profit column the sellingprice will calculate. I fetched the cost value from the database. I'm done working with the calculation using jQuery but I'm stuck on how to update it in the database. Could someone guide me about the server side implementation?
Here is my code:
<html>
<head>
<title>Content Management System</title>
<script>
//calculate the selling price
$(document).ready(function(){
$('tr').each(function(){
var result = 0;
$(this).find("input[name=cost],input[name=profit]").each(function(){
result += (+$(this).val());
});
$(this).find("input[name=sellingprice]").val(result).css("background-color", "green");
});
});
</script>
</head>
<body>
<table>
<tr>
<td><center>ID</center></td>
<td><center>Product</center></td>
<td><center>Selling Price</center></td>
<td><center>Current Cost</center></td>
<td><center>Profit</center></td>
</tr>
<?php
$result = mysql_query("SELECT id, product, cost FROM inventory");
while ($myrow = mysql_fetch_row($result))
{
?>
<tr>
<td>
<?php echo $myrow[0]; ?>
</td>
<td>
<?php echo $myrow[1]; ?>
</td>
<td>
<?php echo "<input type='text' name='sellingprice' size='10' readonly='true'/>"; ?>
</td>
<td>
<?php echo "<input type='text' name='cost' size='10' value='$myrow[2]' readonly='true'/>"; ?>
</td>
<td>
<?php echo "<input type='text' name='profit' size='10' />"; ?>
</td>
</tr>
<?php
}
?>
</table>
</center>
</body>
</html>
change your html and create a new php file to handle your form submission.
in php get the values through $_GET[""] and insert them into table.
<body>
<form name="some_name" id="form1" action="somephpfilename.php">
<table>
<tr>
<td><center>ID</center></td>
<td><center>Product</center></td>
<td><center>Selling Price</center></td>
<td><center>Current Cost</center></td>
<td><center>Profit</center></td>
</tr>
<?php
$result = mysql_query("SELECT id, product, cost FROM inventory");
while ($myrow = mysql_fetch_row($result))
{
?>
<tr>
<td>
<?php echo $myrow[0]; ?>
</td>
<td>
<?php echo $myrow[1]; ?>
</td>
<td>
<?php echo "<input type='text' name='sellingprice' size='10' readonly='true'/>"; ?>
</td>
<td>
<?php echo "<input type='text' name='cost' size='10' value='$myrow[2]' readonly='true'/>"; ?>
</td>
<td>
<?php echo "<input type='text' name='profit' size='10' />"; ?>
</td>
</tr>
<?php
}
?>
</table>
</center>
<input type="submit" name="update" value="update to db">
<form>
</body>
You add form tag to your table rows and when form is submitted, you grab / validate incoming data and run UPDATE query on your table by ids. Also, you need to change your input elements to use Arrays:
echo "<input type='text' name='sellingprice[".$myrow[0]."]' size='10' readonly='true'/>";
Something like that...
Related
I am trying to print out data which is stored in my database and then delete an aspect of it. See below example:
Database includes: id, room, time, date
my code is is as follows:
<?php
$sql = "SELECT id, room, timers, dates, remove FROM groom WHERE person = $a";
$result = $conn -> query ($sql);
if($result -> num_rows >0){
?>
<?php
while($row = $result -> fetch_assoc()){
?>
<tr>
<td> <?php echo $row["room"] ?> </td>
<td> <?php echo $row["timers"] ?> </td>
<td> <?php echo $row["dates"] ?> </td>
<td> <?php echo "Glassrooms" ?> </td>
<td>
<?php
echo
"<form action='' method='post'>
<input type='submit' name='1' value='Delete' />
</form>";
if(isset($_POST['1']))
{
$r = $row["id"];
$sqli ="UPDATE groom SET remove = '$a' WHERE id = $r";
$resultt = $conn -> query ($sqli);
echo
"<form action='removalgr.php' method='post'>
<input type='submit' name='usen' value='Confirm Deletion' />
</form>";
}
?> </td> </tr>
What i am doing is selecting all the data from a database groom where it relates to the person logged in. It prints it out fine in the following format which is perfect:
Room Time Date Area DeleteBooking
1 4 2/3/17 BB Delete
And the where it says delete is a button which allows for the booking to be deleted. However my problem is this:
when there is more than one booking, they all delete because below they are being called the same thing. see here:
<input type='submit' name='useb' value='Delete' />
Is it possible to call the above name something different everytime, or alternatively call it the id of the booking?
Hopefully this makes sense and any help would be greatly appreciated!!
Thanks
Add a hidden input with the ID of the item to be deleted.
echo
"<form action='' method='post'>
<input type='hidden' name='id' value='{$row['id']}'>
<input type='submit' name='1' value='Delete' />
</form>";
I have some combobox with onchange event, and they're reset each other when selected the orther one of them, does any suggest how to retain the value on the page? this my script :
<form method="POST" name="form1" action="<?php $_SERVER['PHP_SELF'];?>">
<table border="0">
<tr>
<td colspan="6"></td>
</tr>
<tr>
<td>
<select name="select_petugas1" style="width:18px;" onchange="this.form.submit('select_petugas1');"> //first combobox
<option></option>
<?php include 'dbconn.php';
$sql_peg1="SELECT * FROM users"; $result_peg1=$conn->query($sql_peg1);
while( $row_peg1=$result_peg1->fetch_assoc() ){
echo "<option>".$row_peg1['nama']."</option>";
}
?>
</select>
</td>
<td>
<?php
if(isset($_POST['select_petugas1'])){
$select_petugas1=$_POST['select_petugas1'];
echo "<input type='text' name='select_petugas1' value='".$select_petugas1."'>"; // Throw 1st result into the text box
$sql_NIP1="SELECT NIP FROM users WHERE nama='$select_petugas1'";
$result_NIP1=$conn->query($sql_NIP1);
$row_NIP1=$result_NIP1->fetch_assoc();
$NIP1=$row_NIP1['NIP'];
?>
</td>
<td> NIP</td>
<td>:</td>
<td><input type="text" name='NIP1' value="<?php echo $NIP1; ?>"></td>
</tr> <!-- child of first result -->
<tr>
<td colspan="5" bgcolor="blue"></td>
</tr>
<tr>
<td>
<select name="peg_2" style="width:18px;" onchange="submit(this)"><!--2nd combobox-->
<option></option>
<?php
$sql_peg2="SELECT nama FROM users";
$result_peg2=$conn->query( $sql_peg2 );
while ($row_peg2=$result_peg2->fetch_assoc()){
echo "<option value='".$row_peg2['nama']."'>".$row_peg2['nama']."</option>";
}
?>
</select>
</td>
<td>
<?php
if( isset($_POST['peg_2']) ){
$peg_2=$_POST['peg_2'];
echo "<input type='text' name='peg2' value='".$peg_2."'>"; // 2nd result throw into 2nd texbox
$sql_NIP2="SELECT NIP FROM users WHERE nama='$peg_2'";
$result_NIP2=$conn->query($sql_NIP2);
$row_NIP2=$result_NIP2->fetch_assoc();
?>
</td>
<td> NIP</td>
<td>:</td>
<td><input type='text' name='NIP2' value="<?php echo $row_NIP2['NIP'];?>"> <!--2nd child of result-->
<?php
}
}
if(isset($_POST['NIP2'])){
$NIP2=$_POST['NIP2'];
echo "<br /> NIP2 :".$NIP2."<br />";
}
mysqli_close($conn);
?>
</td>
</tr>
</table>
</form>
<form method="POST" name="wilayah" id="wilayah" action="<?php $_SERVER['PHP_SELF'];?>">
<table border="1">
<tr>
<td>
<select name="select_provinsi" onchange="submit(this)" style="width:18;">
<option selected>PROVINSI</option>
<?php
include 'dbconn.php';
$sql_prov="SELECT * FROM wilayah GROUP BY provinsi";
$result_prov=$conn->query($sql_prov);
echo "";
while($row_prov=$result_prov->fetch_assoc()){
$provinsi=$row_prov['provinsi'];
echo "<option value='".$provinsi."'>".$provinsi."</option>";
}
?>
</select>
<?php
if(isset($_POST['select_provinsi'])){
$select_provinsi=$_POST['select_provinsi'];
echo "
<input type='text' name='select_provinsi' value='".$select_provinsi."' placeholder='PROVINSI'>
</td>
</tr>";
$sql_kabkota="SELECT * FROM wilayah WHERE provinsi='$select_provinsi' GROUP BY kab_kota";
$result_kabkota=$conn->query($sql_kabkota);
?>
<tr>
<td>
<select name="select_kabkota" style="width:18px;" onchange="submit(this)"><option>KAB/KOTA</option>
<?php
while($row_kabkota=$result_kabkota->fetch_assoc()){
echo "<option>".$row_kabkota['kab_kota']."</option>";
}
?>
</select>
<?php
}
if(isset($_POST['select_kabkota'])){
$select_kabkota=$_POST['select_kabkota'];
?>
<input type="text" name="kab_kota" value="<?php echo $select_kabkota;?>">
<?php
}
mysqli_close($conn);
?>
</td>
</tr>
</table>
</form>
hope any suggestion for resolved of my problem with them,,
onchange="submit(this)" means that you want to submit the form when the value of the combobox changes. So, when the form is sent, the page reloads and you get the default value of your form.
To restore the chosen value, I would do something like :
<select name="select_kabkota" style="width:18px;" onchange="submit(this)">
<option>KAB/KOTA</option>
<?php
if(isset($_POST['select_kabkota']))
$select_kabkota=$_POST['select_kabkota'];
while($row_kabkota=$result_kabkota->fetch_assoc())
{
$selected = $select_kabkota == $row_kabkota['kab_kota'] ? 'selected="selected"' : '';
echo "<option ".$selected." >".$row_kabkota['kab_kota']."</option>";
}
?>
</select>
I have some dropdown menus. If I select one of them I want to change the other's content with something that is reliable with what i selected. How can I do that ? With html and php.
For example I have one table
Year with id_year and year
Stuff with id_stuf and stuff
If I select an year in the first dropdown menu , in the other drodown menu will show only the stuff from that year .
This is my content with the dropdown menus
<div class="view">
<form name="tabel" method="post" action="insertexamen.php">
<table>
<tr>
<td>Data</td>
<td><input type="date" name="data" value="data" required="required"/><br></td>
</tr>
<tr>
<td>An</td>
<td>
<?php
$sql_year="SELECT * FROM an";
$rez_year = mysqli_query($link,$sql_year);
echo "<select name=\"year\" >";
while($year=mysqli_fetch_array($rez_year))
{
echo "<option value=\"".$year['id_an']."\">".$year['grupa']."</option>";
}
echo "</select>";
?><br>
</td>
</tr>
<tr>
<td>Materie</td>
<td>
<?php
$sql_mat="SELECT * FROM materii";
$rez_mat = mysqli_query($link,$sql_mat);
echo "<select name=\"mat\" >";
while($mat=mysqli_fetch_array($rez_mat))
{
echo "<option value=\"".$mat['id_mat']."\">".$mat['numemat']."</option>";
}
echo "</select>";
?><br>
</td>
</tr>
<tr>
<td>Profesor</td>
<td>
<?php
$sql_proff="SELECT * FROM profesor";
$rez_proff = mysqli_query($link,$sql_proff);
echo "<select name=\"proff\" >";
while($proff=mysqli_fetch_array($rez_proff))
{
echo "<option value=\"".$proff['id_prof']."\">".$proff['numep']." ".$proff['prenumep']."</option>";
}
echo "</select>";
?><br>
</td>
</tr>
<tr>
<td>Asistent</td>
<td>
<?php
$sql_profff="SELECT * FROM profesor";
$rez_profff = mysqli_query($link,$sql_profff);
echo "<select name=\"profff\" >";
while($profff=mysqli_fetch_array($rez_profff))
{
echo "<option value=\"".$profff['id_prof']."\">".$profff['numep']." ".$profff['prenumep']."</option>";
}
echo "</select>";
?><br>
</td>
</tr>
<tr>
<td>Sala</td>
<td>
<?php
$sql_room="SELECT * FROM sala";
$rez_room= mysqli_query($link,$sql_room);
echo "<select name=\"room\" >";
while($room=mysqli_fetch_array($rez_room))
{
echo "<option value=\"".$room['id_s']."\">".$room['salaa']."</option>";
}
echo "</select>";
?><br>
</td>
</tr>
<tr>
<td>Tip</td>
<td>
<?php
$sql_type="SELECT * FROM examen";
$rez_type= mysqli_query($link,$sql_type);
echo "<select name=\"type\" >";
while($type=mysqli_fetch_array($rez_type))
{
echo "<option value=\"".$type['id_tip']."\">".$type['tip']."</option>";
}
echo "</select>";
?><br>
</td>
</tr>
<tr>
<td><input name="submit" type="submit" value="Trimite"/></td>
<td><input name="reset" type="reset" value="Reset"/></td>
</tr>
</table>
Are you looking for something like that:
<html>
<title>dropdownlist</title>
<head>
<script language="Javascript" type="text/javascript" >
function choix(formulaire)
{
var j;
var i = form1.boite1.selectedIndex;
if (i == 0)
for(j = 1; j <3; j++)
form1.boite2.options[j].text="";
else{
switch (i){
case 1 : var text = new Array( "London","Manchester","Birmingham");
break;
case 2 : var text = new Array("Paris","Marseille","Lyon");
break;
case 3 : var text = new Array("Berlin","Munich","Francfort");
break;
}
for(j = 0; j<3; j++)
form1.boite2.options[j+1].text=text[j];
}
form1.boite2.selectedIndex=0;
}
</script>
</head>
<body>
<form name="form1">
<select name="boite1" onChange="choix(this.form)">
<option selected>country</option>
<option>England</option>
<option>France</option>
<option>Germany</option>
</select>
<select name="boite2">
<option selected>cities</option>
<option></option>
<option></option>
<option></option>
</form>
</select>
</body>
</html>
if you want absolutely to do it using only html and php you will need to use some ajax because php is serverside and html is slient side. so for what you asy I would recommend the code above
I have a table called "project_name" in my database called "encrypt_decrypt". The table contains only 1 column called "name" which contains different values of name (ex : p1, p2, p3...). I have to retrieve this values(p1,p2,p3..) from my database and display it on a registration form with each value displaying one below the other having a checkbox with it so that user can select any of the name while registering! How do i do this in php ???
Thanks in advance!
<html>
<body>
<form name="reg" action="code_exec2.php" onsubmit="return validateForm()" method="post">
<table width="274" border="0" align="center" cellpadding="2" cellspacing="0">
<tr>
<td><div align="right" style="white-space:nowrap" >Please select the project:</td>
</tr>
<tr>
<td><div align="right"><input type="checkbox" name="project[]" ></div></td>
<td><?php
session_start();
include('connection2.php');
$row=mysql_query("select * from project_name");
$array= array();
$output = mysql_fetch_assoc($row);
while($output){
$array[] = $output;
}
print_r($array);
?></td>
</tr>
<tr>
<td><div align="right"><input type="checkbox" name="Select all"
value="select all" onclick="toggle(this)"></div></td>
<td>Select all</td>
</tr>
<tr>
<td><div align="right"></div></td>
<td><input name="submit" type="submit" value="Submit" /></td>
</tr>
</table>
</form>
</body>
Try this.
$result=mysql_query("select * from project_name");
$checkboxes=array();
while($r=mysql_fetch_assoc($result)){
$checkboxes[]='<input type="checkbox" name="names[]" value="'.$r['name'].'">'.$r['name'].'<br />';
}
Then echo below wherever you want the checkboxes to appear.
echo implode("\n",$checkboxes);
Assuming, that your connection to db is correct, you need to change the way you display results:
<?php
session_start();
include('connection2.php');
$row=mysql_query("select * from project_name");
$array= array();
while($output = mysql_fetch_assoc($row)){
//now you have row with name, and you need to display each name with new tr:
?>
<tr>
<td><div align="right"><input type="checkbox" name="project[]" value="<?php echo $output['name'] ?>"></div></td>
<td><?php echo $output['name'] ?></td>
</tr>
<?php } // closing while loop
?>
Note that I added value to checkbox - if you want to do something with checked project, you have to know which one it was.
And remember taht mysql_ functions are depreated! You should use PDO or mysqli_ instead
i tried this and i got: any ways thanks for all your help :)
<?php
include('connection.php');
$r=mysql_query("select distinct name from project_name");
$ProdArray=array();
while ($row = mysql_fetch_object($r)) {
array_push($ProdArray,$row->name);
}
foreach($ProdArray as $p) {
echo "<tr>";
echo "<td><div align='right'>";
echo "<input type='checkbox' name='project[]' value=" .$p. " />";
echo "</div></td>";
echo "<td>$p</td>";
echo "</tr>";
}
?>
I have a table where each row in the table has a button that allows you to delete that particular row from the database. Although somehow my form only submits the last row and no the one I selected. Please ignore the deprecated tags, well aware of the issues.
<?php
$result = mysql_query("SELECT * FROM booking");
while($row = mysql_fetch_array($result))
{
?>
<form class='table-form' id='form' method='post'>
<tr>
<input id="bookid" type="hidden" name="bookid" value="<?php echo ($row['booking_id']); ?>" />
<td>
<?php echo ($row['booking_id']);?>
</td>
<td>
<?php echo ($row['user_id']); ?>
</td>
<td>
<?php echo ($row['event_id']); ?>
</td>
<td>
<?php echo ($row['payment_type']); ?>
</td>
<td>
<?php echo ($row['booking_date']); ?>
</td>
<center><button type="submit" id="submit" name="submit">Cancel</button><center>
<td>
</td>
</tr>
<?php
}
?>
</form>
</table>
</div>
<?php
if (isset($_POST['bookid'])){
$id = ($_POST['bookid']);
$result = mysql_query("DELETE FROM booking
WHERE booking_id = '$id'");
}
?>
If you preffer programing this way, move </form> above
<?php
}
?>
(to cycle body).
Your code needs a lot of clean up, style cleaning and html validation. :-P