select data from database without refreshing the page in php - javascript

While getting data from the database it is alerting 0000:00:00 while in the database it is inserting write date
<?php
$query = "select * from Reply t1 inner join users t2 on t1.UserId = t2.UserId where comment = '$commentid'";
$run1 = mysqli_query($mysqli,$query);
$numberRows = mysqli_num_rows($run1);
while($row1 = mysqli_fetch_array($run1))
{
$Reply = $row1['Reply'];
$UserId = $row1['UserId'];
$UserName = $row1['UserName'];
$date1 = $row['Date'];
echo "<script>alert('$date1')</script>";
$ageDate1 = time_elapsed_string($date1);
echo "<script>alert('$ageDate1')</script>";
?>

Related

How to get an value from a php page with js

I am trying to capture a value that is calculated on a PHP page called "classes_day.php" at the same time as I pass a value per GET, "? Day = YYYY-mm-dd" to it. How do I do this with JS or JQuery?
<?php
// aulas_dia.php
include '../config.php';
$exped_duration = 14*60;
if (isset($_GET['data'])) {
$data = $_GET['data'];
$query = "SELECT * FROM `task` WHERE `dia` LIKE ".$data."";
$result = mysqli_query($link,$query);
$soma = 0;
while ($row = mysqli_fetch_assoc($result)) {
$soma = $soma+$row['duration'];
}
$aulas_free = floor(($exped_duration-$soma)/50);
echo $aulas_free;
}
?>
I already tried using an iframe and contentwindow, but iframe gets the value and the contentwindow is empty (weird isn't it?).
Following Barmar's tip, I'm using $ .get, but I don't know why this loop is not working, can anyone help me?
for (i = 0; i < num_days; i++) {
x = (first_day+i)%7;
y = (first_day+i-x)/7;
h_dia(String(y)+String(x),i+1);
data_c = ano+"-"+mes+"-"+String(i+1);
$.get("aulas_dia.php?data="+data_c, function(data){
console.log(String(y)+String(x)+" - "+data_c+" - "+data);
set_aulas_fun(String(y)+String(x),data);
});
}
Use $.get() to send an AJAX request.
$.get("classes_day.php?data=YYYY-MM-DD", function(response) {
console.log(response);
});
BTW, you can add up all the durations in the SQL query instead of using a PHP loop. And you should use a prepared statement to prevent SQL injection.
<?php
include '../config.php';
$exped_duration = 14*60;
if (isset($_GET['data'])) {
$data = $_GET['data'];
$query = "SELECT SUM(duration) AS total FROM `task` WHERE `dia` LIKE ?";
$stmt = $link->prepare($query);
$stmt->bind_param("s", $data);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();
$soma = $row['total'];
$aulas_free = floor(($exped_duration-$soma)/50);
echo $aulas_free;
}

unable to display images in a list after uploading using the user id

im trying to display images in a list after uploading, i want php to fetch the images using the 'user_id' from the database.
here's my php code
<div class="container-two">
<?php
$image = "";
$caption = "";
if ("POST" == $_SERVER['REQUEST_METHOD']){
$caption = $_POST['Caption'];
$con = mysqli_connect("localhost","root","Y1qSYlz1iTNBMCfY","schedios");
$query = "SELECT Img_dir FROM images WHERE user_id = '".$_SESSION['user_id']."' ";
$result = mysqli_query($con,$query);
$row = $result->mysqli_fetch_assoc();
$image = $row['Img_dir'];
}
?>
<div>
<?php
$array = array();
while ($row = mysqli_fetch_assoc($query) ) {
$array['user_id'] = $row['user_id'];
echo "<ul><li ><img src='$image' ></li></ul>";}
?>
</div>
i have a table of designers, so this is the dashboard, where the user can upload images and display , thanks in advance to help.
Why are you trying to fetch $query ?
$row = mysqli_fetch_assoc($query)
You should to do the same with $result if i'm getting you right
while ($row = mysqli_fetch_assoc($query) ) {
$image = $row['Img_dir']; // maybe add $row['Img_name'] if exist in your database
// $array['user_id'] = $row['user_id'];// you already know userid from $_SESSION, right ?
echo "<ul><li ><img src='$image' ></li></ul>";}
It seems that, you are trying to echo image dir in li only, so try to save full image path in database while uploading, let me give you, sample code to give an idea.
Receive image uploaded by user from form:
// image file directory
$target = "images/".basename($image);
$sql = "INSERT INTO images (user_id,image)
VALUES
('".$_SESSION['user_id']."','$image')";
// execute query
mysqli_query($con, $sql);
if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
$msg = "Image uploaded successfully";
}else{
$msg = "Failed to upload image";
}
}
$result = mysqli_query($db, "SELECT * FROM images WHERE user_id =
'".$_SESSION['user_id']."' ");
?>
2.Display images back to user on unordered list:
<div id="content">
<?php
while ($row = mysqli_fetch_array($result)) {
echo "<div id='img_div'>";
echo "<ul><li>img src='images/".$row['image']."' /><li></ul>";
echo "</div>";
}
?>

WYSIWYG text editor showing not good in query

I have a problem i integreted the CKeditor to my CMS, and when i copy some text to the query show me a <\h2> <\p> after a , then if a try to delete those statament , after i deleted it showed me on page more <\h2> <\p>
<\p>
<\p>
<\p>
<\p>
page.php
// Query the body section for the proper page
$stmt = $con->prepare('SELECT pagebody FROM travel WHERE link = ?');
$stmt->bind_param('s', $pageid);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_array()) {
// do something with $row
$body = htmlspecialchars_decode(stripslashes($row['pagebody'])); //pentru caractere speciale
} <?php echo $body; ?>
//edit_page
<?php
// You may want to obtain refering site name that this post came from for security purposes here
// exit the script if it is not from your site and script
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$pid = $_POST['pid'];
$titlu = $_POST['titlu'];
$link = $_POST['link'];
$keyword = $_POST['keyword'];
$poza = $_POST['poza'];
$descriere = $_POST['descriere'];
$data = $_POST['data'];
$pagebody = $_POST['pagebody'];
// Filter Function -------------------------------------------------------------------
function filterFunction ($var) {
$var = nl2br(htmlspecialchars($var));
$var = str_replace("/", "\\\\", $var);
$var = preg_replace("~/~", "\\\\", $var);
return $var;
}
$titlu = filterFunction($titlu);
$link = filterFunction($link);
$keyword = filterFunction($keyword);
$poza = filterFunction($poza);
$descriere = filterFunction($descriere);
$data = filterFunction($data);
$pagebody = filterFunction($pagebody);
// End Filter Function --------------------------------------------------------------
include_once "../conx.php";
// Add the updated info into the database table
$stmt = $con->prepare("UPDATE travel SET titlu=?, link=?, keywords=?, poza=?, descriere=?, pagebody=?, data=? WHERE id = ?");
// TODO check that $stmt creation succeeded
// "s" means the database expects a string
$stmt->bind_param("ssssssss", $titlu, $link, $keyword, $poza, $descriere, $pagebody, $data, $pid);
$stmt->execute();
$stmt->close();
That's due to this line in your PHP code:
$var = str_replace("/", "\\\\", $var);

