value of $_POST is always the last value of my <tr> - javascript

I have a problem in which I cannot solve. I have a simple page where I query all Users and list them in a table. When a user clicks on one of the table rows, it should be taken to another page where the user can edit information of the that they picked. The problem is that in my script, the $_POST value is always the value of the last
CODE
<?php
include "conn.php";
$pquery = "SELECT * FROM Patient NATURAL JOIN User ORDER BY LastName;";
$patientQuery = $conn->query($pquery);
if (mysqli_num_rows($patientQuery) == 0)
echo "<p>No patients found.</p>";
else{
while($assoc = $patientQuery->fetch_assoc()){
echo "<tr onclick = 'sub();'>";
echo "<td>";
echo $assoc['UserID'];
echo "<input type = 'hidden' name = 'UserID' value = '". $assoc['UserID'] ."' />";
echo "</td>";
echo "<td>";
echo $assoc['FirstName'];
echo "</td>";
echo "<td>";
echo $assoc['LastName'];
echo "</td>";
echo "</tr>";
}
}
?>
<script>
function sub(){
document.getElementById("edit").submit();
return false;
}
</script>

I've slightly modified your code - this should work:
<?php
include "conn.php";
$pquery = "SELECT * FROM Patient NATURAL JOIN User ORDER BY LastName;";
$patientQuery = $conn->query($pquery);
if (mysqli_num_rows($patientQuery) == 0)
echo "<p>No patients found.</p>";
else{
while($assoc = $patientQuery->fetch_assoc()){
echo "<tr onclick = 'sub(". $assoc['UserID'] .");'>";
echo "<td>";
echo $assoc['UserID'];
echo "</td>";
echo "<td>";
echo $assoc['FirstName'];
echo "</td>";
echo "<td>";
echo $assoc['LastName'];
echo "</td>";
echo "</tr>";
}
}
?>
<script>
function sub(UserID){
document.location.href = 'http://www.yourdomain.com/something.php?UserID='+UserID;
return false;
}
</script>

Related

How can create a separate click event for each input button displayed using a while loop

I am listing users from the database in a table. for each user; there is an 'Approve' and 'Reject' button.
I want to put my code in such way that when i click each button for each user,only the approval or the reject event should affect that specific user? My table looks like this:
right now when i click 'approve' on one user, the other user's approval event also gets triggered. here is my approval code:
$sql = "select * from user_table ORDER BY date_registered DESC LIMIT 10";
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result)){
$emailadr = $row["email"];
$adresses [] = $emailadr;
echo "<tr>";
echo "<td>";
echo $row["firstname"];
echo "</td>";
echo "<td>";
echo $row["lastname"];
echo "</td>";
echo "<td>";
echo $row["idnumber"];
echo "</td>";
echo "<td>";
echo $row["qualification"];
echo "</td>";
echo "<td>";
echo $row["dofa"];
echo "</td>";
echo "<td>";
echo $row["date_registered"];
echo "</td>";
echo "<td>";
echo '<button name="approve" value='. $row["user_id"].'>Approve</button> <button name="reject" value='. $row["user_id"].'>Reject</button>';
echo "</td>";
echo '</tr>';
}
if(isset($_POST['approve'])){
// Approved user ID is now in $_POST['approve']
mail($adresses[$row["user_id"]],'subect
1','approved','From:email#example.com');
}elseif(isset($_POST['reject'])){
//
}
}
print_r($adresses);
When inserting new users, you normally have a auto increment column that relates to each row or user (generally called ID) you can do it a few ways first thing you need to do is include that ID or value so you know which user to change:
Create your Buttons:
<button name="approve" value="5"> Approve</button>
<button name="reject" value="5"> Reject</button>
You would update the value to your user ID
You can then simply check whether you have Approved or Rejected the user:
if(isset($_POST['approve'])){
// Approved user ID is now in $_POST['approve']
}elseif(isset($_POST['reject'])){
// Rejected user ID is now in $_POST['reject']
}
To get $adresses you need to assign it outside the loop. Also updated $adresses [] to array_push($adresses, $row["email"]); just because of preference but can be changed back if you prefer.
<?php
$adresses = array();
$sql = "select * from user_table ORDER BY date_registered DESC LIMIT 10";
if(mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)){
array_push($adresses, $row["email"]);
echo "<tr>";
echo "<td>";
echo $row["firstname"];
echo "</td>";
echo "<td>";
echo $row["lastname"];
echo "</td>";
echo "<td>";
echo $row["idnumber"];
echo "</td>";
echo "<td>";
echo $row["qualification"];
echo "</td>";
echo "<td>";
echo $row["dofa"];
echo "</td>";
echo "<td>";
echo $row["date_registered"];
echo "</td>";
echo "<td>";
echo '<button name="approve" value='. $row["user_id"].'>Approve</button> <button name="reject" value='. $row["user_id"].'>Reject</button>';
echo "</td>";
echo '</tr>';
}
if(isset($_POST['approve'])){
mail($adresses[$_POST["approve"]],'subect 1','approved','From:email#example.com');
//update DB to approved
}elseif(isset($_POST['reject'])){
//update DB to rejected
}
}
print_r($adresses);
?>
Yes.. Passing the contentid or some unique identifier will solve this.

