I need a web page where users can modify a data already captured in database. So I made a tab with the list of every data and when a user click on one, I want to redirect the user in a page with matching data.
So this is my tab :
<?php
$conn_string = "host=localhost port=5432 dbname=test_postgre user=postgres password='1234'";
$dbconn = pg_connect($conn_string);
$sql = "SELECT id_essai, nom_local_essai, thematique FROM public.essai";
$res = pg_query($sql) or die("Pb avec la requete: $sql");
echo'<table class="table table-hover" border="1">';
echo'<thead><tr><th>id_essai</th><th>Nom local</th><th>Thématique</th></tr></thead>';
while ($data = pg_fetch_array($res))
{
echo' <tbody>
<tr>
<td><a id="'.$data['id_essai'].'">
'.$data['id_essai'].'
</a></td>
<td>
'.$data['nom_local_essai'].'
</td>
<td>
'.$data['thematique'].'
</td>
</tr>
</tbody>
';
}
echo'</table>';
?>
And I get the ID of the data selected like this :
<script>
$(document).ready(function(){
$('a').click(function(){
var id_essai = $(this).attr('id');
$.post('traitement_modif_essai.php',{id_essai:id_essai});
});
});
</script>
Now, on the other file, I want to get the ID and display the right page, but I can't get thought.
<?php
session_start();
$_SESSION['id_essai'] = $_POST['id_essai'];
echo $_SESSION['id_essai'];
header('Location:essai_modif.php');
?>
You can redirect the user after the session has been set by you in the PHP in the javascript callback of post
<script>
$(document).ready(function(){
$('a').click(function(){
var id_essai = $(this).attr('id');
$.post('traitement_modif_essai.php',{id_essai:id_essai}, function(){
window.location = "./essai_modif.php";
});
});
});
</script>
and in the PHP
<?php
session_start();
$_SESSION['id_essai'] = $_POST['id_essai'];
echo $_SESSION['id_essai'];
?>
Related
I've a link in admin-dashboard page which on click shows "doctor-details". Now I want the admin must be able to click on the options link in the table and see full details of the doctor in the same page(I'll probably use modals for this).
So my question is how do I get the ID from the table and send it to another php file for database query?
my code for generating details about doctor(doctor-details.php)
<?php
require('config.php');
$sql = "SELECT * FROM doctor";
$result = mysqli_query($conn, $sql);
$count = mysqli_num_rows($result);
if($count > 0){
while($rows = mysqli_fetch_array($result)){
?>
<tr>
<td><?php echo $rows['id'];?> </td>
<td><?php echo $rows['f_name'];?></td>
<td><?php echo $rows['l_name'];?></td>
<td><?php echo $rows['email'];?></td>
<td><?php echo $rows['contact_number'];?></td>
<td><?php echo $rows['gender'];?></td>
<td> Options</td>
</tr>
<?php
}
}
?>
and finally my ajax:
$(document).ready(function(){
$("#load-doctor-data").click(function(){
$.ajax({
url: 'views/admin/doctor-details.php',
type: 'POST',
success: function(result){
$("#response-doctor").html(result);
}
});
});
});
//Hide table on login
$("#show-doctor-details").hide();
$(document).ready(function(){
$("#load-doctor-data").click(function(){
$("#show-doctor-details").show();
$("#show-patient-details").hide();
});
});
So, the gist is I want to click on options and show full details of John Doe.
Data attributes are a pretty easy to pass data. So when first appending make sure you have the id in the options field like this:
<td> Options</td>
Now you can get this id in your click event handler like this:
$(document).on('click','.options', function(){
var currentId = $(this).data('id');
...
});
Also you don't need two document readys. You can wrap both event handlers in one document ready.
I will describde the problem as straight as I can, so:
I've got website, on which I have button, by clicking which I want to run AJAX, which would run .php code which will update database records.
Right now, with code below Im not getting any errors, and also database is at the same state as it was before clicking button. What's more I guess that ajax doesnt works properly, as the website refreshes as I click update button.
Any 1? Anything?
Here goes main php (updated):
<form role='form' method='post' action='' autocomplete='off'>
<input type='submit' id='addBidSlow' name='addBidSlow' value='Slow bid' class='btn btn-inline-block btn-success auctionDetails' tabindex='1'>
</form>
</td>
</tr>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
$("#addBidSlow").click(function(){
var bidAuthor = "<?php echo $_SESSION['username']; ?>";
var itemID = <?php echo $_GET['id']; ?>;
$.post(
"addBidSlow.php",
{bidAuthor: bidAuthor, itemID: itemID},
false
);
});
</script>
And updater php:
<?php
require('includes/config.php');
$bid = 1000;
$bidAuthorPoints = "";
$finalPoints = "";
$finalDate = "";
$todayDate = date("Y-m-d H:i:s");
$stmt = $db->prepare("SELECT itemID, endTime FROM items WHERE itemID = :itemID");
$stmt->execute(array(':itemID' => $itemID));
while($row = $stmt->fetch(PDO::FETCH_OBJ)) {
$finalDate = $row->endTime;
}
$addedTime = strtotime($finalDate) + 60*10;
$finalDate2DB = date('Y-m-d H:i:s' , $addedTime);
$stmt = $db->prepare("UPDATE items SET endTime = :finalDate2DB WHERE itemID = :itemID");
$stmt->execute(array(
':itemID' => $itemID,
':finalDate2DB' => $finalDate2DB
));
?>
I'm facing major issue in changing status of a particular row in table with <select> tag .
I'm using jquery-Ajax so, here go functionality by this when is click on <select on any particular row for Example: Lets say i have changed the status of table row of id 2 from Pending to Delivered. On this jquery change() event triggers and it fetchs the value from 'tag' and sends the value to other file through ajax. Till here everything goes right the data goes through ajax and row with particular id's status is getting updated successfully.
But on Front end it does not change the status of the particular row. but it changes status of only first row .
Here i need it to change status of the particular row.
Note : I have searched in stackoverflow for this question. but those answere not come close to my question. Here are those links below Link 1link2link3
THANK YOU IN ADVANCE
Here is the Output and i have explained details in images also
There is the full code
HTML page : index.php
<?php
include('processing.php');
$newobj = new processing();
?>
<html>
<head>
<title>Jquery Ajax select <tag> with PHP Mysql</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>
<body>
<table border="1">
<tr>
<th>Id</th>
<th>Product name</th>
<th>Status</th>
<th>Action</th>
</tr>
<?php echo $newobj->display();?>
</table>
<script>
$(document).ready(function(){
$(".selectstatus").change(function(){
var statusname = $(this).val();
var getid = $(this).attr("status-id");
//alert(displid);
$.ajax({
type:'POST',
url:'ajaxreceiver.php',
data:{statusname:statusname,getid:getid},
success:function(result){
$("#display").html(result);
}
});
});
});
</script>
</body>
</html>
AJAX HANDLER PAGE : ajaxreceiver.php
<?php
include('processing.php');
$newobj = new processing();
if(isset($_POST['statusname'],$_POST['getid'])){
$statusid = $_POST['statusname'];
$id = $_POST['getid'];
$newobj->getdata($statusid,$id);
}
?>
PHP CLASSES FILE : processing.php
<?php
class processing{
private $link;
function __construct(){
$this->link= new mysqli('localhost','root','','example');
if(mysqli_connect_errno()){
die ("connection failed".mysqli_connect_errno());
}
}
function display(){
$sql = $this->link->stmt_init();
$id=1;
if($sql->prepare("SELECT id,productname,status FROM ajaxselect")){
$sql->bind_result($id,$productname,$status);
if($sql->execute()){
while($sql->fetch()){
?>
<tr>
<td><?php echo $id;?></td>
<td><?php echo $productname;?></td>
<td><p id="display"><?php echo $status;?></p></td>
<td>
<select status-id=<?php echo $id;?> id="selectstatus" class="selectstatus">
<option>Pending</option>
<option>Delivered</option>
<option>Cancelled</option>
<option>Amount Paid</option>
</select>
</td>
</tr>
<?php
}
}
}
}
function getdata($statusid,$id){
$sql = $this->link->stmt_init();
if($sql->prepare("UPDATE ajaxselect SET status=? WHERE id=?")){
$sql->bind_param('si',$statusid,$id);
if($sql->execute()){
echo $statusid;
}
else
{
echo "Update Failed";
}
}
}
}
?>
the problem is in this line :
$("#display").html(result);
so what's going here ?
you are creating display id for every row, which is not good practice to do , specially in your particular .
there are various solutions for this problem ,
1)
("#display", $(this).parent().parent()).html(result);
here you are going to apply the action to particular ID which is belongs to the parent of the parent of the particular class which had received the change action
2)
giving the display row unique id for each row
for example like follows :-
<td><p id="display_<?php echo $id; ?>"><?php echo $status;?></p></td>
and then apply your action to it directly ,
$("#display_" + getid).html(result);
3)
and this solution is similar to the past one , by giving the parent <tr> a unique id
for example :-
<tr id='parent_<?php echo $id; ?>'>
and then apply your action it from your ajax like this
$("#display", $('#parent_' + getid)).html(result);
or even :
$('#parent_' + getid + ' #display').html(result);
Yes there is problem in $("#display").html(result); line because id ($("#display")) always select first element found in the DOM you can fix it by - following code-
<script>
$(document).ready(function(){
$(".selectstatus").change(function(){
// make jquery object that make reference to select in which click event is clicked
$this = $(this);
var statusname = $(this).val();
var getid = $(this).attr("status-id");
//alert(displid);
$.ajax({
type:'POST',
url:'ajaxreceiver.php',
data:{statusname:statusname,getid:getid},
success:function(result){
// this refer to the ajax callback function so we have to use $this which is initialized before ajax call
$($this).parents('tr').find("#display").html(result);
}
});
});
});
</script>
I'm attempting to create a shipping status page and I want a really basic feature to work. I want to be able to press a button on this page that says "Mark Shipped". Then I want the button's text to change to "Shipped". I then want the option to change the status of that back to "Mark Shipped', but have an alert prevent it from doing it until you click Proceed or something like that.
I am attempting to do this with php and ajax. I've never used Ajax before or too much JS, so I'm not too sure on how to use the two simultaneously.
I have created a database table that will house the status of the 'shipped' status, so whenever I click the 'mark as shipped' button the word 'shipped' will go into my db table with the id of the order and then I want the word shipped to echo back into that button and remain there indefinitely. The php query was working great until I changed the action of the Ajax script.
So this is my table...
if( $result ){
while($row = mysqli_fetch_assoc($result)) :
?>
<form method="POST" action="shippingStatus.php">
<tr>
<td class="tdproduct"><?php echo $row['order_id']; ?> </td>
<td class="tdproduct"><?php echo $row['date_ordered']; ?> </td>
<td class="tdproduct"><?php echo $row['customer_name']; ?> </td>
<td class="tdproduct"><?php echo $row['streetline1'] . "<br />" . $row['streetline2'] . "<br />" . $row['city'] . ", " . $row['state'] . " " . $row['zipcode']; ?> </td>
<td class="tdproduct"><?php echo $row['product_name']; ?> </td>
<td class="tdproduct"><button data-text-swap="Shipped">Mark Shipped</button></td>
<input type="hidden" name="product_id" value="<? echo $row['id']; ?>"/>
<td class="tdproduct"><input name="delete" type="submit" value="DELETE "/></td>
</tr>
</form>
<?php
endwhile;
}
?>
</table>
This is my Ajax script. At first I had 'shipped' as the action, but it wasn't saving the status. When I would reload the page it would go back to 'Mark Shipped'.
<script>
$("button").on("click", function(e) {
e.preventDefault()
var el = $(this);
$.ajax({
url: "shippingStatusSend.php",
data: {action: "<?php echo $shipped; ?>", order: order_id},
type: "POST",
dataType: "text"
}).fail(function(e,t,m){
console.log(e,t,m);
}).done(function(r){
//Do your code for changing the button here.
//Getting Shipping Status button to chance from 'mark as shipped' to 'shipped'
el.text() == el.data("text-swap")
? el.text(el.data("text-original"))
: el.text(el.data("text-swap"));
});
});
</script>
My php in a page called shippingStatusSend:
<?php
//connection to db
$con = mysqli_connect("localhost", "root", "", "bfb");
//Check for errors
if (mysqli_connect_errno()) {
printf ("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$order_id = trim($_POST['order_id'] );
$status = trim($_POST['action'] );
$shipped = "Shipped";
$markshipped = "Mark Shipped";
/* create a prepared statement */
if ($stmt = mysqli_prepare($con, "INSERT INTO shippingStatus (order_id, status, date_Shipped) VALUES (?, ?, NOW())")) {
/* bind parameters for markers */
$stmt->bind_param('is', $order_id, $status);
/* execute query */
$stmt->execute();
echo $shipped;
/* close statement */
mysqli_stmt_close($stmt);
}
while($row = mysqli_fetch_assoc($stmt)){
$shipped = $row['status'];
$markshipped = "Mark Shipped";
}
else
echo $markshipped;
?>
I am not sure what I am doing wrong, could anyone point me in the direction of what is wrong? Is it my php code or the way I'm attempting to do this with Ajax or both. Which area of my code is wrong?
In my case, I make auto refresh after X seconds. But I want to make it only refresh when new data inserted or updated.
Here is my script.
refresh.php
<script src="jquery-1.4.js"></script>
<script>
$(document).ready(function() {
$("#responsecontainer").load("refreshnow.php");
var refreshId = setInterval(function() {
$("#responsecontainer").load('refreshnow.php?randval='+ Math.random());
}, 1000);
});
</script>
<div id="responsecontainer"></div>
refreshnow.php
<?php
$query = mysqli_query($connection,"select * from `table`");
$rows = mysqli_num_rows($query);
?>
<table style="background:#F0F0F0;" border=1 style="border-collapse:collapse">
<tr>
<th>Name</th>
<th>Job</th>
</tr>
<tbody>
<?php
while($result = mysqli_fetch_array($query)){
echo "<tr>";
echo "<td>".$result["name"]."</td>";
echo "<td>".$result["job"]."</td>";
echo "</tr>";
}
?>
</tbody>
So, could it be possible to load new data automatically when new data inserted or updated without refresh after x seconds?
If you want to use your existing code, just add another column in a mysql table (if you got a config or settings table, this would be easyer, if not just ad timestamp for each record you insert) with the value time(), this updates the timestamp in the database each time a row is inserted/modified.
Then just do a query like:
"select timestamp_column from `settings_table` order by timestamp_column limimt 1"
If ($result["timestamp_column"]+1000>=time()){
//do the refreshnow.php code here
}
Hope it helps,
Cheers