How to use php with sql query in javascript

var phpCode = '<?php
$sql = "SELECT Name,Surname,id_room FROM timatable.professors WHERE p.id_professor = '".mysqli_real_escape_string($_POST['hiddenProfId'])."'";
$resutl = mysqli_query($db,$sql);
if ($result == 1 ) {
$row = mysqli_fetch_array($result);
$professorName = $row['Name'];
$professorSurname = $row['Surname'];
} else echo "Error";
?>';
alert(phpCode);
this is my code. how to make it work ????
Try this.
First initialize, variables to null.
$professorName = "";
$professorSurname = "";
This is because, if php code enters else part, you will not get any error in javascript part.
<?php
$sql = "SELECT Name,Surname,id_room FROM timatable.professors WHERE p.id_professor = '".mysqli_real_escape_string($_POST['hiddenProfId'])."'";
$resutl = mysqli_query($db,$sql);
if ($result == 1 ) {
$row = mysqli_fetch_array($result);
$professorName = $row['Name'];
$professorSurname = $row['Surname'];
} else echo "Error";
?>
<script>
var professorName = "<?php echo $professorName ?>";
var professorSurname = "<?php echo $professorSurname ?>";
alert(professorName);
alert(professorSurname);
</script>
PHP is a server-side language. So it is processed on a server. Therefore you cannot have a PHP code in javascript.
If you want to have javascript managed some editing in database, you can use AJAX to do it without reloading the page.

How to get the value(number) from a clicked number

I want to get the clicked number
and load the data from the database according to this number. The column that the numbers are stored is named as id.
I have this code in order to display the numbers(id)...
$sql = "SELECT id FROM work WHERE username='$username' order by id asc limit 10;";
$result = mysql_query($sql);
if ($result != 0) {
$num_results = mysql_num_rows($result);
for ($i=0;$i<$num_results;$i++) {
$row = mysql_fetch_array($result);
$id = $row['id'];
echo '' .$id. '';
}
}
And then I want to load the data from this number in a form which the code for the form is...
function kotoula() {
$username = $_SESSION["username"];
if($query = mysql_query("SELECT job_title,company,website,start_date,end_date,start_year,end_year,work_history FROM work WHERE id>'$id' AND username='$username' order by id asc limit 10") or die(mysql_error()))
{
if(mysql_num_rows($query)>=1){
while($row = mysql_fetch_array($query)) {
$job_title = $row['job_title'];
$company = $row['company'];
$website = $row['website'];
$start_date = $row['start_date'];
$end_date = $row['end_date'];
$start_year = $row['start_year'];
$end_year = $row['end_year'];
$work_history = $row['work_history'];
}
}
}
}
Just put it in the URL using GET. Something like:
http://yoururl.com/user.php?id=12345
Then, user.php will receive that value on $_GET array. Example:
$_GET['id'];

Categories

Resources