Objective: My main.php displays the courses in my class. Based on which one I click, It should show me all the projects for that specific course ( either a dropdown or for now I'm displaying them in displayProjects.php) So i need to get the id of the clicked php button, write a query to search that name with it's corresponding class_id in the student table, in order to get the project names. DO I need some sort of Ajax call to get the php onclick button value or maybe a session var to display in my displayprojects.php page?
Main.php
result = mysqli_query($link,"SELECT `course_code`FROM `class`");
echo "<table>";
while($row = mysqli_fetch_array($result)){
echo "<a href='displayProjects.php'> <input type='submit' id='project' class='btn btn-primary' style='display:block; width:10%; margin-bottom:10px;' value=". $row['course_code'] . ">";
}
echo "<tr><td><a href='CreateNewClass.php'><button type='button'> Create a New Class </button></a></td></tr>";
echo "</table>";
displayProjects.php
Need help modifying this query to get projects based on class_id
$projectQuery=mysqli_query($link, "SELECT DISTINCT project_name from Student S ");
echo "<table>";
while($row = mysqli_fetch_assoc($projectQuery)) {
echo "<a href='displayGroups.php'> <input type='button' id='project' class='btn btn-primary' style='display:block; width:10%; margin-bottom:10px;' value=". $row['project_name'] . ">";
}
echo "</table>";
class table .
.
student table
Related
I have this delete function in my system but first I need the server needs to know which table he has to delete that is why I am sending a data when the user click the delete button into the server which is the ID of the data I want to delete..first I need to try and get that data being sent by the form but the problem is sending data is not working in my part I tried to echo out the ID just to see if I have a result but it works fine but when I send it to the server it doesn't print anything.. Here is my code where I fetch the scheduleID and the form
if ($strand<>""){
$query1 = mysqli_query($conn,"SELECT * from schedule natural join instructor where day = 'm' and schedule.strand= '$strand' and timeID ='$id' and grade = '$grade' and semester = '$semester'");
}
$row1 = mysqli_fetch_array($query1);
$schedID = $row['scheduleID'];
$id = $row1 ['scheduleID'];
$count=mysqli_num_rows($query1);
if ($count==0)//checking
{
//echo "<td></td>";
}
else
{
//print
echo "<div class='show'>";
echo "<ul>
<li class='options' style='display:inline'>
<span style='float:left;'><a href='sched_edit.php?id=$id1' class='edit' title='Edit'>Edit</a></span>
<span class='action'><a href='#' id='$id1' class='delete' title='Delete'>Remove</a></span>
</li>";
echo "<form class = 'delete' method = 'post' action ='../functions/delete.php'>";
echo "<li class='showme'>";
echo " <input type='hidden' name='delete' value='$id'>";
echo "<button type='submit' name='delete' class='btn btn-danger'>Display Schedule</button>";
echo $row1['subject'];
echo "</li>";
echo "<li class='$displayc'>$row1[strand]";
echo "<li class='$displaym'>$row1[fname], $row1[lname]</li>";
echo "<li class='$displayr'>Room $row1[room]</li>";
echo "</form>";
echo "</ul>";
echo "</div>";
}
?>
</td>
I tried the hidden attribute in some of my forms and it work there but I don't know why it won't in this form, the $id is working also I tried echoing that inside in this page but the data sent is not printing in the server, here's my server
<?php
session_start();
include 'database.php';
if (isset($_POST['delete'])){
$ID = $_POST['delete'];
}
thanks in advance
The problem here is that you have 2 form elements using the same name attribute, so PHP is only keeping the last value, which is not defined.
See both the <input> and the <button> have the same name! The button is last and has no value; that is what is being used by PHP.
<input type='hidden' name='delete' value='$id'>
<button type='submit' name='delete' class='btn btn-danger'>Display Schedule</button>
So you can just remove the name from the button or change it to something other than delete :-)
Good Afternoon.
I just want to ask a Question but before that let me explain it to all of you in a best way I can
As of now I have the ff.
Database: Election2016
Table: Candidate_Info
Fields: CandidateName and Position
As of now here is my code and the output of this is Show the Data in HTML Table
<html>
<center>
<font size="2" face = "century gothic">
<?php
$con = mysqli_connect("localhost", "root", "", "election2016");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con, "SELECT * FROM candidate_info");
echo "<table border='1'>
<tr>
<th>CandidateName</th>
<th>Position</th>
</tr>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['CandidateName'] . "</td>";
echo "<td>" . $row['Position'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</center>
</font>
</html>
And my target here is how can I attach a Radio Button to it? next to it? Attach a Radio Button in every row populated.
and my next target here which is very tricky to me is that when I press a button how will the data i choose with the corresponding radiobutton will be saved?
Example:
Candidate Name
Student 1
Position
President
Radio Button 1 (Example name of the Radio Button)
I selected RadioButton1 and press button "Save" how will Student 1 and President will be saved in a table?
I hope you understand TYIA
You should create one primary_key field to the table Candidate_Info as CandidateId with Auto increment and not null.
To add radio button
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td><input type='radio' name='candidateid' value='".$row['CandidateId']."' />" . $row['CandidateName'] . "</td>";
echo "<td>" . $row['Position'] . "</td>";
echo "</tr>";
}
while select the radio button and saving the form you should get the candidate record as
SELECT * FROM candidate_info WHERE CandidateId=[CandidateId]
replace the [CandidateId] with the selected radio button value.
After executing the above query you will be getting the selected candidate info, then you can save it.
while($row = mysqli_fetch_assoc($result))
{
echo "<tr>";
echo "<form action='./form_page.php' method='post'>";
echo "<td style='width:9%'>" . $row['studentID']."</td>";
echo "<td style='width:9%'><input type='submit' name='Prove' value='Approve' /></td>";
echo "<td style='width:9%'><input type='submit' name='$count' value='Delete' /></td>";
echo "</form>";
echo "</tr>";
}
I want get the specific student id, but when I reached to here, I dont know how can I get it. If I put $row['studentID'] for button name, I cant use POST['varname'] beacause the name would be different when I select is different.
Submit buttons are identified by their name.
For e.g. If there are 3 forms with 3 submit buttons named Accept1, Accept2 and Accept3.
When user submits a form by clicking on Accept1, the POST variable will be $_POST['Accept1']. Similarly $_POST['Accept2'], $_POST['Accept3'] respectively.
You can also print POST variable to get a clear idea i.e. print_r($_POST).
If i put $row['studentID'] for button name, i cant use POST['varname']
You can loop over all the values in $_POST and see if any of them match the pattern of a student ID.
Alternatively, you can use a button element (which will let you have display text and a value which don't match each other):
<button name="approve"
value="<?php echo htmlspecialchars($row['studentID'])>
Approve
</button>
and then just test for $_POST['approve'].
Try like this..
while($row = mysqli_fetch_assoc($result))
{
echo "<tr><form action='./form_page.php' method='post'>";
echo "<td style='width:9%'>" . $row['studentID']."</td>";
echo "<input type='hidden' name='studentID' value='".$row['studentID']."'>";
echo "<td style='width:9%'><input type='submit' name='Prove' value='Approve'>;
echo "<td style='width:9%'><input type='submit' name='$count' value='Delete'>
</td></form></tr>";
}
By using input type hidden you will be able to get the student id when submit the from. Hope this will help
I have on my page a dataTable with tons of rows. Each row has an id, name, surname and an action column. In the action column there is a textarea where you can add a comment and a button that submits that comment. When I submit the comment I want the page to position itself where it was, but I cannot figure it out how. Any ideas?
Here is the snippet of the code:
$result = mysql_query($query, $connection);
while ($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<form method='post' action='saveComment.php#position_".$row['id']."'>";
echo "<td hidden><input hidden name='id' readonly value=" . $row['id'] . " /></td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['surname'] . "</td>";
echo "<td><input type='submit' name='submit' value='Comment'/></td>";
echo "<a id='position_".$row['id']."'></a>";
echo "</tr>";
}
I tried passing the id of the current row to the script that saves the comment and it does pass the value, after which it should position it back where it was, but it doesn't seem to work :/
use Ajax to submit the form, and on success:
window.location = 'saveComment.php#position_<? echo $row['id']; ?>';
I found out how it works with anchors! The problem was that I didn't pass the id in the saveComment.php script when redirecting back to the original page.
Code in the page where the form is:
if ($_POST['submit']){
$id=$_POST["id"];
header('Refresh: 0.5; URL=originalPage.php#position_' . $id);
//...rest of code goes here...
}
That's it :)
You need to use
<a name='position_".$row['id']."'></a>
instead of
<a id='position_".$row['id']."'></a>
We can't get the delete "submit" button to set, otherwise it would work, we can't understand what part of the code is wrong.
Imagine a table row with a few columns, which contain data from a database in the td's
// While it goes through each row (which is selected from another SQL query above, but I won't bother pasting that)
while ($row = mysql_fetch_array($result)) {
// Variable is equal to the Request ID (e.g. 10)
$numb = $row['ReqID'];
// Echo's the table td rows with each column name in the database
echo "<tr>";
echo "<td class='req' align='center'>".$row['ReqID']."</td>";
echo "<td class='req' align='center'>".$row['ModTitle']."</td>";
echo "<td class='req' align='center'>".$row['BuildingID']."</td>";
echo "<td class='req' align='center'>".$row['RoomID']."</td>";
echo "<td class='req' align='center'>".$row['Priority']."</td>";
echo "<td class='req' align='center'>".$row['W1']."</td>";
echo "<td class='req' align='center'>".$row['P1']."</td>";
// Delete button also in a td row, uses the variable "$numb" to set the button name
echo "<td class='req' align='center'><input type='submit' name='{$numb}' value='Delete'></td>";
echo "</tr>";
// If the delete button is pressed, delete the row (this query works, we tested it, but the button just doesn't set so it doesn't activate the SQL command)
if (isset($_POST[$numb])) {
$result2 = mysql_query("DELETE FROM Request WHERE ReqID = {$numb} LIMIT 1", $connection);
}
}
Use a hidden input to store the value:
somewhere above...
<form method="post" action="delete.php">
a little bit below...
echo "<td class='req' align='center'><input type='submit' name='submit' value='Delete'>";
echo '<input name="hello" type="hidden" value="$numb" /></td>';
...
$number = mysql_real_escape_string($_POST["hello"]);
$result2 = mysql_query("DELETE FROM Request WHERE ReqID = ".$number." LIMIT 1", $connection);
at the bottom:
</form>
Note:
Your approach isn't safe enough. I can easily edit the DOM and give another value to the hidden field (or another name for the button) causing to delete other elements from the database. Keep in mind this.