How to connect dynamic db value from one html element to another? - php

I had a hard time to formulate this question so it would be understandable, but here goes.
I have a page called apartments.php. On this page I list a number of rows from my database on apartments that you can show interest to rent.
The apartments are printed out like this:
<?php
foreach ($apartments as $apartment) { ?>
<tr>
<td><?php echo $apartment['apartments_room'] ?></td>
<td><?php echo $apartment['apartments_area'] ?></td>
<td><?php echo $apartment['apartments_rent'] ?></td>
<td><?php echo $apartment['apartments_address'] ?></td>
<td><?php echo $apartment['apartments_floor'] ?></td>
<td><?php echo $apartment['apartments_city'] ?></td>
<td><?php echo $apartment['apartments_freeFromDate'] ?></td>
<td>Apply!</td>
</tr>
<?php } ?>
Each apartment that gets printed out has its own button. When you press this button, a modal / popup will show.
And here's my problem.
When the modal pops up I want it to show the information of the apartment from witch I pressed the button.
So the data-reveal-id value from the button (that represent the specific apartment's db-id) is what I need to get into the modal divs id-value. Additionally I need to write out the address of that apartment (the db column 'apartments_address').
So this is how I need it to be:
<div id="HERE I NEED TO GET THAT ID" class="reveal-modal">
<h2>AND HERE I NEED TO PRINT OUT THE NAME OF THE ADDRESS</h2>
// Here i make room for an application form, but that's irrelevant...
</div>
This shouldn't be a hard thing, right? I hope you can help me out. Thanks!
(For more details if it matters, I'm using the modal of Foundation Zurb 4.)

First, your html isn't linking to a new page:
<a href="#" id="<?php echo $apartment['apartments_id'] ?>">
This should probably look something like this:
<a href="streetEdit.php?id=<?php echo $apartment['apartments_id'] ?>">
Then you can create a new php page named streetEdit.php
in that page, you can select the appropriate row from the database
$sql = "select * from myApartmentsTable where id = {$_GET['id']}";
See how the ID passed in the URL above shows up in $_GET?
next, do your query using the sql and output it on the page.
inside that you need an html form. Use a hidden input to keep track of the ID you are editing:
<form method="POST" action="updateStreet.php">
<input type="hidden" name="id" value="<?php echo $apartment['apartments_id'] ?>">
<input type="text" name="streetName" value="<?php echo $apartment['streetName'] ?>"
<input type="submit">
</form>
Then you need to write updateStreet.php. Since the form was POSTed you will use $_POST instead of $_GET
$id = $_POST['id'];
$streetName = $_POST['streetName'];
Note, you should use PDO for doing your sql queries
$sql = "update apartments set streetName = :name where id = :id";
$pdo = new PDO();
$stmt = $pdo->prepareStatement($sql);
$stmt->bindParam(':name', $streetName );
$stmt->bindParam(':id', $id);
http://php.net/manual/en/pdo.prepared-statements.php
This will prevent SQL injection attacks.. Ask further if you have more questions

Related

php search bar: how can I clear entries, when clearing search bar

This is my first contribution here:
So I am using PHP and MySQLi. I have a search bar and it works great,
but I want to entries to disappear when I clear the search.
Using the search bar is changing the URL:
without search: http://localhost:52694/index.php
with search: http://localhost:52694/index.php?search=test
If I clear my search bar, it is still the same URL (http://localhost:52694/index.php?search=test) and I cant click go because it tells me that the box can't be empty to start search.
I hope everything is clear and you understand my issue.
This is my search in php code:
<?php
if(isset($_GET['search'])){
$filtervalues = $_GET['search'];
$query = "SELECT * FROM table WHERE CONCAT( value, name, zla ) LIKE '%$filtervalues%' ";
$query_run = mysqli_query($con, $query);
if(mysqli_num_rows($query_run) > 0) {
foreach($query_run as $items) {
?>
<tr>
<td><?= $items['value']; ?></td>
<td><?= $items['name']; ?></td>
<td><?= $items['zla']; ?></td>
<td>
<a class='btn btn-danger btn-sm' href='/delete.php?id=<?php echo $items['id']; ?>'>Del</a>
</td>
</tr>
<?php
}
} else {
?>
<tr>
<td colspan="4">No Record Found</td>
</tr>
<?php
}
}
?>
From
I cant click go because it tells me that the box can't be empty to
start search.
I assume that in your HTML you have the required attribute present in the input field.
Here's an example using required :
<form action="index.php" method="GET">
Search for : <input name="search" required/>
<input type="submit" value="send"/>
</form>
<?php
if(isset($_GET["search"]))
echo $_GET["search"];
else
echo "nothing to search for";
using example like this: if your search field is empty, a little box will pop telling you to fill the field.
So please give brief view of your html search bar code , or check if required is present, if so .. delete it, and use it only for the fields that must be filled like in login or ...

Pass Two values when a Submit button is clicked -PHP

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.

can't delete from specific row from generate table from mySQL

Hye, i'm working on a small project of Inventory system. Everything is okay until this last part. I have included a delete button at the end of each row for user to delete any item of choice, but when user press the delete button, it deleted the last row of the table, instead of the row/item of choices, where is my mistake in my coding?
Thank you!
<?php
$result= mysql_query("SELECT * from Staff ORDER BY status");
$count=1;
while($row=mysql_fetch_array($result)){
?>
<tr>
<td align="left"><?php echo $count; ?></td>
<td align="left"><?php echo $row['staffId']; ?></td>
<td align="left"><?php echo $row['name']; ?></td>
<td align="left"><?php echo $row['address'];?></td>
<td align="left"><?php echo $row['pNum'];?></td>
<td align="left"><?php echo $row['status'];?></td>
<td align="left"><?php echo $row['type'];?></td>
<td><input type="submit" name="deleteStaff" value="Delete" onClick="displayMessage()">
<input type="hidden" name="staffId1" value="<?PHP echo $row['staffId']; ?>">
</td>
</tr>
</tbody>
<?php
$count++; }
if(isset($_POST['deleteStaff'])){
$id=$_POST['staffId1'];
$result=mysql_query("DELETE from Staff WHERE staffId='$id' ");
if($result){
echo '<script> location.replace("viewStaff.php"); </script>';
}
else{
?>
<script>
alert ("Fail to delete data")
window.location.href='viewStaff.php'
</script>
<?PHP
}
}
?>
I've encountered this error before as well. I think the error is happening since you're calling the same "onclick" function for each array result.
Instead, create a link to a page where you can run the php deletion script and pass the staffID into the URI and Get that in the php deletion script that you created.
example
<form action="deletion_script.php?staffid=<?php echo $staffid; ?>">
Put that inside of the td tag
Then in your deletion_script.php file, put something like this
<?php
//get staffid from URI
$staffid = $_GET['staffid'];
$sql="DELETE FROM $table_name WHERE staffid = '$staffid'"
$result = mysqli_query($connect, $sql);
if($result) {
//send back to the page you want
header("Location: ../inventory.php");
}
?>
This worked for me, and how I handle all situations like this from now on. You'll need to adjust the code a little to fit your set up.
since the name for the hidden field is the same for all rows being generated the post request is pulling the value of the last row( also "staffId1" ).
I recommend either using a individual form for each row in which case your code doesn't change as much or use js to get the required staffID using selectors
If you include the same hidden field (StaffId1) for every record it will contain more then one value.
You are now listening to the pressed SUBMIT so you don't need the hidden field. You can just get the value if the pressed SUBMIT.
Button
type="submit" name="staffId" value="<?php echo $id ?>"
Php
$id=$_POST['staffId'];

Passing Value of my Textbox named "title" to a query

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

how to save a javascript rendered countdown timer to mysql using PHP?

can someone please tell me how to save a javascript rendered countdown timer to mysql db using PHP ?
e.g
I have this button, that when someone clicks it, the countdown appears and starts counting.
so yeah, that's my problem, what's the best way to do in order to ensure that
- the exact date/time(in unix format) the button got clicked will be saved in db?
is this possible with PHP ?
// this is the PHP and HTML
<?php foreach($task->result() as $row): ?>
<tr>
<td><a title="<?php echo $row->descr; ?>"
href="#"><?php echo $row->title; ?></a>
</td>
<td><input type = "button"
id =<?php echo $row->title; ?>
value = "Unlocked"
onmouseover = "asktolock(this.id)"
onclick = "lockme(this.id)">
</td>
<td><?php echo $row->assign_to; ?></td>
<td><a id="txt"></a></td>
</tr>
Have your button populate a hidden formfield with a timestamp when it is clicked. Set the form to POST to a PHP script which processes the formfields and stores them where you like.
ETA: So on your form you'll need a field like this (with a unique name) for every timestamp you wish to store:
<input type='hidden' name='mytimestamp_unique' id='mytimestamp_unique' value=''/>
And then you'll add some code to your javascript function to set the value of the corresponding "mytimestamp" field. Something like:
function lockme(id){
//parse the id to get the unique part of the id which is also the unique part of mytimestamp_unique
var $hiddenfield=getElementById('mytimestamp_' + $uniquepart);
$hiddenfield.value=new Date().getTime();
//do other lockme stuff
}

Categories