Update MySQL database using php form using text boxes - javascript

Hello I have an issue with updating MySQL using PHP form in Netbeans the issue that occurs is:
"Notice: Undefined variable: fetched_row in C:\Xampp\htdocs\Resume_DB\PHP\Edication_Edit.php on line 75"
In all 4 of my text boxes. The code I have is :
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("cs266db_db1");
if(isset($_GET['edit_id']))
{
$sql_query="SELECT * FROM education Where Edit_Education=".$_GET['edit_id'];
$result_set= mysql_query($sql_query);
$fetched_row= mysql_fetch_array($result_set);
}
if(isset($_POST['btn_update']))
{
//variables for input data
$Class = $_POST['Class'];
$Discipline = $_POST['Discipline'];
$Description = $_POST['Description'];
$Term = $_POST['Term'];
//sql query for update data
$sql_query = "UPDATE Education SET Class='$Class', Discipline='$Discipline', Description='$Description', Term='$Term' WHERE Education_Edit=".$_GET['edit_id'];
//sql query execution function
if(mysql_query($sql_query))
{
?>
<script type='text/javascript'>
alert('Data Has Been Updated');
window.location.href='Eduaction.php';
</script>
<?php
}
else
{
?>
<script type='text/javascript'>
alert('Error Occured while Updating Data');
</script>
<?php
}
//sql query execution function
}
if(isset($_POST['btn-cancel']))
{
header("Location: Education.php");
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Edit Education</title>
</head>
<body>
<center>
<div id="header">
<div id="content">
<label>Edit Education</label>
</div>
</div>
<div id="body">
<div id="content">
<form method="post">
<table align="center">
<tr>
**<td><input type="text" name="Class" placeholder="Class" value="<?php echo $fetched_row['Class']; ?>" required /></td>
</tr>
<tr>
<td><input type="text" name="Discipline" placeholder="Discipline" value="<?php echo $fetched_row['Discipline']; ?>" required /></td>
</tr>
<tr>
<td><input type="text" name="Description" placeholder="Description" value="<?php echo $fetched_row['Description']; ?>" required /></td>
</tr>
<tr>
<td><input type="Text" name="Term" placeholder="Term" value="<?php echo $fetched_row['Term']; ?>" required</td>**
</tr>
<tr>
<td>
<button type="submit" name="btn-update"><strong>UPDATE</strong></button>
<button type="submit" name="btn-cancel"><strong>Cancel</strong></button>
</td>
</tr>
</table>
</form>
</div>
<div id="footer">
<div id="content">
</div>
</div>
</center>
</body>
</html>
Any thoughts? Thanks for your help guys

The problem is that fetched_row is only assigned when $_GET['edit_id'] is set. Otherwise it does not exist, but you're still trying to use it, even if $_GET['edit_id'] is not set.
Change
<td><input type="Text" name="Term" placeholder="Term" value="<?php echo $fetched_row['Term']; ?>" required</td>**
To:
<td><input type="Text" name="Term" placeholder="Term" value="<?php echo (isset($fetched_row['Term']))? $fetched_row['Term'] : ''; ?>" required</td>**

It seems instead of "Edit_Education" you are using "Education_Edit" in update query or instead of "Education_Edit" you are using "Edit_Education" in select query.
$sql_query="SELECT * FROM education Where Edit_Education=".$_GET['edit_id'];
$sql_query = "UPDATE Education SET Class='$Class', Discipline='$Discipline', Description='$Description', Term='$Term' WHERE Education_Edit=".$_GET['edit_id'];

change if(isset($_POST['btn_update'])) to if(isset($_POST['btn-update'])) because in html button tag you named button as btn-update .
In update query change column name Education_Edit change to Edit_Education

Related

Changing background color onclick on a specific table row

I want to change the background color of a specific clicked table row. It also should change it back to the original color. Back and forth from red to green. And it should also check the checkbox. Green = checkbox checked/ Red = checkbox unchecked.
The rows look like this
<tr id="<?php echo $row['id'] ?>" value="<?php echo $row['id'] ?>">
and for JQUERY I have this:
$(document).ready(function() {
$('.table tr').click(function(event) {
var id = $(this).attr('id');
var bcolor = $('.task').css('background-color');
console.log(id);
console.log(bcolor);
if (event.target.type !== 'checkbox') {
$(':checkbox', this).trigger('click');
if (bcolor == 'rgb(205, 92, 92)') {
$('.task').css('background-color', 'green');
} else {
$('.task').css('background-color', 'indianred');
}
};
});
});
How can I use the var id = $(this).attr('id'); to change the background color only for the specific clicked row?
Right now it makes every row green by clicking on any of them.
Whole main.php:
Header
<?php include 'taskpdo.php';?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>To Do List</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script src="jquery-3.4.1.min.js"></script>
<script src="script.js"></script>
</head>
Body
<body>
<!-- CONFIRM MESSAGE -->
<?php if (isset($_SESSION['message'])): ?>
<div class="msg">
<?php
echo $_SESSION['message'];
unset($_SESSION['message']);
?>
</div>
<?php endif?>
<div class="spacer">
<!-- Heading -->
<div class="heading">
<h2>To Do List</h2>
</div>
<!-- Userinformation -->
<div class="logout">
<?php if (isset($_SESSION['username'])): ?>
<p>Welcome<strong><?php echo $_SESSION['username']; ?></strong></p>
<p>logout</p>
<?php endif?>
</div>
</div>
Table
<table class="table">
<thead>
<tr>
<th>Check</th>
<th>UID</th>
<th>Username</th>
<th>Num</th>
<th>Tasks</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php
$tasks = sqlsrv_query($db,
"SELECT * FROM todo
join registrieren
on username = ersteller
where username = '" . $_SESSION['username'] . "'");
Rows
$i = 1;
while ($zeile = sqlsrv_fetch_array($tasks)) {
?>
<tr id="<?php echo $zeile['id'] ?>" type="checkbox" name="check[]" value="<?php echo $zeile['id'] ?>">
<td class="task"><input type="checkbox" id="check--<?php echo $zeile['id'] ?>"></td>
<input type="hidden" name="id" value="<?php echo $zeile['id']; ?>">
<td class="task" ><?php echo $zeile['uid'] ?></td>
<td class="task" ><?php echo $zeile['username'] ?></td>
<td class="task" ><?php echo $zeile['id'] ?></td>
<td class="task" id="<?php echo $zeile['id'] ?>" ><?php echo $zeile['task'] ?></td>
<td class="task" onclick="myFunction(<?php echo $zeile['id'] ?>)">
<form method="post" action="main.php">
<button type="submit" name="edit">
Edit
</button>
<input type="hidden" name="id" value="<?php echo $zeile['id']; ?>">
<input type="hidden" name="task" value="<?php echo $zeile['task']; ?>">
</form>
</td>
<td class="task" onclick="myFunction(<?php echo $zeile['id'] ?>)">
<form method="post" action="main.php" >
<button type="submit" name="delete">
Delete
</button>
<input type="hidden" name="id" value="<?php echo $zeile['id']; ?>">
</form>
</td>
</tr>
<?php
$i++;}
?>
</tbody>
</table>
Buttons
<form method="post" action="main.php" class="input_form">
<input type="hidden" name="ersteller" value="<?php echo $_SESSION['username']; ?>">
<?php
if ($update == true): ?>
<input type="text" name="task" class="task_input" value="<?php echo $task ?>">
<button type="submit" name="update" id="add_btn" class="add_btn">Update</button>
<input type="text" name="id" value="<?php echo $id ?>">
<?php else: ?>
<input type="text" name="task" class="task_input">
<button type="submit" name="addtask" id="add_btn" class="add_btn">Add Task</button>
<input type="hidden" name="id" value="<?php echo $id ?>">
<?php endif ?>
</form>
</body>
</html>
You can use toggleClass of jQuery:
// css for put in your element
.common-class {
background-color: #ff0000; // replace for your color
}
.highlight-class {
background-color: #0000ff !important; // replace for your color
}
$('your-selector').toggleClass('highlight-class'); // jQuery
Bensilson's answer helped me and i want to clarify what i have done:
I delete the class='task'inside the <td> and put it into the <tr> - Tag.
I've done a .highlighttask{background-color: green !important} inside the css.
And inside the js i've done
$(document).ready(function() {
$(".table tr").on('click', function() {
var checked = $(this).find('input[type="checkbox"]');
checked.prop('checked', !checked.is(':checked'));
$(this).toggleClass('highlighttask');
});
});
Now it overrights the background-color on a specific row AND it checks the checkbox of the specific row.
Thanks!

call a javascript function with a select dropdown list

im working with a dynamic form that the end user can enter as much material as he wants by clicking on an add button, this button calls a JavaScript function that inserts a drop-down list into my form. if i removed the select section it works and it calls the input field but if i add the select section it doesn't call the function.
<script type="text/javascript" src="jQuery.js"></script>
<script type="text/javascript">
function add_row()
{
$rowno=$("#recipe_details tr").length;
$rowno=$rowno+1;
$("#recipe_details tr:last").after("<tr id='row"+$rowno+"'> <td><select name="Material1[]">
<?php
$sql2 = mysqli_query($conn, "SELECT * FROM Material");
while ($r = $sql2->fetch_assoc()){
?>
<option value= <?php echo $r["Material_ID"] ?>><?php echo $r["Material"]; ?></option>
<?php
// close while loop
}
?></select></td><td><input type='text' name='quantity[]' placeholder='Enter Quantity'></td><td><input type='button' value='DELETE' onclick=delete_row('row"+$rowno+"')></td></tr>");
}
function delete_row(rowno)
{
$('#'+rowno).remove();
}
</script>
<div id="wrapper">
<div id="form_div">
<form method="post" action="">
<table id="recipe_details" align=center>
<tr id="row1">
<td><select name="Material1[]">
<?php
$sql2 = mysqli_query($conn, "SELECT * FROM Material");
while ($r = $sql2->fetch_assoc()){
?>
<option value= <?php echo $r["Material_ID"] ?>><?php echo $r["Material"]; ?></option>
<?php
// close while loop
}
?></select></td>
<td><input type="text" name="quantity[]" placeholder="Enter Quantity"></td>
</tr>
</table>
<input type="button" onclick="add_row();" value="ADD ROW">
<input type="submit" name="submit_row" value="SUBMIT">
</form>
</div>
</div>
Updated code :-
<script type="text/javascript" src="jQuery.js"></script>
<script type="text/javascript">
function add_row()
{
$rowno = $("#recipe_details tr").length;
$rowno = $rowno+1;
$("#recipe_details tr:last").after(
`<tr id='row` + $rowno + `'><td><select name='Material1[]'>
<?php
$sql2 = mysqli_query($conn, "SELECT * FROM Material");
while ($r = $sql2->fetch_assoc()){
?>
<option value = <?php echo $r["Material_ID"] ?>><?php echo $r["Material"]; ?></option>
<?php
// close while loop
}
?>
<option value = 'Option 1'>Option 1</option>
</select ></td >
<td><input type='text' name='quantity[]' placeholder='Enter Quantity'></td>
<td><input type='button' value='DELETE' onclick=delete_row('row`+ $rowno + `')></td></tr>"`);
}
function delete_row(rowno)
{
$('#'+rowno).remove();
}
</script>
<div id="wrapper">
<div id="form_div">
<form method="post" action="">
<table id="recipe_details" align="center">
<tr id="row1">
<td><select name="Material1[]">
<?php
$sql2 = mysqli_query($conn, "SELECT * FROM Material");
while ($r = $sql2->fetch_assoc()){
?>
<option value ='<?php echo $r["Material_ID"] ?>><?php echo $r["Material"];?>'></option>
<?php
// close while loop
}
?></select></td>
<td><input type="text" name="quantity[]" placeholder="Enter Quantity"></td>
</tr>
</table>
<input type="button" onclick="add_row();" value="ADD ROW">
<input type="submit" name="submit_row" value="SUBMIT">
</form>
</div>
</div>
I have checked this snippet on my machine for employee list. Its working fine.
Let me know if you are still facing some issues.
It should work on your machine also.

How to display the list from database using selected value in drop down

manage_bank
Hi everyone,
Can someone help me, How to display the bank name based on selected drop down, when we choose
for example:- 50 records
then it will display 50 records of the bank name from database.
When we logout the value still remain the same 50 records.
<?php
// Includes the required files
include ("../ecompany/terms_includes.php");
?>
<?php
// Check user authority for this page
$ModuleName = "Manage Banks";
$ModuleAction = "view";
base_checkAuthority($ModuleName,$ModuleAction)
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php
include PATH_INC_HEAD;
?>
<?php
//get page number
$page = $_GET["page"];
$record = $_GET["record"];
//get Miscellaneous detail
$MiscellaneousDetail = base_getMiscellaneousDetail();
?>
<?php
if(!isset($page))
{
$page = 1;
}
if(!isset($record))
{
$record = $MiscellaneousDetail['base_mis_default'];
}
//go to reload page
if(isset($_POST['load']))
{
$page = 1;
$record = $_POST['records_per_page'];
}
//go to add profile page
if(isset($_POST['add']))
{
echo '<script type="text/javascript">' . "\n";
echo 'window.location="../ecompany/add_bank.php";';
echo '</script>';
}
//go to home page
if(isset($_POST['cancel']))
{
echo '<script type="text/javascript">' . "\n";
echo 'window.location="../terms_base/home.php";';
echo '</script>';
}
?>
</head>
<body>
<div class="container">
<?php
include PATH_INC_HEADER;
?>
<div class="main_body1">
<div id="update_category"></div>
<div style="width:800px; margin:auto">
<?php
include PATH_INC_DESC;
?>
<div style="clear:both"></div>
<form name="bank_list_form" id="bank_list_form" method="post" action="">
<div style="width:800px; margin:auto; text-align:left">
<select name="records_per_page" id="records_per_page" >
<option value="<?php echo $record; ?>">Default Records/Page :</option>
<option value="1" title="1" >1</option>
<option value="3" title="3" >3</option>
<option value="5" title="5" >5</option>
<option value="10" title="10" >10</option>
<option value="20" title="20" >20</option>
<option value="30" title="30" >30</option>
<option value="50" title="50" >50</option>
<option value="100" title="100" >100</option>
<option value="200" title="200" >200</option>
<option value="500" title="500" >500</option>
<option value="1000" title="1000" >1000</option>
</select>
<input type="" size="4" style="width:25px" name="record"
id="record"value="<?php echo $record; ?>" readonly="readonly" />
<div id="searchbutton">
<input type="submit" id="load" name="load" value="Refresh">
</div>
</div>
<table cellspacing="1" class="tablesorter">
<thead>
<tr>
<th></th>
<th>Code</th>
<th>Name</th>
<th>Description</th>
<th>Remark</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<?php ecoy_listBank($page, $record) ?>
</tbody>
<tr>
<td align="center" colspan="8"><input type="submit" id="add"
name="add" value="Add Bank" /><input type="submit" id="cancel"
name="cancel" value="Cancel" /></td>
</tr>
</tfoot>
</table>
</form>
<br />
</div>
<div class="clearboth"></div>
</div>
<?php
include PATH_INC_FOOTER;
?>
</div>
</body>
</html>
Much appreciate, Thanks.
The title of your question is misleading. But if I got it right you actually succeed in querying your data correctly and refreshing the number of records with your refresh button.
You real question is how to persist the default value of the dropdown after a logout.
By mentioning your logout feature I assume you have users and a user table in your database.
From here you have two solutions I can think of:
1. Save the users preferences like the default value of your dropdown within your users table or create a separated table dedicated for this purpose.
2. Store users preferences in cookies

Calculate values in two input boxes and get result in third input box using javascript?

<table><tr>
<?php
if ($_POST['class']) {
$sql6 = mysql_query("select * from sr_reg where present_class='$_POST[class]' order by id ASC");
$num6 = mysql_num_rows($sql6);
if ($num6) {
$i = 0;
while ($val6 = mysql_fetch_array($sql6)) {
$i = $i + 1;
?>
<form name="frm1" action="class.php" method="post">
<td bgcolor="#FFFFFF">
<div align="center"><strong>
<?php echo $i ?><input type="hidden" name="class" value="<?php echo $_POST['class']; ?>" />
</strong></div>
</td>
<td bgcolor="#FCD5B4">
<div align="center">
<input type="text" name="roll" value="" />
</div>
</td>
<td bgcolor="#C5BE97">
<div align="center">
<?php echo $val6['studentname'] . " " . $val6['sname']; ?>
</div>
</td>
<td bgcolor="#D99795">
<input type="text" name="oral" id="txt_oral[]" onkeyup="totalMarks();"/>
</td>
<td bgcolor="#8DB4E3">
<div align="center">
<input type="text" name="writen" id="txt_writen[]" onkeyup="totalMarks();" />
</div>
</td>
<td bgcolor="#D7E4BC" colspan="2">
<div align="center">
<input type="text" name="tot" id="txt_tot[]" />
</div>
</td>
</form>
</tr>
<?php
}
} else {
}
} else {
}
?>
If you use jQuery:
$("#txt_tot").val($("#txt_oral").val() + $("#txt_written.val());
http://api.jquery.com/val/
If you don't want to use jQuery for this project
document.getElementById('txt_tot').value = document.getElementById('txt_oral').value + document.getElementById('txt_written').value;
I have got my answer in threadliyakat's answer
I just changed the value * to + and added parseInt to make input values in number. function multiply changed in calculation
and onkeyup='calculate(".$i.");' in first user input and second user input .
answer is working in id='txt_tot".$i."'
its working well!!!!

Move options between two select boxes, php jquery

hi guys pls help me with this i dont know how to start to send this function to the php database. pls help me i need to send the right box to the database tnx the function of the jquery is working but i cannot think of how do i insert this to the database pls help me with this:) tnx all move options(text) two the next box and insert to the php database.
select.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Jquery move listbox items from left to right example</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script language="javascript" type="text/javascript">
function move_list_items(sourceid, destinationid)
{
$("#"+sourceid+" option:selected").appendTo("#"+destinationid);
}
//this will move all selected items from source list to destination list
function move_list_items_all(sourceid, destinationid)
{
$("#"+sourceid+" option").appendTo("#"+destinationid);
}
</script>
<style type="text/css">
select {
width:200px;
height:100px;
}
</style>
</head>
<body>
<form action="submit.php" method="post">
<table cellpadding="5" cellspacing="5">
<tbody>
<tr>
<td colspan="2">
<select id="from_select_list" multiple="multiple" name="from_select_list">
<option value="apple">Apple</option><option value="mango">Mango</option> <option value="bannana">Bannana</option> <option value="grapes">Grapes</option>
</select>
</td>
<td colspan="2">
<select id="to_select_list" multiple="multiple" name="to_select_list">
<option value="winder">Winter</option> <option value="summer">Summer</option> <option value="rainy">Rainy</option> <option value="Spring">Spring</option>
</select>
</td>
</tr>
<tr>
<td><input id="moveright" type="button" value="Move Right" onclick="move_list_items('from_select_list','to_select_list');" /></td>
<td><input id="moverightall" type="button" value="Move Right All" onclick="move_list_items_all('from_select_list','to_select_list');" /></td>
<td><input id="moveleft" type="button" value="Move Left" onclick="move_list_items('to_select_list','from_select_list');" /></td>
<td><input id="moveleftall" type="button" value="Move Left All" onclick="move_list_items_all('to_select_list','from_select_list');" /></td>
<input type="submit" name="submit" />
</tr>
</tbody>
</table>
</body>
</html>
connection.php
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$db = "copy";
$conn = mysqli_connect($dbhost,$dbuser,$dbpass,$db);
?>
submit.php
<?php
include 'connection.php'
?>
change the select option name into array then only u can get all the selected items
<td colspan="2">
<select id="to_select_list" multiple="multiple" name="to_select_list[]">
<option value="winder">Winter</option> <option value="summer">Summer</option> <option value="rainy">Rainy</option> <option value="Spring">Spring</option>
</select>
</td>
in submit.php you can check like this
echo '<pre>';
print_r($_POST);

Categories