How do I make the onchange event when I'm choosing an option from my 1st dropdown menu then auto show table without clicking submit?
How do I refresh the above table whenever I click submit on the Edit data?
<?php
$conn = new mysqli('localhost', 'root', 'jared17', 'hbadb')
or die ('Cannot connect to db');
$result = $conn->query("select * from english");
echo "<html>";
echo "<body>";
echo "<form name='form' method = POST>";
echo "<select name = 'Students'>";
while ($row = $result->fetch_assoc()) {
$LRN = $row['LRN'];
$Last = $row['Last_Name'];
$First = $row['First_Name'];
if ($LRN == $_POST['Students']) $selected = 'selected="selected"';
echo '<option value="'.$LRN.'">'.$Last.', '.$First.'</option>';
}
echo "</select>";
echo "<input type='submit' name='submit' value='Show'>";
if (isset($_POST['Students'])) {
$lrn = $_POST['Students'];
$stmt = $conn->prepare("SELECT Last_Name, First_Name, Level, Q1, Q2, Q3, Q4, FINAL FROM english WHERE LRN = ?");
$stmt->bind_param('i', $lrn);
$stmt->execute();
$stmt->bind_result($last, $first, $level, $q1, $q2, $q3, $q4, $final);
$stmt->fetch();
echo "<table><tr><th>LRN</th><th>Name</th><th>Level</th><th>Q1</th><th>Q2</th><th>Q3</th><th>Q4</th><th>Final</th></tr>";
echo "<tr><td>$lrn</td><td>$last, $first</td><td>$level</td><td>$q1</td><td>$q2</td><td>$q3</td><td>$q4</td><td>$final</td></tr></table>";
}
echo "</form>";
echo "<form name='form2' method = POST>";
///////////EDIT DATA
echo "Edit Data: ";
echo "<select name = 'Edit'>";
echo '<option value=Q1>Q1</option>';
echo '<option value=Q2>Q2</option>';
echo '<option value=Q3>Q3</option>';
echo '<option value=Q4>Q4</option>';
echo '<option value=FINAL>FINAL</option>';
echo '<input type="number" name="editdata">';
echo "</select>";
echo "<input type='submit' name='submit2' value='Edit Now'>";
if (isset($_POST['Edit'])) {
$conn2 = new mysqli('localhost', 'root', 'jared17', 'hbadb')
or die ('Cannot connect to db');
$upd = $_POST['Edit'];
$txt = $_POST['editdata'];
$now = "UPDATE english SET $upd='$txt' WHERE LRN='$lrn'";
$res = $conn2->query($now);
if (!$conn2->error) {
echo "Errormessage: $conn->error";
}
echo $now;
}
echo "</form>";
echo "</body>";
echo "</html>";
?>
All of your questions must be fixed by ajax.
$.ajax({
url: "example.php", //file which has query select to db table
data: {id:theid}, //describe your value of select option here
dataType: 'json', // type of data that will you get (JSON/HTML).
type: 'POST', //sending type (POST/GET)
success: function(data) {
showTable();
}
});
Related
I have a drop-down in html which is being rendered from php template.
I can edit drop-down choice which takes usernames from DB, but how make it "selected" (what I have choose before) before editing?
Code for the template is below.
$result = $conn->query("select username from users");
echo "<html>";
echo "<body>";
echo "<select name='workers' >";
while ($row = $result->fetch_assoc()) {
unset($username);
$username = $row['username'];
echo '<option value=" '.$username.'" >'.$username.'</option>';}
echo "</select>";
echo "</body>";
echo "</html>";
?>
Please help!
You need to give to name to select tag and get that as that name like :
<?php
$conn = new mysqli($servername, $username, $password, $dbname) or die ('Cannot connect to db');
$result = $conn->query("select username from users");
echo "<html>";
echo "<body>";
echo "<select name='drpdown'>";
while ($row = $result->fetch_assoc()) {
unset($username);
$username = $row['username'];
echo '<option value=" '.$username.'" >'.$username.'</option>';
}
echo "</select>";
echo "</body>";
echo "</html>";
?>
get the post variable like :
print_r($_POST['drpdown']);
Got a table which stores information about pupils.
CREATE TABLE pupis (name TEXT, city TEXT, caretaker TEXT);
By opening this web-site, info from this table is showed.
There is a "Sort" button, which sorts all the pupils by their cities.
Also by clicking this button, a drop down list occurs near every pupil.
This list allows to choose a caretaker for a pupil.
This way, my question is how to send all chosen caretakers from numerous drop down lists by clicking single button? (UPDATE TABLE)
I am sorry I am new to PHP and right now I do not understand how to be here. I can only suggest to create arrays dynamically that consists of pupil id and its caretaker.
I've created this web-site especially for this question and this code is how that site works:
<?php
mb_internal_encoding("UTF-8");
$link = mysql_connect($mysql_host, $mysql_user, $mysql_password)
or die('' . mysql_error());
mysql_select_db($mysql_dbname) or die('');
mysql_set_charset('utf8');
$query = 'SELECT * FROM pupils';
$result = mysql_query($query) or die(mysql_error());
echo "<table>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td>$col_value</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";
mysql_free_result($result);
mysql_close($link);
?>
<form action="" method="post">
<input type="submit" name="sort" value="Sort">
</form>
<?php
if (isset($_POST["sort"])) {
$link = mysql_connect($mysql_host, $mysql_user, $mysql_password)
or die(mysql_error());
mysql_select_db($mysql_dbname) or die();
mysql_set_charset('utf8');
$query = 'SELECT * FROM pupils WHERE city = "Moscow";' or die("died");
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) != 0) {
echo "<h1>Moscow</h1>";
echo "<table>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td>$col_value</td>\t\t";
}
echo "<td><form action=\"\" method=\"post\">";
echo "<select name=\"choose_caretaker\" required>";
echo "<option value=\"0\">Choose a caretaker</option>";
echo "<option value=\"Caretaker1\">Caretaker1</option>";
echo "<option value=\"Caretaker2\">Caretaker2</option>";
echo "<option value=\"Caretaker3\">Caretaker3</option>";
echo "</select></td>";
echo "</form>";
echo "</tr>\n";
}
echo "</table>\n";
}
$query = 'SELECT * FROM pupils WHERE city = "London";' or die("died");
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) != 0) {
echo "<h1>London</h1>";
echo "<table>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td>$col_value</td>\t\t";
}
echo "<td><form action=\"\" method=\"post\">";
echo "<select name=\"choose_caretaker\" required>";
echo "<option value=\"0\">Choose a caretaker</option>";
echo "<option value=\"Caretaker1\">Caretaker1</option>";
echo "<option value=\"Caretaker2\">Caretaker2</option>";
echo "<option value=\"Caretaker3\">Caretaker3</option>";
echo "</select></td>";
echo "</form>";
echo "</tr>\n";
}
echo "</table>\n";
}
mysql_free_result($result);
mysql_close($link);
}?>
<form action="" method="post">
<input type="submit" name="send_to_caretakers" value="Send">
</form>
<?if (isset($_POST["send_to_caretakers"])) {
$link = mysql_connect($mysql_host, $mysql_user, $mysql_password)
or die(' ' . mysql_error());
mysql_select_db($mysql_dbname) or die('');
mysql_set_charset('utf8');
echo "Sent to the caretakers";
$temp = $_REQUEST['choose_caretaker'];
echo $temp;
// Have no idea how it can designed
mysql_close($link); // Закрываем соединение
}
?>
To use array for "select name" e.g choose_caretaker[pupil_id]
add in "multiple" in select box to select mutiple
render your result in php. you will see your caretaker under a pupil in array format.
echo "<td><form action=\"\" method=\"post\">";
echo "<select name=\"choose_caretaker[$link['id']]\" multiple required>";
echo "<option value=\"0\">Choose a caretaker</option>";
echo "<option value=\"Caretaker1\">Caretaker1</option>";
echo "<option value=\"Caretaker2\">Caretaker2</option>";
echo "<option value=\"Caretaker3\">Caretaker3</option>";
echo "</select></td>";
echo "</form>";
How do I refresh the table when I click EDIT NOW Button? or this is easier, how do I make refresh button to refresh the table? I have no knowledge on JSP to auto refresh. However, I manage to make an onchange event so when I choose menu, it will refresh upon changing. When I edit a data and submit, it does not refresh. How do I re-execute the echo of table? Thanks!
<?php
$selected='';
function get_options($select)
{
$conn = new mysqli('localhost', 'root', 'jared17', 'hbadb')
or die ('Cannot connect to db');
$result = $conn->query("select * from students");
$options='';
while ($row = $result->fetch_assoc())
{
$LRN = $row['LRN'];
$Last = $row['Last_Name'];
$First = $row['First_Name'];
if($LRN == $_GET['Students'])
{
$options.='<option value="'.$LRN.'" selected>'.$Last.', '.$First.'</option>';
}
else
{
$options.='<option value="'.$LRN.'">'.$Last.', '.$First.'</option>';
}
}
return $options;
}
if (isset($_GET['Students'])) {
$conn = new mysqli('localhost', 'root', 'jared17', 'hbadb')
or die ('Cannot connect to db');
$result = $conn->query("select * from students");
$lrn = $_GET['Students'];
$stmt = $conn->prepare("SELECT Last_Name, First_Name, Level, Q1, Q2, Q3, Q4, FINAL FROM english WHERE LRN = ?");
$stmt->bind_param('i', $lrn);
$stmt->execute();
$stmt->bind_result($last, $first, $level, $q1, $q2, $q3, $q4, $final);
$stmt->fetch();
echo "<table><tr><th>LRN</th><th>Name</th><th>Level</th><th>Q1</th><th>Q2</th><th>Q3</th><th>Q4</th><th>Final</th></tr>";
echo "<tr><td>$lrn</td><td>$last, $first</td><td>$level</td><td>$q1</td><td>$q2</td><td>$q3</td><td>$q4</td><td>$final</td></tr></table>";
}
echo "<html>";
echo "<body>";
echo "<form method=GET>";
echo "<select name=Students onchange=this.form.submit();>";
echo get_options();
echo "</select>";
echo "</form>";
echo "<form method=POST>";
///////////EDIT DATA
echo "Edit Data: ";
echo "<select name = 'Edit'>";
echo '<option value=Q1>Q1</option>';
echo '<option value=Q2>Q2</option>';
echo '<option value=Q3>Q3</option>';
echo '<option value=Q4>Q4</option>';
echo '<option value=FINAL>FINAL</option>';
echo '<input type="number" max="100" name="editdata" required>';
echo "</select>";
echo "<input type='submit' name='submit2' value='Edit Now'>";
if (isset($_POST['Edit'])) {
$conn2 = new mysqli('localhost', 'root', 'jared17', 'hbadb')
or die ('Cannot connect to db');
$upd = $_POST['Edit'];
$txt = $_POST['editdata'];
$now = "UPDATE english SET $upd='$txt' WHERE LRN='$lrn'";
$res = $conn2->query($now);
if (!$conn2->error) {
echo "Errormessage: $conn->error";
}
echo $now;
}
echo "</form>";
echo "</body>";
echo "</html>";
?>
I would use 2 pages view.php and edit.php. View would display the data with link to edit. When you click edit it would open edit.php to load the form to edit data and save it to db. Then issue the command
header("Location: view.php");
to reload the view.php and display the new data
I have uploaded the scripts I use as a basis for databases so you can see if this is what you want - feel free to amend any data it is just a test database. If you want more code just ask
www.cambodia.me.uk/php/view.php
Edited to include scripts as requested - sorry it is old code and mysql not mysqli
Connect script
<?php
/*
CONNECT-DB.PHP
Allows PHP to connect to your database
*/
// Database Variables (edit with your own server information)
$server = 'server';
$user = 'user';
$pass = 'pass';
$db = 'database';
// Connect to Database
$connection = mysql_connect($server, $user, $pass)
or die ("Could not connect to server ... \n" . mysql_error ());
mysql_select_db($db)
or die ("Could not connect to database ... \n" . mysql_error ());
?>
View script
<?php
include('remote-connect.php');
$result = mysql_query("SELECT * FROM stats")
or die(mysql_error());
echo "<p><b>View All</b> | <a href='view-paginated.php?page=1'>View Paginated</a></p>";
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>date</th> <th>Home Team</th> <th></th><th></th><th>Away Team</th> <th></th> </tr>";
while($row = mysql_fetch_array( $result )) {
echo "<tr>";
echo '<td>' . $row['date'] . '</td>';
echo '<td>' . $row['hometeam'] . '</td>';
echo '<td>' . $row['fthg'] . '</td>';
echo '<td>' . $row['ftag'] . '</td>';
echo '<td>' . $row['awayteam'] . '</td>';
echo '<td>Edit</td>';
//echo '<td>Delete</td>';
echo "</tr>";
}
echo "</table>";
?>
<p>Add a new record</p>
</body>
</html>
Edit script
<!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>Untitled Document</title>
</head>
<body>
<?php
function renderForm($id, $hometeam, $awayteam, $error)
{
if ($error != '')
{
echo '<div style="padding:4px; border:1px solid red; color:red;">'.$error.'</div>';
}
?>
<form action="" method="post">
<input type="hidden" name="id" value="<?php echo $id; ?>"/>
<div>
<p><strong>ID:</strong> <?php echo $id; ?></p>
<strong>First Name: *</strong> <input type="text" name="hometeam" value="<?php echo $hometeam; ?>"/><br/>
<strong>Last Name: *</strong> <input type="text" name="awayteam" value="<?php echo $awayteam; ?>"/><br/>
<p>* Required</p>
<input type="submit" name="submit" value="Submit">
</div>
</form>
</body>
</html>
<?php
}
include('remote-connect.php');
if (isset($_POST['submit']))
{
if (is_numeric($_POST['id']))
{
$id = $_POST['id'];
$hometeam = mysql_real_escape_string(htmlspecialchars($_POST['hometeam']));
$awayteam = mysql_real_escape_string(htmlspecialchars($_POST['awayteam']));
if ($hometeam == '' || $awayteam == '')
{
$error = 'ERROR: Please complete all mandatory fields!';
renderForm($id, $hometeam, $awayteam, $error);
}
else
{
mysql_query("UPDATE stats SET hometeam='$hometeam', awayteam='$awayteam' WHERE id='$id'")
or die(mysql_error());
// Go back to view page and redisplay the edited data
header("Location: view.php");
}
}
else
{
echo 'Error!';
}
}
else
{
if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0)
{
$id = $_GET['id'];
$result = mysql_query("SELECT * FROM stats WHERE id=$id")
or die(mysql_error());
$row = mysql_fetch_array($result);
if($row)
{
$hometeam = $row['hometeam'];
$awayteam = $row['awayteam'];
renderForm($id, $hometeam, $awayteam, '');
}
else
{
echo "No results!";
}
}
else
{
echo 'Error!';
}
}
?>
</body>
</html>
It's so easy. If you fetch your table's values after edit or delete operation, sql will bring fresh data.
Need help for my last query. When I submit, I want to show all the columns of that particular LRN. I can't get it to work. When I click submit, it doesn't show anything. Thanks in advance.
<?php
$conn = new mysqli('localhost', 'root', 'user, 'pwd')
or die ('Cannot connect to db');
$result = $conn->query("select * from students");
echo "<html>";
echo "<body>";
echo "<form method = POST>";
echo "<select name = 'Students' onchange=this.form.submit()>";
while ($row = $result->fetch_assoc()) {
unset($LRN, $First, $Last);
$LRN = $row['LRN'];
$First = $row['First_Name'];
$Last = $row['Last_Name'];
echo '<option value="'.$LRN.'">'.$Last.', '.$First.'</option>';
}
echo "</select>";
echo "<input type='submit' name='submit' value='Show'>";
echo "</form>";
$submit = filter_input(INPUT_POST,'submit');
$students = filter_input(INPUT_POST,'Students');
if(isset($submit)) {
$tae = $conn->query('select * from students WHERE LRN=.$Students.');
echo $tae;
}
echo "</body>";
echo "</html>";
?>
It is showing all the students in database and textbox with them. But when I enter marks of every student it picks only the first record and save only marks.
I want:
all name of student should print on page with text-box in which I will enter marks of each student.
I want to save the record of every student with their marks.
--
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
$sql = "SELECT * FROM stunr WHERE stucou = '".$_POST["cls"]."'";
mysql_select_db('college-db');
$retval = mysql_query( $sql, $conn );
if($retval === FALSE) {
die(mysql_error());
}
$row = mysql_fetch_array($retval);
echo "<div style='margin-top:120px; margin-left:180px;'>";
$cla=$row['stucou'];
echo "<h3>Class"."<b> $cla</b>"."</h3>";
echo"</div>";
echo "<div style='margin-top:50px; margin-left:180px;'>";
while($row = mysql_fetch_array($retval)) {
echo"<form method='post' action='mst-marks-insert.php'>";
$clsnm = $row['stufname'];
echo"$clsnm";
echo "<br>";
echo"<input type='text' name='mas' class='textbox' placeholder='Enter Marks'/><br><br>";
}
echo "<input type='text' name='tot' />";
echo"<input type='submit' />";
echo"</form>";
echo"</div>";
I would start with moving the beginning form tag before the while loop.
echo"<form method='post' action='mst-marks-insert.php'>";
while($row = mysql_fetch_array($retval))
{
Do something like this:
Move out the beginning form before while loop
Make field "mas" an array
This could look like this
<?php
mysql_select_db('college-db');
$retval = mysql_query($sql, $conn);
if ($retval === FALSE) {
die(mysql_error());
}
$row = mysql_fetch_array($retval);
echo "<div style='margin-top:120px; margin-left:180px;'>";
$cla = $row['stucou'];
echo "<h3>Class" . "<b> $cla</b>" . "</h3>";
echo"</div>";
echo "<div style='margin-top:50px; margin-left:180px;'>";
echo "<form method='post' action='mst-marks-insert.php'>";
while ($row = mysql_fetch_array($retval)) {
$clsnm = $row['stufname'];
echo "$clsnm";
echo "<br>";
echo "<input type='text' name='mas[$clsnm]' class='textbox' placeholder='Enter Marks'/><br><br>";
}
echo "<input type='text' name='tot' />";
echo "<input type='submit' />";
echo "</form>";
echo "</div>";
?>
In your "mst-marks-insert.php" the $_POST var then looks something like this:
<?php
var_dump($_POST['mas']);
array (
'clsnm1' => 'test',
'clsnm2' => 'test2',
)
?>
So you could do something like this (it's just an example what would be possible, because I don't know how your SQL table looks like):
<?php
foreach ($_POST['mas'] as $clsnm => $mas) {
mysql_query("INSERT INTO `mastable` (`clsnm`, `mas`) VALUES ('".mysql_real_escape_string($clsnm)."', '".mysql_real_escape_string($mas)."')");
}
?>