Problem with PHP, using load jquery + GET - php

I'm currently doing a private chat (style messenger), and I got a problem..
I have a href a link which sends an ID using GET to another page, the thing is that on the other page I load a jquery script which again sends to another page, suddenly it no longer finds the ID GET, what should I do? I want to actualise the page (the messages) thanks (noted that I'm new, I'm not enough good to use ajax or something..)
message.php
message
<?php
// $allUsers = 'SELECT * FROM members WHERE name LIKE "%cc%" ORDER BY id DESC' / SEARCH MEMBERS
$allUsers = $dbh->query('SELECT * FROM members ORDER BY id DESC LIMIT 0, 5');
if ($allUsers->rowCount() > 0)
{
while ($user = $allUsers->fetch())
{
?>
<div id="s_un_main">
<div class="s_un_main_pun">
<img src="../images/avatar/<?php echo $user['avatar'];?>">
<p><?php echo $user['name']; ?></p>
</div>
<div class="s_un_main_pdeux">
<a class="private" target="_blank" href="private.php?id=<?php echo $user['id']; ?>">Message</a>
</div>
</div>
<?php
}
}
else
{
echo "<p>" . "Aucun utilisateur trouvé. " . "</p>";
}
?>
private.php
private
<div id="get_name">
<?php
// USERINFO
if (isset($_SESSION['id']) AND !empty($_SESSION['id']))
{
$getid = $_GET['id'];
$req = $dbh->prepare('SELECT * FROM members WHERE id = :getid');
$req->bindValue('getid', $getid);
$req->execute();
$userinfo = $req->fetch();
}
?>
<div>
<img id="img_header" width="50" src="../images/avatar/<?php echo $userinfo['avatar'];?>">
</div>
<?php echo "<p>" . $userinfo['name'] . "</p>"; ?>
</div>
<section id="zz">
<div id="show_msg">
<?php
// AFFICHER LES MESSAGES
$getid = $_GET['id'];
$takeMsg = $dbh->prepare('SELECT * FROM private WHERE id_sender = :sender AND id_receipter = :receipter OR id_sender = :senderr AND id_receipter = :receipterr');
$takeMsg->bindValue('sender', $_SESSION['id']);
$takeMsg->bindValue('receipter', $getid);
$takeMsg->bindValue('senderr', $getid);
$takeMsg->bindValue('receipterr', $_SESSION['id']);
$takeMsg->execute();
while ($message = $takeMsg->fetch())
{
if ($message['id_receipter'] == $_SESSION['id'])
{
?>
<p style="color: red"><?php echo $message['message']; ?></p>
<?php
}
elseif ($message['id_receipter'] == $_GET['id'])
{
?>
<p style="color: green "><?php echo $message['message']; ?></p>
<?php
}
}
?>
</div>
</section>
<form id="private_form" method="POST" action="">
<textarea name="message"></textarea>
<input type="submit" name="send"></input>
</form>
<script>
setInterval('load_messages()', 1500);
function load_messages()
{
$('#zz').load('private_message.php');
}
</script>
private_message.php
error
<!-- DB -->
<?php include("../db/db.php"); ?>
<!-- DB -->
<?php
// AFFICHER LES MESSAGES
$getid = $_GET['id'];
var_dump($getid);
$takeMsg = $dbh->prepare('SELECT * FROM private WHERE id_sender = :sender AND id_receipter = :receipter OR id_sender = :senderr AND id_receipter = :receipterr');
$takeMsg->bindValue('sender', $_SESSION['id']);
$takeMsg->bindValue('receipter', $getid);
$takeMsg->bindValue('senderr', $getid);
$takeMsg->bindValue('receipterr', $_SESSION['id']);
$takeMsg->execute();
while ($message = $takeMsg->fetch())
{
if ($message['id_receipter'] == $_SESSION['id'])
{
?>
<p style="color: red"><?php echo $message['message']; ?></p>
<?php
}
elseif ($message['id_receipter'] == $_GET['id'])
{
?>
<p style="color: green "><?php echo $message['message']; ?></p>
<?php
}
}
?>
var_dump($id) = not found

Related

PHP/HTML Button works only in the first element from a loop

