updating sql with php - php

I need to be able to update my sql table from open to close task.
I already wrote code for this but it is not working properly. On the modal it shows 2 'continue' buttons and right now the task table contains two tasks. Can somebody help me?
This is what the modal looks like:
and this is my code:
<?php
if (isset($_POST["closetask"])) {
$close_task_id = mysqli_real_escape_string($con, $_POST["close_task_id"]);
$sql = "UPDATE task SET task_status='closed' WHERE id_task='$close_task_id'";
$result = mysqli_query($con, $sql);
if ($result) {
$_SESSION['success'] = "Task closed";
$_SESSION['text'] = "Task has been closed successfully";
$_SESSION['icon'] = "success";
} else {
$_SESSION['success'] = "Error";
$_SESSION['text'] = "Unkown error, please try again";
$_SESSION['icon'] = "error";
}
}
?>
<?php
$query = "SELECT * FROM task ORDER BY id_task DESC";
$result = mysqli_query($con, $query);
?>
<!-- reject button -->
<form action="task-view.php" method="post" enctype="multipart/form-data">
<div id="rejectModal" class="modal fade" role="dialog" >
<div class="modal-dialog modal-lg">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">Close this task?</h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<div class="modal-body">
<div class="d-flex align-items-center justify-content-center form-group" >
<p class="align-items-center text-center" for="company">This button means closing the task. Are you sure you want to continue?</p>
</div>
</div>
<div class="modal-footer">
<?php
while ($row = mysqli_fetch_array($result)) {
if ($row['task_status'] == 'open'){
$check_task = '
<input type="text" name="close_task_id" class="form-control" value="' . $row["id_task"] . '" hidden>
<button class="btn btn-success" type="submit" name="closetask" class="btn btn-success">Continue</button>
</button>
';
}
echo '
<br>
' . $check_task . '
';
}
?>
</div>
</div>
</div>
</div>
</div>
</form>
<!-- end of reject button-->
<?php
if (isset($_SESSION['success']) && $_SESSION['success'] != '') {
?>
<script>
swal({
title: "<?php echo $_SESSION['success']; ?>",
text: "<?php echo $_SESSION['text']; ?>",
icon: "<?php echo $_SESSION['icon']; ?>",
button: "OK",
});
</script>
<?php
unset($_SESSION['success']);
}
?>

Well, after going through your code snippet,
I could see this query
$query = "SELECT * FROM task ORDER BY id_task DESC";
$result = mysqli_query($con, $query);
meaning this fetches all records in the task table, the two continue buttons you see are coming from the while loop
while ($row = mysqli_fetch_array($result)) {
....
}
it logically implies you have two records or rows in your database task table and all the records where retrieved from the table.

Related

modals showing blank after ajax call

I am developing a page in php that has multiple dynamic modals for displaying pdf files in the page itself with respective title and author names. All the data comes from a database designed in MySQL. The trigger buttons are also meant to be dynamic. I have tried the following code. The problem is I am getting the modals blank. I am able to print the data in fetch-reader.php page itself. I think I am doing something wrong in AJAX or modal part. Please guide me through it.
<?php $sqlGal = "select * from articles where year = '2019'";
$queryGal = $con->query($sqlGal);
if ($queryGal->num_rows > 0) {
while ($row = $queryGal->fetch_array()) { ?>
<div class="col-md-3 gallery-grid">
<h3 style="height: 200px; vertical-align: bottom; margin: 0; display: table-cell;"><?php echo($row['title']) ?></h3>
by<h4><?php echo($row['name']) ?></h4>
<i class="fa fas fa-book-open"></i> Read
</div>
<?php }
}
?><div class="clearfix"></div>
<div class="modal fade" id="myReader" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h3 id="articleName"></h3>
<h4 id="name"></h4>
<h6 id="position"></h6>
<h6 id="address"></h6>
</div>
<div class="modal-body">
<object data="" id="reader" width="100%" height="450px"></object></div>
</div>
</div>
</div>
</div>
in <script> part
$(document).ready(function() {
$('#myReader').on('show.bs.modal', function(e) {
var rowid = $(e.relatedTarget).data('id');
$.ajax({
type: 'get',
url: 'include/fetch_reader.php',
data: 'rowid='+rowid,
dataType: 'json',
success : function(response) {
$('#articleName').text(response.title);
$('#name').text(response.name);
$('#position').text(response.position);
$('#address').text(response.address);
var add = "uploads/articles/" + response.yearSl + ".pdf";
$('#reader').prop('data', add);
}
});
});
});
in fetch-reader.php
<?php
require_once('connect.php');
if ($_GET['rowid']) {
$id = $_GET['rowid'];
$sql = "SELECT `title`, `name`, `position`, `address`, `yearSl` FROM articles WHERE `id` = '".$id."'";
$result = $con->query($sql);
if ($result->num_rows > 0) {
$row = $result->fetch_array();
}
$connect->close();
header('Content-Type: application/json');
echo json_encode($row);
}
Please guide me any other way which can serve my purpose, but I don't want to use any third-party tools for the purpose.
I think you are sending the wrong data in ajax. Try this into your "$ .ajax" call:
data: { 'rowid': rowid }
And try checking into browse console if the request shows any error. Into your ajax call you check only for success return, not for fail.