loop list of input checkbox and get values

I am trying to get a table of names checked,
and send this array to a PHP page.
But I dont know how, I began by this code:
$(".ok").click(function(){
$("input[type='checkbox']:checked").each(function() {
var tt = $(this).val();
alert(tt);
});
});
echo "<table>";
foreach ($result as $data) {
echo "<tr>";
echo "<td><input type='checkbox' value='$data[0]' /></td><td>$data[0]</td>";
echo "</tr>";
}
echo "</table>";
Try..
$(".ok").click(function(){
//check if any are checked and if so redirect..
if($("input[type='checkbox']:checked").length > 0)
window.location.href = 'page.php?'+$("input[type='checkbox']:checked").serialize();
});
then in page.php..
$result = $_GET;
if(count($result)){
foreach($result as $data){
echo "<tr>";
echo "<td><input type='checkbox' value='$data' /></td><td>$data</td>";
echo "</tr>";
}
}

Echoing a table in PHP

I want to have this table in PHP? how can I create this and use it in Javascript after?
echo "<table border='1'>
<tr>
<th>word</th>
<th>meaning</th>
<th>checking</th>
</tr>";
while($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['word'] . "</td>";
echo "<td>";
echo "<div";
echo "class='hiding' style='display:none'>" . $row['meaning'];
echo "</div>";
echo "</td>";
echo "<td>";
echo "<input name=\"f\" type=\"checkbox\" value=\"\"> ";
echo "</td>";
echo "</tr>";
}
echo "</table>";
I want to use columns that have class=hiding attribute. but in this way it doesn't work and I have an error:
document.getElementsByClassName(...).item(0) is null
document.getElementById('hiding').style.visiblility = 'visible';
I think I should echo table another way but I don't know how?
Here is my Javascript code:
document.getElementsByClassName.item(0).('hiding').style.visiblility = 'visible';
You can not use getElementById to get an element by its classname.
Try something like
document.getElementsByClassName('hiding')[0].style.visibility = 'visible'
change style.visiblility to style.display, and style.display = 'block' for shown

Clickable data displayed from mysql using PHP