I am learning PHP and am stuck right now. In a while loop, all the to-do items are displayed. Each of these elements has a delete button and a check button. The delete button works in every element, but the check button only works in the first one. The check button is to be used to check off an element. When the check button is pressed, an error message is displayed that this element could not be deleted. Only in the first element of course not. I can't find the error.
I suspect there is something wrong with the in the index
<?php
$todos = $conn->query("SELECT * FROM todos ORDER BY id DESC");
?>
<div class="show-todo-section">
<?php if($todos->rowCount() <= 0){ ?>
<div class="todo-item">
<div class="empty">
<img src="img/f.png" width="100%" />
<img src="img/Ellipsis.gif" width="80px">
</div>
</div>
<?php } ?>
<?php while($todo = $todos->fetch(PDO::FETCH_ASSOC)) { ?>
<div class="todo-item">
<form action="app/checked.php" method="POST">
<button class="check-box" value="<?php echo $todo['id']; ?>" name='checked' type="submit">Check</button>
</form>
<form action="app/delete.php" method="POST">
<button class="remove-to-do" value="<?php echo $todo['id']; ?>" name='delete' type="submit">x</button>
</from>
<?php if($todo['checked']){ ?>
<h2 class="checked"><?php echo $todo['title'] ?></h2>
<?php }else { ?>
<h2><?php echo $todo['title'] ?></h2>
<?php } ?>
<small>created: <?php echo $todo['date_time'] ?></small>
<?php for ($i = 1; $i <= $todo['coins']; $i++) {
echo "<img class='coins' src='img/coin.gif' width='40px' />";
} ?>
</div>
<?php } ?>
</div>
the checked.php
<?php
if (isset($_POST['checked'])) {
require '../db_conn.php';
$id = $_POST['checked'];
if(empty($id)){
header("Location: ../index.php?mess=errorChecked". $id);
}else{
$todos = $conn->prepare("SELECT id, checked FROM todos WHERE id=?");
$todos->execute([$id]);
$todo = $todos->fetch();
$uId = $todo['id'];
$checked = $todo['checked'];
$uChecked = $checked ? 0 : 1;
$res = $conn->query("UPDATE todos SET checked=$uChecked WHERE id=$uId");
header("Location: ../index.php?mess=successChecked");
$conn = null;
exit();
}
} else{
header("Location: ../index.php?mess=errorChecked");
}

How to detect link in text by php and send default meta information

How to detect link in some text which is included from chat database and detect default meta information and put it to text like on discord.
ATTACHMENT
CODE
<?
session_start();
include('../../php/connect.php');
if(isset($_GET['uid']) && isset($_GET['cid']) && isset($_SESSION['user'])) {
$uid = $_GET['uid'];
$user = $_SESSION['user'];
$check = mysqli_query($con, "SELECT * FROM users WHERE id = '$uid'");
$ch = mysqli_fetch_array($check);
if($ch['username'] == $user) {
$cid = $_GET['cid'];
$uq = mysqli_query($con, "SELECT * FROM users WHERE id = '$cid'");
$u = mysqli_fetch_array($uq);
$asd = $u['username'];
$photo = $ch['photo'];
$query = mysqli_query($con, "SELECT * FROM users WHERE username = '$asd'");
$q = mysqli_fetch_array($query);
$row = mysqli_query($con, "SELECT * FROM messages WHERE (user1,user2,type) = ('$user','$asd','message') OR (user2,user1,type) = ('$user','$asd','message') ORDER BY id ASC");
while($result = mysqli_fetch_object($row)) { $date = date_create($result->sent); $time = date_format($date, 'H:i'); $date = date_format($date, 'd.m.Y');
?>
<div class="message" id="message">
<div class="hr-text">
<span>
<? echo $date; ?>
</span>
</div>
<div id="avatar" class="avatar-u"><img src="<? if($result->user1 == $asd) { echo '../../img/avatars/'.$q['photo']; } elseif($result->user1 == $user) { echo '../../img/avatars/'.$photo; } ?>">
</div>
<span class="username-u">
<? echo $result->user1; ?>
</span>
<span class="time">
<? echo $time; ?>
</span>
<div class="message-content">
<? echo '<xmp>'.$result->content.'</xmp>'; ?>
</div>
</div>
<?
}
} else {
header('location: ../index.php');
}
} else {
header('location: ../index.php');
}
?>
AJAX imports this site to #content-frame every 750ms.
I just want for example paste detected link to <a> element and then write some meta information like in ATTACHMENT upper

My user ranking system php sql help me

