This table is displaying details about my workload table per row. And when I click my "pre-View_attend" submit button I want to get the "WorkloadID" and "subjectID" of that workload row where the submit button was clicked.
Currently I have a hidden field containing the subject ID which is "subjectID_hidden", and when I try to get the "WorkloadID" and "subjectID" by using isset.
It seems that I can't accurately get the exact subject ID of the row where I've clicked the "pre-View_attend" submit button.
<tbody style="font-size:20px; text-align:center;">
<tr>
<td><?php echo $row["subjectName"]; ?></td>
<td><?php echo $row["className"]; ?></td>
<td><?php echo $row["RoomNumber"]; ?></td>
<td><?php echo $Sched_Days.'<br>'.$FST.' To '.$FET;?></td>
<td><button style="font-size:15px;" type="button" id="<?php echo $row["WorkloadID"]; ?>" class="btn btn-primary view_StudentList">View Student List</button></td>
<td><button style="font-size:15px;" type="submit" name="gettingAttendance" value="<?php echo $row["WorkloadID"]; ?>" class="btn btn-primary">Take Attendance</button></td>
<td><button style="font-size:15px;" type="submit" name="pre-View_attend" value="<?php echo $row["WorkloadID"]; ?>" class="btn btn-primary">View Attendance</button></td>
<input type="hidden" name="subjectID_hidden" value="<?php echo $row["subjectID"]; ?>">
</tr>
<?php } mysqli_close($connect);?>
</tbody>
/*
I'm getting the workload ID accurately, but the subject ID is incorrect.
I believe I'm getting the last subject ID that my query produced
*/
<?php
if(isset($_POST['pre-View_attend']))
{ $FWID=$_POST['pre-View_attend'];
$FSJID=$_POST['subjectID_hidden'];
echo"Workload ID: $FWID SubjectID: $FSJID";
}
?>
You get the last value because you have a bunch of hidden fields with the same name.
You could name them subjectID_hidden[<?=$row['WorkloadID'];?>] to get an array to then get the subjectID by WorkloadID:
$subjectID = $_POST["subjectID_hidden"][$_POST["pre-View_attend"]];
You need to query the table for something unique to the row you want to retrieve information from. This unqiue data should already be existing on the page, encoded into the button/form.
Example;
<?php
if(isset($_POST['pre-View_attend'])) {
global $db;
$select = "select * from mytable where unique='$unique'";
$connect = mysqli_query($db, $select);
while($row=mysqli_fetch_array($connect)) {
$myfirstVariable = $row['myfirstVariable'];
$mysecondVariable = $row['mysecondVariable'];
$show = "$myfirstVariable $mysecondVariable";
}
}
?>
Then you can call $show when you want in the HTML.
p.s. there are quite a few errors in your html like;
<input type="hidden" name="subjectID_hidden" value="<?php echo $row["subjectID"]; ?>">
should be
<input type="hidden" name="subjectID_hidden" value="<?php echo $row["subjectID"]; ?>" />
self closing at the end with forwards slash.
Related
My code is running a cycle that adds textareas where the user can input queries, which the div it is in also contains a dropdown with a list of servers available to run those queries in. At first it was working just fine for only the first iteration and not showing the list at all in the next dropdowns, but while I was messing with the code and pasted the query inside the cycle all dropdowns were filled but the server ID of the first textarea stopped being posted correctly, instead sending the value that's currently in the database. By other words it won't update properly.
<?php
if(mysqli_num_rows($result_query) > 0){
while($rowq = mysqli_fetch_assoc($result_query)){
$sql_servers = "SELECT id, name, address FROM servers ORDER BY id ASC";
$result_servers = mysqli_query($link, $sql_servers);
?>
<table>
<form name="formStep" method="post" action="">
<br>
<tr>
<textarea class="form-control scrollabletextbox" id="query<?php echo $rowq['step']?>" name="query<?php echo $rowq['step']?>"><?php echo $rowq['query'];?></textarea>
</tr>
<tr>
<td width="25%" style="vertical-align:middle;"><select id="server" name="server" class="form-control input-md">
<?php
if (mysqli_num_rows($result_servers) > 0) {
while($rows = mysqli_fetch_assoc($result_servers)){ ?>
<option value="<?php echo $rows["id"];?>" <?php if($rows['id']==$row_query2['id_server']) echo 'selected=\"selected\"' ?> ><?php echo $rows["name"];?></option>
<?php
}
}?>
</select>
</td>
<td style="padding:10px;"><input type="submit" name="submit" formaction="save.php?i=4&id=<?php echo $id; ?>&s=<?php echo $rowq['step'];?>" class="btn btn-block btn-primary" value="<?php echo $lableSave; ?>"></td>
</tr>
<?php
}
}?>
</form>
</table>
If I echo the server variable in the save.php it will not update for the new selected value in the dropdown, but it will work for all the subsequent iterations of the cycle.
Any way to solve this problem or the previous one before i started trying to hammer the code until it worked would be greatly appreciated.
I am trying to teach myself PHP. I want to know how to interact with a database and insert data from a website and to display the data back into a table on the webpage.
I am trying to let the user edit the results or have them shown in an input box. This way, the user can edit the data and send it back to the database. But, I am stuck. I have tried different ways and none of them seem to work. I'm opened to suggestions.
The bookingid is what the user picks.
I know all table names are not that good but I am learning.
$query = "SELECT * FROM Trip_Booked Where bookingId = $bookingid";
//executes the query
$result = mysqli_query($db,$query);
// create table and display top row
echo "<td>".$row['email']."</td>";
echo "<div align='center'>";
echo "<table cellpadding=2 border=1>";
echo "<tr>";
echo "<td><strong>booking ID</strong></td>";
echo "<td><strong>boatdate</strong></td>";
echo "<td><strong>fromdate</strong></td>";
echo "<td><strong>saleto</strong></td>";
echo "<td><strong>salefrom</strong></td>";
echo "<td><strong>NoOfAdults</strong></td>";
echo "<td><strong>NoOfWheelchair</strong></td>";
echo "<td><strong>NoUnder2</strong></td>";
echo "<td><strong>NoChild3–10</strong></td>";
echo "<td><strong>NoChild11–16</strong></td>";
echo "<td><strong>TotalPassgers</strong></td>";
echo "</tr>";
// print each record one after another
while($row = mysqli_fetch_assoc($result))
{
echo "<tr>";
echo "<td>".$row['bookingId']."</td>";
echo "<td>".$row['boatDate']."</td>";
echo "<td>".$row['fromDate']."</td>";
echo "<td>".$row['saleto']."</td>";
echo "<td>".$row['salefrom']."</td>";
echo "<td>".$row['NoOfAdults']."</td>";
echo "<td>".$row['NoOfWheelchair']."</td>";
echo "<td>".$row['NoUnder2']."</td>";
echo "<td>".$row['NoChild3–10']."</td>";
echo "<td>".$row['NoChild11–16']."</td>";
echo "<td>".$row['TotalPassgers']."</td>";
echo "</tr>";
}
echo "</table>";
echo "</div>";
?>
I would like the result be shown in an input box for the user to edit as it will go in a table and check it is pulling the right information from the database.
A very simplified example; double check mysqli usage because I don't use mysqli...
Things to take note of:
Use prepared statements. period.
A common practice is to do all your script stuff, then output HTML. Don't mix logic with presentation.
Pay attention to whether a variable is GET or POST. The reason I used both GET and POST is because I use GET to display a specific record; but POST is used for anything that changes a record.
I use camelCase for variables but lowercase for #id. Just be aware of it, that can be confusing if you're not aware.
I first check if this is a request to update the record. If it is, do the update, then redirect back to the same page in order to prevent resubmissions. (This is only possible if no output has been sent. See point 2)
Using the <?= ?> format is personal preference; I like it because it keeps the HTML code cleaner and makes the PHP as unobtrusive as possible.
Using <form method='{GET | POST}'> with no action simply sends back to the same URL.
// Create connection
$db = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($db->connect_error) {
die("Connection failed: " . $db->connect_error);
}
// first, take action on POST results if this is an update
if($_POST['action']=='update') {
// use prepared query to avoid SQL injection
$query = "UPDATE Trip_Booked SET boatDate=?, fromDate=?, saleto=?, salefrom=?, NoOfAdults=?, NoOfWheelchair=?, NoUnder2=?, NoChild3_10=?, NoChild11_16=?, TotalPassgers=? WHERE bookingId=?";
$result = $db->prepare($query);
$result->bind_param("ssssiiiiiii", $_POST['boatDate'],$_POST_['fromDate'],$_POST_['saleto'],$_POST_['salefrom'],$_POST_['NoOfAdults'],$_POST_['NoOfWheelchair'],$_POST_['NoUnder2'],$_POST_['NoChild3_10'],$_POST_['NoChild11_16'],$_POST_['TotalPassgers'],$_POST['bookingId']);
$result->execute();
// since we don't want post data resent on back button press, redirect back to this page (change url to match)
header("Location: http://www.example.com/?bookingId=" . $POST['bookingId']);
exit;
}
// if not an update, but we do have the booking ID, find the row using BookingId
// use prepared query to avoid SQL injection
if($_GET['bookingId']) {
$query = "SELECT * FROM Trip_Booked Where bookingId = ?";
$result = $db->prepare($query);
$result->bind_param("s", $_GET['bookingId']);
$result->execute();
}
Now, we have everything we need to output the HTML. Remember, PHP is a templating language, so don't be afraid to use it as such.
<!-- language: lang-html -->
// continuing from above
?><html>
<head>
<title>Edit a Booking</title>
</head>
<body>
<h1>Choose a Page</h1>
<form method="get">
<label for="bookingid">Page</label><input type="text" id="bookingid" name="bookingId" />
<input type="submit" value="Go to page" />
</form>
<?php if($_GET['bookingId']): ?>
<!-- better way to enter values: -->
<!-- <div><label for="fieldname">Label</label><input type="text" id="fieldname" name="fieldname" value="{value}" /></div> -->
<h1>Edit Fields</h1>
<form method="post">
<input type="hidden" name="action" value="update" />
<input type="hidden" name="bookingId" value="<?= htmlentities($_GET['bookingId']) ?>" />
<table>
<thead>
<tr>
<th>bookingId</th>
<th>boatDate</th>
<th>fromDate</th>
<th>saleto</th>
<th>salefrom</th>
<th>NoOfAdults</th>
<th>NoOfWheelchair</th>
<th>NoUnder2</th>
<th>NoChild3_10</th>
<th>NoChild11_16</th>
<th>TotalPassgers</th>
</tr>
</thead>
<tbody>
<!-- since this is an edit of only one record, you could use 'if' instead of 'while' -->
<?php while($row = $result->fetch_object())): ?>
<tr>
<th><input type="text" name="bookingId" value="<?= $row->bookingId ?>" /></th>
<th><input type="text" name="boatDate" value="<?= $row->boatDate ?>" /></th>
<th><input type="text" name="fromDate" value="<?= $row->fromDate ?>" /></th>
<th><input type="text" name="saleto" value="<?= $row->saleto ?>" /></th>
<th><input type="text" name="salefrom" value="<?= $row->salefrom ?>" /></th>
<th><input type="text" name="NoOfAdults" value="<?= $row->NoOfAdults ?>" /></th>
<th><input type="text" name="NoOfWheelchair" value="<?= $row->NoOfWheelchair ?>" /></th>
<th><input type="text" name="NoUnder2" value="<?= $row->NoUnder2 ?>" /></th>
<th><input type="text" name="NoChild3_10" value="<?= $row->NoChild3_10 ?>" /></th>
<th><input type="text" name="NoChild11_16" value="<?= $row->NoChild11_16 ?>" /></th>
<th><input type="text" name="TotalPassgers" value="<?= $row->TotalPassgers ?>" /></th>
</tr>
<?php endwhile; ?>
</tbody>
</table>
<input type="submit" value="Edit" />
</form>
<?php endif; ?>
</body>
</html>
You need to output the data into the input fields you want. Small example:
<input type="hidden" name="bookingId" value="<?php echo $row["bookingId"];?>" />
<input type="text" name="NoOfAdults" value="<?php echo $row["NoOfAdults"];?>" />
Using a form action of POST you can then run the UPDATE query using the $_POST['NoOfAdults'] and using the $_POST['bookingId'] as the WHERE
I am currently new in PHP can you please guide me on how can I get the value of my textbox? and pass it to my sql query? where I want to become the value of my Column name Page? Please Guide me.
<?php
$sql_query="SELECT * FROM tblpost WHERE Page = '//what to put here?' ";
$result_set=mysql_query($sql_query);
while($row=mysql_fetch_row($result_set)){
?>
<tr>
<td><?php echo $row[1]; ?></td>
<td><?php echo $row[3]; ?></td>
<td><?php echo $row[6]; ?></td>
<td>
<a class="btn btn-primary" href="javascript:edt_id('<?php echo $row[0]; ?>')">Edit</a>
<a class="btn btn-danger" href="javascript:delete_id('<?php echo $row[0]; ?>')">Delete</a>
</td>
</tr>
<?php } ?>
this is my textbox.
<div class="form-group">
<label>Page Title</label>
<input name="title" type="text" value="<?php echo $fetched_row['Title']; ?>" required class="form-control" placeholder="Page Title">
</div>
You may want to search for PHP tutorials in addition to this answer.
First, you'll have to wrap your input field in some sort of form. The form will have to have a submit button that leads back to your page, using method GET.
Second, you can get parameters from a GET request in php with $_GET['name-of-input-field']. name-of-input-field is the value of the name attribute of the input field.
Then, you'll want to create a "PreparedStatement", into which you can bind this get parameter. This is a guide on PreparedStatements in PHP: http://php.net/manual/en/pdo.prepared-statements.php
Okay so I'm a bit lost here as I'm very new to AJAX and fairly new to PHP. So I have an inventory management system build with phpmyadmin and wamp. The registration/ log in system works great. so when the user logs in a table of all inventory is generated on screen, along with a button next to each inventory item that allows the current user to check in or check out an item. the buttons are all generated and I gave them a unique id using php and the code looks like this.
<table class = "table">
<thead>
<tr>
<td><center><strong>Item Id</strong></center> </td>
<td><center><strong>Item Description</strong></center> </td>
<td><center><strong>Item Type</strong> </center></td>
<td><center><strong>Availability</strong></center></td>
<td><center><strong>Name</strong></center></td>
<td><center><strong>Check in/out button</strong></center></td>
</tr>
</thead>
<tbody>
<?php
mysql_select_db('inventory management system');
$query="SELECT * FROM inventory";
$results = mysql_query($query);
$a = 0;
while($row = mysql_fetch_array($results)) {
$a++;
echo '<script type = "text/javascript">c++;</script>'
?>
<tr>
<td><center><?php echo $row['item_id']?></center></td>
<td><center><?php echo $row['item_desc']?></center></td>
<td><center><?php echo $row['item_type']?></center></td>
<td><center><?php echo $row['availability_status']?></center></td>
<td><center><?php echo $row['name']?></center></td>
<td><center><?php
if(is_null($row['name'])){
//this is what is generating each button with a unique id echo '<input class="btn btn-default" type="submit" value="Check Out" id = "<?php echo $a; ?>" onclick="<?php ?>">';
}else if(!is_null($row['name'])){
echo '<input class="btn btn-default" type="submit" value="Check In" id = "<?php echo $a; ?>" onclick="updateinventory()">';
}
?></center></td>
</tr>
<?php
}
?>
</tbody>
</table>
so now when the user clicks a button, it updates the database to set the name field to null signifying that nobody has the item checked out, that way another user can sign in and check out an item in the inventory. I'm really struggling and have tried everything from on click methods (which for some reason I couldn't get to work) to plenty of things with ajax and php.. and I cant get a seemingly simple feature to work... Thank you for any possible help.
You have already opened php tags so change
echo '<input class="btn btn-default" type="submit" value="Check In" id =
"<?php echo $a; ?>" onclick="updateinventory()">';
to
echo "<input class='btn btn-default' type='submit' value='Check In'
id ='$a' onclick='updateinventory()'>";
Similarly remove php tags from IF since php starting tags have been written above.
type="submit" this will directly submit the page, and you probably need to make it button so that you may call javascript function that will eventually call ajax.
I have a parent page which has a drop down list in it. using the onchange event, data is posted to a second page using $.post(). This page uses the posted variables to output mysql data with a checkbox for each line. This page output is then inserted into the parent page using jquery $('#DIV name').html(output).show();
The user can then see the mysql table rows with corresponding checkboxes. They then select the checkboxes they want and say delete. Delete is a form submit button.
My question is, when they click delete how do I take the form data from the second page and post it with that of the parent page so that I can then use $_POST[] to get the checkbox info and delete the selected table rows?
example of the parent page code is:
javascript/jquery
<script type="text/javascript">
function get(row){ //row being processed, defined in onchange="get(x)"
('getpeopleinjobs.php',{ //data posted to external page
postvarposition: form["position"+row].value, //variable equal to input box, sent to external page
postvarjob: form["job"+row].value, //variable equal to input box, sent to external page
postvarperson: form["person"+row].value, //variable equal to drop down list, sent to external page
postrow: row}, //variable equal row being processed, sent to external page
function(output){
$('#training'+row).html(output).show(); //display external results in row div
//popupWindow = window.open('t4.php?variable1Name=Vicki&variable2Name=Maulline','popUpWindow','height=400,width=1000,left=10,top=10,resizable=yes,scrollbars=yes,toolbar=no,menubar=no,location=no,directories=no,status=yes')
});
}
</script>
Form data is
<tr>
<td>
<?PHP echo $i; ?>
</td>
<td>
<input type=text NAME="position<?PHP echo $i; ?>" id="position<?PHP echo $i; ?>" style="border: 1px solid #2608c3;color:red; width=200px" value="<? echo mysql_result($resultpositionjob,$r,0);?>">
</td>
<td>
<input type=text NAME="job<?PHP echo $i; ?>" id="job<?PHP echo $i; ?>" style="border: 1px solid #2608c3;color:red; width=200px" value="<? echo mysql_result($resultpositionjob,$r,1);?>">
</td>
<td>
<SELECT NAME="person<?PHP echo $i; ?>" id="person<?PHP echo $i; ?>" style="border: 1px solid #2608c3;color:red; width=200px" onchange="get(<? echo $i; ?>);">
<OPTION VALUE=0 >
<?=$optionpeople?>
</SELECT>
</td>
<td onclick="train(<? echo $i; ?>);" style="color:grey; cursor: pointer;">
<div id="training<?PHP echo $i; ?>"><font color=grey size=2></div>
</td>
<td>
</td>
</tr>
<?PHP
$i++;
$r++;
}
?>
The second page or page called by jquery, the output is:
echo
"
<table border=0 width=400>
<tr>
<td width=20>
</td>
<td width=150>
<b>Position<b>
</td>
<td>
<b>Job<b>
</td>
</tr>
";
while($line = mysql_fetch_array($result))
{
echo "
<tr>
<td>
";
?>
<input type=checkbox name="delete[]" id="delete[]" value="<?php echo $rows['id']; ?>">
<?PHP
echo "
</td>
<td>
";
echo $line['position'];
echo "
</td>
<td>
";
echo $line['job'];
echo "
</td>
</tr>
";
}
?>
<tr>
<td>
<input type=submit name="update" id="update" value="Update">
</td>
</tr>
</table>
<?PHP
}
To repeat I want the table checkbox element from the second page posted with the form data of the parent page.
Is this possible as my testing hasnt given me any luck.
Am I doing something wrong or do I need to modify the jquery code?
Thanks as always for the assistance.
There is no second page. Really - you're loading HTML content from the second page, but it's being inserted into the parent page. All content, from your browsers perspective, is in the same page. The fields should be included as long as they're inside the DOM inside the element. Use the developer tools for your browser or Firebug for Firefox to make sure that the content is placed within the form element in the DOM. The developer tools should also be able to show you exactly which variables are submitted to the server when the form is submitted.
The 'action' parameter of 'form' will indicate where the content gets submitted (and it seems you've left that part out of your HTML, so it's impossible to say if you've left out or just not included it in the paste.