I'm not very good at JavaScript but here is my problem.
I have three pages: page1.php, page2.php, page3.php
On page1.php, I have a form for users to select grade level they want to view, then the action is performed on page2.php -- displaying the list of all students in that grade.
This is the code for page2.php
<?php
//database variables
require_once('admin_settings.php');
//these variables are from a form used to display the current data
$level = $_POST['level_group'];
$room = $_POST['room_group'];
$con=mysqli_connect("$host","$dbuser","$dbpass","$dbname");
mysqli_set_charset($con, "utf8");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT std_id, std_name FROM students WHERE std_level LIKE '$level%' AND std_room LIKE '$room';");
//table
echo"
<table border='1' id='mytable'>
<tr bgcolor = #99CCFF>
<th><b>Student ID</b></th>
<th><b>Name</b></th>
<th><b>Action</b></th>
</tr>";
//loop through the database
while($row = mysqli_fetch_array($result))
{
echo"<form action='view_one_student.php' method='post'>";
echo "<tr bgcolor = '#c0eae4n' id = 'listings'>";
echo "<td name= 'stdid'>" . $row['std_id'] . "</td>";
echo "<td>" . $row['std_name'] . "</td>";
echo "<td>" . '<input type="submit" value="view"> <input type="submit" value="sdq">' . "</td>";
echo "</tr>";
echo "</form>";
}
echo "</table>";
mysqli_close($con);
?>
The question...now is How can I write the code on page3.php so that when users clicks on view or sdq button next to each column, the student ID should be captured, send a request to the database, and query other data related to this particular student such as age, address, phone..etc. and display them on that page3.php
page2.php
You have to add a hidden input with the id of the student in page2.
<input type="hidden" value="'.$row["std_id"].'" name="std_id">
Change your action page to page3 (if that's where you want to display the student info: name, age...)
<form action='page3.php' method='post'>page2.php
The code for page2:
<?php
//database variables
require_once('admin_settings.php');
//these variables are from a form used to display the current data
$level = $_POST['level_group'];
$room = $_POST['room_group'];
$con=mysqli_connect("$host","$dbuser","$dbpass","$dbname");
mysqli_set_charset($con, "utf8");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT std_id, std_name FROM students WHERE std_level LIKE '$level%' AND std_room LIKE '$room';");
//table
echo"
<table border='1' id='mytable'>
<tr bgcolor = #99CCFF>
<th><b>Student ID</b></th>
<th><b>Name</b></th>
<th><b>Action</b></th>
</tr>";
//loop through the database
while($row = mysqli_fetch_array($result))
{
echo"<form action='page3.php' method='post'>";
echo "<tr bgcolor = '#c0eae4n' id = 'listings'>";
echo "<td name= 'stdid'>" . $row['std_id'] . "</td>";
echo "<td>" . $row['std_name'] . "</td>";
echo "<td> <input type='hidden' value='" . $row["std_id"] . "' name='std_id'>";
echo "<input type='submit' value='view'>";
echo "<input type='submit' value='sdq'></td>";
echo "</tr>";
echo "</form>";
}
echo "</table>";
mysqli_close($con);
?>
page3.php
A posible solution for page3 can be: (add your columns you need: age, address, phone..)
<?php
//database variables
require_once('admin_settings.php');
//these variables are from a form used to display the current data
$id = $_POST['std_id'];
$con=mysqli_connect("$host","$dbuser","$dbpass","$dbname");
mysqli_set_charset($con, "utf8");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM students WHERE std_id ='$id';");
echo '<h3>Student detail</h3>';
//table
echo"
<table border='1' id='mytable'>
<tr bgcolor = #99CCFF>
<th><b>Student ID</b></th>
<th><b>Name</b></th>
<th><b>Age</b></th>
</tr>";
//loop through the database
while($row = mysqli_fetch_array($result))
{
echo "<tr bgcolor = '#c0eae4n' id = 'listings'>";
echo "<td name= 'stdid'>" . $row['std_id'] . "</td>";
echo "<td>" . $row['std_name'] . "</td>";
echo "<td>" . $row['std_age'] . "</td>";
echo "</tr>";
echo "</form>";
}
echo "</table>";
mysqli_close($con);
?>
You can modify your code like this. A hidden field will send the student id to the page of view_one_student.php.
while($row = mysqli_fetch_array($result))
{
echo"<form action='view_one_student.php' method='post'>";
echo "<tr bgcolor = '#c0eae4n' id = 'listings'>";
echo "<td name= 'stdid'>" . $row['std_id'] . "</td>";
echo "<td>" . $row['std_name'] . "</td>";
echo '<input type="hidden" value="'.$row["std_id"].'" name="std_id">';
echo "<td>" . '<input type="submit" value="view"> <input type="submit" value="sdq">' . "</td>";
echo "</tr>";
echo "</form>";
}
echo "</table>";
mysqli_close($con);
On view_one_student.php you have to catch that with:
$_POST["std_id"]
And eventually input that into a sql sentense in view_one_student.php:
$sql = "SELECT * FROM <table> WHERE id=".$_POST["std_id"];
Thats the general idea.

How to add select box field dynamically after clicking add button by using php or javascript and store selected value in selected box in database

if (isset($_POST['dept']) && isset($_POST['batch']) && isset($_POST['Month']) && isset($_POST['Year']) && isset($_POST['semester'])) // based on these values selected from database
{
$dept = $_POST['dept'];
$batch = $_POST['batch'];
$month = $_POST['Month'];
$year = $_POST['Year'];
$semester = $_POST['semester'];
$query = db_select('student_master');
$query->fields('student_master', array('reg_no','name','dob','dept_code','degree','batch_year'));
$query->condition('dept_code',$dept,'=') AND $query->condition('batch_year',$batch,'=');
$results = $query->execute();
echo "<table>";
echo "<tr>";
echo "<td><label for='reg_no'> Registration Number </label></td>";
echo "<td>";
echo "<select name='reg_no'>";
foreach($results as $student_result)
{
echo "<option value ='$student_result->reg_no'> $student_result->reg_no</option>";
}
echo "</select>";
echo "</td>";
echo "</tr>";
$query = db_select('subject');
$query->fields('subject', array('subject_name','credits','subject_code'));
$query->condition('dept_code',$dept,'=') AND $query->condition('semester_appear',$semester,'=') ;
$subject_results = $query->execute();
echo "<tr>";
echo "<td><label for='Subject'>Subject Name</label></td>";
echo "<td>";
echo "<select name = 'sub_name' id = 'sub_name'>";
foreach($subject_results as $subjects_result)
{
echo "<option value ='$subjects_result->subject_code'> $subjects_result->subject_name</option>";
}
echo "</select>";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td><label for='Subject'>Subject Serial Number</label></td>";
echo "<td>";
echo "<select name='subject_serial' id = 'sub_name'>";
if ($semester == "SEMESTER-I")
{
for($i=101; $i<=110; $i++ )
{
echo "<option value ='$i'>$i</option>";
}
}
elseif ($semester == "SEMESTER-II")
{
for($i=201; $i<=210; $i++ )
{
echo "<option value ='$i'>$i</option>";
}
}
elseif ($semester == "SEMESTER-III")
{
for($i=301; $i<=310; $i++ )
{
echo "<option value ='$i'>$i</option>";
}
}
elseif ($semester == "SEMESTER-IV")
{
for($i=401; $i<=410; $i++ )
{
echo "<option value ='$i'>$i</option>";
}
}
echo "</select>" ;
echo "</td>";
echo "</tr>";
echo "</table>";
}
else
{
return "please check the your input";
}
How to add select box field dynamically after clicking add button (the selected box contain the datas from database after clicking the add button, and same selected value store in database) by using php or javascript and store selected value in selected box to database
Create a PHP page that returns the required HTML string only
And then on client side using jquery you can append that html to desired element
e.g.
$("#btnAddnew").click(function(){
$.get('ajax/test.php?id=1', function(data) {
$('#divDropdown').html(data);
});
});
Then in post you can pass the selected dropdown's value using javascript
e.g. If it is like
then $("#drpRegNum").val() would give you the selected value

Categories

Resources