see screenshot development
as seen in the picture, where is the red circle should the number of user rankig appear. example: 1ts, 2th, 3rd...
I have tried everything but not me.
My table is users
ID Name username password Wins Loses
I leave my php code file profile.php
<?
require_once('values.inc.php');
require_once('func.inc.php');
if(!($_GET[user])) exit('Error.');
open_conn();
$res=mysql_query("SELECT ID, Name, username, Thumbnail, LinkText, LinkUrl, country FROM users WHERE Status='1' ORDER BY RAND() LIMIT 2");
while($row=mysql_fetch_assoc($res)) {
$username[] = $row[username];
$ID[] = $row[ID];
$Thumbnail[] = $row[Thumbnail];
$Name[] = $row[Name];
$LinkText[] = $row[LinkText];
$Model=mysql_query("SELECT ID, Name, username, Wins, Loses, Draws, Thumbnail, LinkText, LinkUrl, country, (Wins) / (Wins+Loses) AS WinPercent, (Wins+Loses+Draws) AS Matches, DATE_FORMAT(DateTime, '%d %M %Y') AS Date FROM users WHERE username='$_GET[user]' LIMIT 1");
$Model=mysql_fetch_assoc($Model);
$WinPercent=#round(($Model['Wins'] / ($Model['Wins']+$Model['Loses'])) * 100, 2);
$LosePercent=#round(($Model['Loses'] / ($Model['Wins']+$Model['Loses'])) * 100, 2);
if($Model['LinkText'] != '' && $Model['LinkUrl'] != '') {$Url="<br><br>$Model[LinkText]";}
if($row['LinkText'] != '' && $row['LinkUrl'] != '') {$Url[]="<br><br>$row[LinkText]<br><br>";}else{$Url[]='';}
}
close_conn();
?>
<?php if (isset($Model['username'])) { ?>
<div class="content-holder">
<div class="profile-holder">
<div class="profile-image">
<img src="images/uploads/<?php echo ucwords($Model['Thumbnail'])?>" class="photo">
</div>
<div id="photo-upload-holder">
<label for="pic" id="photo-upload-label">
<h1 class="txtShadow"><?php echo ucwords($Model['Name'])?></h1>
</label>
</div>
<!--<p class=\"userName\">#dada</p>-->
</div>
<div class="dashboard-Details overFlow">
<div class="leftColumn pull">
<div class="displayBlock"><br>
<i class="heart-icon"></i>
<span><?php echo ucwords($Model['Wins'])?></span>
</div><br>
<div class="displayBlock">
<i class="star-icon"></i>
<span><?php echo ($WinPercent)?>%</span>
</div>
</div>
<div class="rightColumn push">
<h2><?php echo ucwords($Model['username'])?></h2>
<?php if($Model['country']):?>
<h4> <?php echo $countrys; ?> <?php echo ucwords($Model['country'])?></h4>
<?php endif?>
<?php if($Model['LinkText']):?>
<h4> Instagram: #<?php echo ucwords($Model['LinkText'])?></h4>
<?php endif?>
</div>
<?php } else { ?>
<?php echo $profile14; ?>
use quotes here
$username[] = $row['username'];
$ID[] = $row['ID'];
$Thumbnail[] = $row['Thumbnail'];
$Name[] = $row['Name'];
$LinkText[] = $row['LinkText'];

Show song with typed letter

I'm making a search form with suggestions from database. All is going good but the problem is when I type "t" it shows the song with t likewise shows the song with s letter as well here is my code:
<?php
include('config.php');
if($_POST) {
$q = $_POST['searchword'];
$sql_res=mysql_query("select * from vass_songs");
while($row=mysql_fetch_array($sql_res)) {
$id=$row['id'];
$title=$row['title'];
$img=$row['img'];
$album_id=$row['album_id'];
$re_title='<b>'.$q.'</b>';
?>
<div class="suggestion_box" align="left">
<a href="/song/<?php echo $id; ?>">
<img src="/static/albums/<?php echo $id; ? >_small.jpg");">
</div><?php echo $title; ?><br/>
<span style="font-size:9px; color:#999999"><?php echo $country; ?></span>
</a>
</div>
<?php
}
} else {
}
?>
try this:
<?php
include('config.php');
if(isset($_POST['searchword'])) {
$q = $_POST['searchword'];
$sql_res=mysql_query("
SELECT *
FROM vass_songs
WHERE title LIKE '%$q%'
");
while($row=mysql_fetch_array($sql_res)) {
$id = $row['id'];
$title = $row['title'];
$img = $row['img'];
$album_id = $row['album_id'];
$re_title = "<b>{$q}</b>";
?>
<div class="suggestion_box" align="left">
<a href="/song/<?php echo $id; ?>">
<img src="/static/albums/<?php echo $id; ?>_small.jpg");">
<div><?php echo $title; ?></div><br/>
<span style="font-size:9px; color:#999999">
<?php echo $country; ?>
</span>
</a>
</div>
<?php
}
} else {
}
?>