assign value to php variable using jquery [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 3 years ago.
i am working on website with buttons each button fire up a bootstrap modal contain data from mysql database i pass a variable from the jquery that fire up the model into a mysql query inside the modal the problam is the php variable cannot get the data send it from the jquery any hints please ?!
i am passing data through jquery post to ajax.php file
the modal code
<div class="modal" id="myModal">
<div class="modal-dialog">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<span id="codeElem"></span>
<?php
$resultHead = mysqli_query($con2,"SELECT * FROM coops WHERE Code = $getCode ");// WHERE Code IN ('".$codeArrayStr."')
?>
<?php
$i=0;
$row3 = mysqli_fetch_array($resultHead);
?>
<h4 class="modal-title"><?=$row3['CoopName'];?></h4>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- Modal body -->
<div class="modal-body">
<?php
$result = mysqli_query($con,"SELECT DISTINCT * FROM reports WHERE host = $getCode GROUP BY host DESC");
?>
<?php
$i=0;
while($row = mysqli_fetch_array($result)) {
?>
<div class="card card-figure has-hoverable">
<figure class="figure">
<img class="img-fluid" src="http://www.iroof.tv/screenshots/<?=$row['screenshot'];?>" alt="Card image cap">
<figcaption class="figure-caption">
<h6 class="figure-title"><?=$row['host'];?></h6>
<p class="text-muted mb-0"> <?=$row['timestamp'];?> </p>
<?php
// Assign JSON encoded string to a PHP variable
$statusJson = $row['status'];
// Decode JSON data into PHP associative array format
$arr = json_decode($statusJson, true);
// Call the function and print all the values
// $result2 = printValues($arr);
echo "<hr>";
echo "<h3> Player </h3>";
// Print a single value
echo "Status: ".$arr["player"]["status"] . "<br>";
echo $arr["player"]["filename"] . "<br>";
echo "<hr>";
echo "<h3> Graphics </h3>";
echo "Display: ".$arr["video"]["display"] . "<br>";
echo "Resolution: ".$arr["video"]["resolution"] . "<br>";
echo "Colors: ".$arr["video"]["colors"] . "<br>";
echo "<hr>";
echo "<h3> System </h3>";
echo "CPU: ".$arr["cpu"] . "<br>";
echo "Ram: ".$arr["ram"] . "<br>";
//echo "Temprature: ".$arr["temperature"] . "<br>";
echo "Fan: ".$arr["fan"] . "<br>";
?>
</figcaption>
</figure>
</div>
<?php $i++;
}
?>
</div>
<!-- Modal footer
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
</div>-->
</div>
the jquery script
<script>
$(document).ready(
function() {
setInterval(function() {
$('.card');
}, 5000); //Delay here = 5 seconds
var gen;
$(".btn").click(function(){
gen = $(this).attr("data-code");
//$.post("ajax.php", {"code": gen},function(data){console.log(data);});
});
$('#myModal').on('shown.bs.modal', function () {
$.post("ajax.php", {"code": gen},function(data){console.log(data);});
//var phpCode = "<? $getCode = $_POST["code"]; ?>";
//$('#codeElem').html(phpCode);
})
});
</script>
ajax.php file
<?php
if(isset($_POST['code']) && isset($_POST['code'])){
//$_SESSION["code"] = $_POST["code"];
$_SESSION['header'] = $_POST['code'];
$getCode = $_POST["code"];
echo $_SESSION['header'];
}
?>
i expect the variable $getCode to get the code from jquery to complete the mysql query inside the modal :
$resultHead = mysqli_query($con2,"SELECT * FROM coops WHERE Code = $getCode");
I dont think its possible to post variables to a modal, I didnt see example like that when I was searching for popup modal login.
guess you need an action page to forvard infos to modal.
This is the solution to post variables:
Change variables to your needs..
$(document).ready(function(){
$("form").submit(function(event){
event.preventDefault();
var post_id =$("#mail-id").val();
var name =$("#mail-name").val();
var email =$("#mail-email").val();
var message =$("#mail-message").val();
var type =$("#mail-type").val();
var captcha_code =$("#captcha_code").val();
var submit =$("#mail-submit").val();
$(".form-message").load("heads/comment.php",{
post_id: post_id,
name: name,
email: email,
message: message,
type: type,
captcha_code: captcha_code,
submit: submit
});
});
});
you probably need some thing like this to show variables in popup
<p class="form-message"></p>
Or you can check Bootstrap modal table :
Here

keeping what i searched in my page

i'm working on a a website and i've made a php file that will show my search results, (i'm working on request appointment) so when i click that it works but the only problem is my webpage refresh and it shows a blank page until i click on home button to redirect
// my research loop...
if(isset($_GET['submit-search'])) {
$search = mysqli_real_escape_string($conn, $_GET['search']);
$sql = "SELECT * FROM doctor WHERE DoctorFullName LIKE '%$search%' OR DoctorLocation LIKE '%$search%' OR DoctorSpeciality LIKE '%$search%'";
$result = mysqli_query($conn, $sql);
$queryResult = mysqli_num_rows($result);
if ($queryResult > 0) {
?>
<div class="container overflow">
<div class="container overflow">
<span class="mentor">
<h2 class="display Text-mentor">
<span class="mentor-admin">If the result shown bellow not seems like what you need then ;</span>
<span class="mentor-admin-quote">select the speciality of the doctor you need and your insurance, and <span class="turquoise">search</span> again</span>
</h2>
</span>
</div>
<div class="container overflow">
<p class="p-about-results"><?php echo "There is ".$queryResult." result matching your search"?></p>
<p class="p-about-results">we hope that these results are the ones you are looking for :</p>
</div>
<?php
while ($row = mysqli_fetch_assoc($result)) :
?>
// my calendar for so i can submit appointment
<div id="myModal" class="modal">
<!-- Modal content -->
<div class="modal-content">
<span class="close">×</span>
<form>
<input type="text" name="ap-time" id="t1" class="modal-input-text" placeholder="Choose a date">
<div id="sub" style="top: 40px;left: 5px;z-index:1;text-align: center;"></div>
<input type=submit name="ap_validation" value="request appointment" class="input-requestdate">
</form>
</div>
</div>
//Ajax code that i've been working on
<script type="text/javascript">
$(document).ready(function(){
//alert("jquery is working");
$(".input-requestdate").click(function(){
var name = $(".modal-input-text").val();
$.ajax({
url: "Appointment.php",
type: "POST",
async: false,
data: {
"done": 1,
"message": name,
},
success: function(data){
alert("requested");
window.location = window.location.href
}
})
});
});
</script>`
thanks for helping :)

How do I get my modal information to show, while trying to make it dynamic?

Trying to open a modal which should grab information from my database and present it onto the modal, after clicking on a specific image. The modal pops up, but it shows on the html inputs and none of the php inputs. How can i fix this? rental_id is the primary in the database table.
<?php
require_once '../core/init.php';
$id = $_POST['rental_id'];
$id = (int)$id;
$sql = "SELECT * FROM rental WHERE rental_id = '$id'";
$result = $db->query($sql);
$rental = mysqli_fetch_assoc($result);
?>
<!-- the div that represents the modal for the form -->
<?php ob_start();?>
<div class="modal fade" id="quote" tabindex="-1" role="dialog" aria-labelledby="quote" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button class="close" type="button" data-dismiss='modal' aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title text-center">Quote</h4>
</div>
<div class="modal-body">
<div class="container-fluid">
<div class="row">
<div class="col-sm-6">
<div class="center-block">
<img src="<?= $rental['img']; ?>" alt="<?= $rental['make']; ?>" class="w3-image img-responsive"/>
</div>...
and...
<script>
function detailsmodal(rental_id){
var data = {"rental_id" : rental_id};
jQuery.ajax({
url : <?php echo BASEURL;?> + 'includes/detailsmodal.php',
method : "post",
data : data,
success: function(data){
jQuery('body').append(data);
jQuery('#quote').modal('show');
},
error: function(){
alert("Something went wrong!");
}
});
}
</script>
Have you checked $id = (int)$id; ? is it set, ok with a value ?
Any return error message ? (SO question )
<?php
print_r($rental); /* seems ok when tested with ie: $id=3 */
$sql = " SELECT img, make FROM rental WHERE rental_id=? ";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param("s", $id);
$results = $stmt->execute();
$stmt->bind_result($img, $make);
$stmt->store_result();
while($stmt->fetch()){
echo"[ $img / $make ]";
}
?>
does it make a difference ?

Pulling the user's ID number from database (depending on the logged in user)

So my user's have access to changing their custom notify which is stored in my 'clients' database as 'notify'. I cannot seem to find out how to pull this information per session (as I don't want another user to change someone else's notify. Basically I'm trying to allow the user to click "Change Notify" in the navigation menu, then their notify (which I am guessing will depend on their session ID) shows up in the notify textbox, which will allow them to edit it accordingly.
This is my session starting php
<?php
include "includes/settings.php";
include "includes/database.php";
if(!isset($_SESSION))
{
session_start();
echo "Welcome=" . $_SESSION['id'];
}
if(!isset($_SESSION['username']))
{
echo'
<script language="javascript">
window.location.href="index.php"
</script>
';
}
$usersrow = mysqli_query($con, "SELECT COUNT(1) FROM `users`");
$user_row = mysqli_fetch_array($usersrow);
$user_total = $user_row[0];
$clients = mysqli_query($con, "SELECT COUNT(1) FROM `clients`");
$clients_row = mysqli_fetch_array($clients);
$clients_total = $clients_row[0];
$time_now = time("Y-m-d");
if(isset($_GET['deleteclient']))
{
if($_GET['deleteclient'])
{
$id = strip_tags($_GET['deleteclient']);
mysqli_query($con, "DELETE FROM `clients` WHERE `id` = '$id'") or die(mysqli_error($con));
}
}
?>
This is my update notify php
<?php
if(isset($_POST['upduser']))
{
$clientnotify = $_POST['notifyVal'];
$update_now = mysqli_query($con, "UPDATE `clients` SET `notify`='$clientnotify' WHERE `notify` = '".$client_notify."'");
if($update_now)
{
echo'<div class="row"><div class="col-md-12"><div class="alert alert-success">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">x</button>
<strong>OK!</strong> Client successfully updated!.
</div></div></div>';
echo '<meta http-equiv="refresh" content="1;url="dashboard.php">';
}
elseif(!$update_now)
{
echo'<div class="row"><div class="col-md-12"><div class="alert alert-danger">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">x</button>
<strong>Fail!</strong> Client not update, Try again!.
</div></div></div>
';
}
}
?>
This is my Custom Notify Form
<div class="panel panel-info">
<form method="POST">
<div class="panel-heading">
<h3 class="panel-title">SET CUSTOM NOTIFY</h3>
</div>
<div class="panel-body controls no-padding">
<div class="row-form">
<div class="col-md-3"><strong>Notify : </strong></div>
<div class="col-md-9"><input type="text" required class="form-control" value = "<?php echo $client_notify; ?>" placeholder="Notify" name="notifyVal"/></div>
</div>
</div>
<div class="panel-footer">
<button class="btn btn-success" name="updnotify">Set Custom Notify</button>
</div>
</form>
</div>
Any help would be appreciated.

Categories