How can I execute a mysql request on click?

<?php
require 'connect.inc.php';
$query = "SELECT `item`, `up`, `down` FROM mytable ORDER BY RAND() LIMIT 1";
$query_run = mysql_query($query);
if ($query_run = mysql_query($query)) {
while ($query_row = mysql_fetch_assoc($query_run)) {
$item = $query_row['item'];
$up = $query_row['up'];
$down = $query_row['down'];
?>
<div id="one">
<div id="votes">
<?php
echo "$up votes";
?>
</div>
</div>
<div id="or">
<div id="item">
<?php echo $item ?>
</div>
</div>
<div id="two">
<div id="votes">
<?php
echo "$down votes";
?>
</div>
</div>
<?php }
} else {
echo mysql_error();
}
?>
This is my code. I need to make it so when a user clicks the div id="one" or id="two" execute this sql query (if div one was clicked (same thing for two))
UPDATE mytable SET up=$up+1 WHERE id="1(can be any id)"
Also, can I do that math in an sql query?
Thanks
Alright so this consist of 3 parts:
1.html.
2.JS/ajax.
3.php.
1.) HTML.
<div data-action="up" class="clicker" id="<?php echo $item; ?>">
<div id="votes">
<?php
echo "$up votes";
?>
</div>
</div>
<div id="or">
<div id="item">
<?php echo $item ?>
</div>
</div>
<div data-action="down" class="clicker" id="<?php echo $item; ?>">
<div id="votes">
<?php
echo "$down votes";
?>
</div>
</div>
2.) JS/Ajax.
<script type='text/javascript' >
$('.clicker').click(function (){
doAjaxCall($(this).attr('id'), $(this).attr('data-action'));
});
function doAjaxCall(varID, vote){
var pageUrl = document.URL;
ar post = '{"ajax":"yes", "varID":' + varID + ', "vote":' + vote + '}';// Incase you want to pass dynamic ID
$.post(pageUrl,post,function(data){
var response = $.parseJSON( data )
if(response.success){
//do whatever you like.
alert('baaaam');
}
});
}
</script>
3.) php.
if($_POST['ajax'] == 'yes'){
$field = $_POST['vote'];
$query = "UPDATE mytable SET `$field` = `$field` +1 WHERE id=".$_POST['var_id'];
$query_run = mysql_query($query);
$return = array();
$return['success'] = false;
if($query_run){
$return['success'] = true;
}
echo json_encode($return);
die()
}
Update whole Page
if($_POST['ajax'] == 'yes'){
$field = $_POST['vote'];
$query = "UPDATE mytable SET `$field` = `$field` +1 WHERE id=".$_POST['var_id'];
$query_run = mysql_query($query);
$return = array();
$return['success'] = false;
if($query_run){
$return['success'] = true;
}
echo json_encode($return);
die()
}
$query = "SELECT `item`, `up`, `down` FROM mytable ORDER BY RAND() LIMIT 1";
$query_run = mysql_query($query);
if ($query_run = mysql_query($query)) {
while ($query_row = mysql_fetch_assoc($query_run)) {
$item = $query_row['item'];
$up = $query_row['up'];
$down = $query_row['down'];
?>
<div data-action="up" class="clicker" id="<?php echo $item; ?>">
<div id="votes">
<?php
echo "$up votes";
?>
</div>
</div>
<div id="or">
<div id="item">
<?php echo $item ?>
</div>
</div>
<div data-action="down" class="clicker" id="<?php echo $item; ?>">
<div id="votes">
<?php
echo "$down votes";
?>
</div>
</div>
<script type='text/javascript' >
$('.clicker').click(function (){
doAjaxCall($(this).attr('id'), $(this).attr('data-action'));
});
function doAjaxCall(varID, vote){
var pageUrl = document.URL;
var post = '{"ajax":"yes", "varID":' + varID + ', "vote":' + vote + '}';// Incase you want to pass dynamic ID
$.post(pageUrl,post,function(data){
var response = $.parseJSON( data )
if(response.success){
//do whatever you like.
alert('baaaam');
}
});
}
</script>
<?php }
} else {
echo mysql_error();
}
?>
Basically this is the whole thing. if you want i can also put the pieces together for you.
You can add and onClick() event in javascript/jquery. Then, do a POST like this
$("#div1").onClick(function(){
var info = $(this).val();
$.post("fileWithQuery.php",{toUpdate: info},function(data){ // (fileName to post, variables to send via POST, callBack function)
// Show a message
});
});
You just need to create that php file.

Categories