I've created a search using Ajax PHP..Anything is going good but when i try to echo the whole post in my search page from PHP script.it doesn't work.Inclucding the PHP script in the page showing all the posts without clicking the specific.
Is there any way to echo using ajax without including the PHP script ?
This is my search.php:
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#search').keyup(function(){
var sea = $('#search').val();
$.get('test.php',{getsearch:sea},function(data){
$('#result').show().html(data);
});
});
$('#search').blur(function(){
$('#result').hide();
});
});
</script>
</head>
<body>
<div class="cover">
<div class="btns-holder">
<input type="search" name="s" placeholder="Search for something.." id="search" autocomplete="off" /><input type="submit" name="sub" value="" id="sub" />
</div>
</div>
<div id="result">
</div>
<?php
$post = #$_GET['p'];
$que = "SELECT * FROM `search` WHERE `id`='$post'";
$run = mysqli_query($con,$que);
while($row=mysqli_fetch_array($con,$que)):
?>
<img src="<?php echo $row['image']; ?>" widrh="500px" height="450px" style="float: left;"></img>
<h2 style="font-family: sans-serif;"><?php echo $row['title']; ?></h2>
<?php endwhile; ?>
</body>
This is test.php from where i want to echo the data:
<?php
$host = "localhost";
$user = "root";
$pass = "";
$db = "ajax_search";
$con = mysqli_connect($host, $user, $pass, $db);
if(mysqli_connect_errno())
{
echo "connection failed".mysqli_connect_error();
}
$search = #$_GET['getsearch'];
$que = "SELECT * FROM `search` WHERE `title` LIKE '%$search%'";
$run = mysqli_query($con,$que);
while($row=mysqli_fetch_assoc($run)):
?>
<a href="search.php?p=<?php echo $row['id']; ?>">
<div class="container">
<img src="<?php echo $row['image']; ?>" width="50px" height="45px"></img>
<h3><?php echo $row['title']; ?></h3></br>
</div>
</a>
<?php endwhile; ?>
Related
I made a message deleter button, but I need 2 reloads to appear the changes...
(The rest of the code work, so it's normal that I don't show you the rest of the code...)
<?php while($r = $replies->fetch()) { ?>
<div class="message" id="<?= $r['id'] ?>">
<div class="profile">
<img class="avatar" src="members/avatars/<?php if(empty(get_avatar($r['id_author']))) { echo "default.png"; } else { echo get_avatar($r['id_author']); } ?>" width="150" height="150">
<h3 style="text-align: center;"><?= get_username($r['id_author']) ?></h3>
</div>
<div class="content">
<div class="date">
<?= date('d F Y - g:iA', strtotime($r['date_hour_post'])) ?>
</div>
<br><br>
<?= htmlspecialchars_decode($r['content']) ?>
<form method="POST"><button name="delete<?= $r['id'] ?>">Test</button></form>
<?php
$test = "delete".$r['id'];
if(isset($_POST[$test])) {
$delete = $db->prepare('DELETE FROM f_messages WHERE id = ?');
$delete->execute(array($r['id']));
$success = "Your message was successfully removed !";
}
?>
</div>
</div>
<br>
<?php } ?>
UPDATE:
I added the deleting code at the top of my php code, and it's working, thanks to Ray Andison
By the way thanks to keidakida too; he helped me to find a solution to my value problem. (And I think he don't know that)
Your form doesn't contain any data (the id to be deleted) or action (page to submit data to)?
<form method="POST" action="thispage.php">
<input id="test" name="test" type="hidden" value="<?= $r['id'] ?>">
<input type="submit">
</form>
UPDATED:
<?
if(isset($_POST[id])) {
$delete = $db->prepare('DELETE FROM f_messages WHERE id = ?');
$delete->execute(array($_POST[id]));
$success = "Your message was successfully removed !";
}
while($r = $replies->fetch()){
echo '
<div class="message" id="'.$r[id].'">
<div class="profile">
<img class="avatar" src="members/avatars/';
if(empty(get_avatar($r[id_author]))){
echo "default.png";
}else{
echo get_avatar($r[id_author]);
}
echo '
" width="150" height="150">
<h3 style="text-align:center;">
'.get_username($r[id_author]).'
</h3>
</div>
<div class="content">
<div class="date">
'.date('d F Y - g:iA', strtotime($r[date_hour_post])).'
</div>
<br>
<br>
'.htmlspecialchars_decode($r[content]).'
<form method="POST" action="thispage.php">
<input id="id" name="id" type="hidden" value="'.$r[id].'">
<input type="submit">
</form>
</div>
</div>';
}
?>
This is how I would code this, you need to change the action="thispage.php" to be the name of itself so it posts to itself, replace with the actual name of your php file
It is because the delete PHP code is at the bottom. Actions such as delete should be at the top of the HTML or while loops before presenting the data. SO try this:
<?php
if(isset($_POST["delete"])) {
$delete = $db->prepare('DELETE FROM f_messages WHERE id = ?');
$delete->execute(array($_POST['delete']));
$success = "Your message was successfully removed !";
}
while($r = $replies->fetch()) { ?>
<div class="message" id="<?= $r['id'] ?>">
<div class="profile">
<img class="avatar" src="members/avatars/<?php if(empty(get_avatar($r['id_author']))) { echo "default.png"; } else { echo get_avatar($r['id_author']); } ?>" width="150" height="150">
<h3 style="text-align: center;"><?= get_username($r['id_author']) ?></h3>
</div>
<div class="content">
<div class="date">
<?= date('d F Y - g:iA', strtotime($r['date_hour_post'])) ?>
</div>
<br><br>
<?= htmlspecialchars_decode($r['content']) ?>
<form method="POST">
<button type="button" name="delete" value="<?php echo $r['id']; ?>">Test</button>
</form>
</div>
</div>
<br>
<?php
}
?>
But you can do the same functionality without any page reload. Check AJAX PHP
Since it would be better and easier with AJAX, this is how it goes:
main.php
<?php
while ($r = $replies->fetch()) { ?>
<div class="message" id="<?= $r['id'] ?>">
<?php echo htmlspecialchars_decode($r['content']) ?>
<button onclick="delete('<?php echo $r['id']; ?>')">Delete</button>
</div>
<br>
<?php } ?>
<script>
function delete(id) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
alert("Delete successfully");
location.reload();
}
};
xmlhttp.open("POST", "delete.php", true);
// Mandatory for simple POST request
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
// Send id you want to delete
xmlhttp.send("id=" + id);
}
</script>
And make another PHP file name is delete.php like this:
<?php
include 'YOUR_DB_Connect.php';
if(isset($_POST["delete"])) {
$delete = $db->prepare('DELETE FROM f_messages WHERE id = ?');
$delete->execute(array($_POST['delete']));
}
?>
I'm setting up a new Website and i want to show the results of a searching MySQLi query in boxes.
My php code....
$query = mysqli_query($con,"SELECT * FROM `products`");
while($row = mysqli_fetch_array($query)) {
echo "<img src='/Images/".$row['product_image']."' height='200' width='200'>"
echo '<h6> Id:'.$row['product_id'].'</h6>';
echo '<h6> Name:'.$row['product_name'].'</h6>';
echo '<h6> Proce:'.$row['product_price'].'</h6>';
}
With that code the results of a searching query are presented like this
I'm trying using with many ways to achieve a presentation of the results like this way:
Any idea or advice? Thank you
Use css "float: left"
$query = mysqli_query($con,"SELECT * FROM `products`");
while($row = mysqli_fetch_array($query)) {
echo '<div style="float:left">';
echo "<img src='/Images/".$row['product_image']."' height='200' width='200'>";
echo '<h6> Id:'.$row['product_id'].'</h6>';
echo '<h6> Name:'.$row['product_name'].'</h6>';
echo '<h6> Proce:'.$row['product_price'].'</h6>';
echo '</div>';
}
you can use bootstrap to achieve this. For example, just add class="col lg-4" inside the div tag, and you will have 3 columns for each row.
Sample code:
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
<script src="https://code.jquery.com/jquery-latest.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<section class="content">
<div class="container">
<div class="row">
<?php
$get = mysqli_query($con, "SELECT * FROM `products` ORDER BY `product_name`");
while ($products = mysqli_fetch_assoc($get)) { ?>
<div class="col-lg-4">
<div class="thumbnail" align="center">
<img src="admin/images/products/<?php echo $products['photo']; ?>" style="width: 220px; height: 150px">
<div class="caption">
<h4>
<?php echo $products['product_name']; ?>
</h4>
<span>$<?php echo number_format($products['price']); ?></span>
<br />
<br />
Detail
<input type="button" name="wishlist" id="add_wishlist" value="+ " onclick="addWishlist('<?php echo $products[id]; ?>', '<?php echo $id_user; ?>')" class="btn btn-info">
</div>
</div>
</div>
<?php } ?>
</div>
</div>
</section>
I'm working on a website with php and mysql and have some problems to generate web pages URL from database rows.
I have only 3 page connection.php (mysql connection) index.php (where show al products/contents thumbnails with button with product details URL) and details.php where i want show info for single product.
from index.php i add a link to redirect to details.php page with this:
<a href="details.php?id=<?php echo $row['ID']; ?>"
it's work but Big problem is in details.php because the script don't show a single products details, but show all products, please someone can help me? Thank you
index.php code
......other html code......
<div class="row">
<?php
require_once 'connection.php';
$query = "SELECT * FROM campi_name";
$stmt = $DBcon->prepare( $query );
$stmt->execute();
while($row=$stmt->fetch(PDO::FETCH_ASSOC)){
?>
<div class="col-sm-4 col-md-3">
<div class="thumbnail">
<img src="<?php echo $row['Thumbnail']; ?>" alt="<?php echo $row['Title']; ? >">
<div class="caption">
<h4><?php echo substr($row['Title'], 0, 30); ?></h4>
<p><?php echo $row['Brand']; ?></p>
<?php echo $row['ID']; ?>
<p>Cofronta Dettagli</p>
</div>
</div>
</div>
<?php
}
?>
......other html code......
connection.php code
$DBhost = "localhost";
$DBuser = "root";
$DBpass = "";
$DBname = "prodotti";
try {
$DBcon = new PDO("mysql:host=$DBhost;dbname=$DBname",$DBuser,$DBpass);
$DBcon->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $ex){
die($ex->getMessage());
}
?>
details.php code
......other html code......
<div class="container">
<div class="row">
<?php
require_once 'connection.php';
$query = "SELECT * FROM campi_name";
$stmt = $DBcon->prepare( $query );
$stmt->execute();
while($row=$stmt->fetch(PDO::FETCH_ASSOC)){
?>
<div class="col-sm-4 stylerow">
<a href="<?php echo $row['AffiliateLink']; ?>" class="thumbnail">
<img src="<?php echo $row['Thumbnail']; ?>" alt="<?php echo $row['Title']; ? >">
</a>
</div>
<div class="col-sm-8 stylerow">
<h2><?php echo $row['Title']; ?></h2>
<p><?php echo $row['Brand']; ?></p>
<button type="button" class="btn btn-primary btn-lg">Amazon</button>
</div>
</div>
</div><!-- /.container -->
......other html code......
Add
$id=$_GET['id'];
edit following line in your code
$query = "SELECT * FROM campi_name";
To
$query = "SELECT * FROM campi_name where id="'.$id.'" ";
I am trying to select some data from a database and display that in HTML.
I thought the code below would work but it doesn't.
What code should I use instead?
And does the CSS still work that way?
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="se.css">
<title>Lotto</title>
</head>
<body>
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "lotto";
$db = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
$query = "SELECT id, vraag, AntwA, AntwB, AntwC, AntwD,id FROM vraag1";
$rows = mysqli_query($db, $query);
while($row = mysqli_fetch_assoc($rows))
{
echo "<div id='Vraag'>$row["vraag"];</div>";
}
?>
<div id="headerbg"></div>
<center>
<h1>Vraag 1</h1>
</center>
<center>
<div>
<img src="antw.png" id="img6">
</div>
<div id="plaatje">
<img id="img1" src="wip.jpg">
</div>
<div id="BGantA">
<img id="img2" src="antw.png">
</div>
<div id="antA">
Antwoord A
</div>
<div>
<img src="antw.png" id="img3">
</div>
<div>
Antwoord B
</div>
<div>
<img src="antw.png" id="img4">
</div>
<div>
Antwoord C
</div>
<div>
<img src="antw.png" id="img5">
</div>
<div>
Antwoord D
</div>
</center>
</body>
</html>
use this instead of your echo code
echo "<div id='".$row['id']."'>".$row['vraag']."</div>";
Hope it's help you.
Your syntax is wrong.Change your code to this:
while($row = mysqli_fetch_assoc($rows))
{
echo "<div id='Vraag'>".$row["vraag"]."</div>";
}
Note: To get different id for each div use a counter(you can use your table id).
its a good practice to check if your query does return results.
this while($row = mysqli_fetch_assoc($rows)) { echo "<div id='Vraag'>$row["vraag"];</div>"; } will output many div's with the same ID and ID needs to be different, so you need to have your ` before you start the loop.
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="se.css">
<title>Lotto</title>
</head>
<body>
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "lotto";
$db = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
$query = "SELECT id, vraag, AntwA, AntwB, AntwC, AntwD,id FROM vraag1";
$results = mysqli_query($db, $query);
if (mysqli_num_rows($results) > 0) {
// output data of each row?>
<div id="Vraag">
<?php
while($row = mysqli_fetch_assoc($results)) {
echo $row["vraag"];
}
} else {
echo "no results found";
}?>
</div>
?>
<div id="headerbg"></div>
<center>
<h1>Vraag 1</h1>
</center>
<center>
<div>
<img src="antw.png" id="img6">
</div>
<div id="plaatje">
<img id="img1" src="wip.jpg">
</div>
<div id="BGantA">
<img id="img2" src="antw.png">
</div>
<div id="antA">
Antwoord A
</div>
<div>
<img src="antw.png" id="img3">
</div>
<div>
Antwoord B
</div>
<div>
<img src="antw.png" id="img4">
</div>
<div>
Antwoord C
</div>
<div>
<img src="antw.png" id="img5">
</div>
<div>
Antwoord D
</div>
</center>
</body>
</html>
You have concatenate problem .
Replace the line
while($row = mysqli_fetch_assoc($rows))
{
echo "<div id='Vraag'>$row["vraag"];</div>";
}
with
$counter = 1;
while($row = mysqli_fetch_assoc($rows))
{
echo "<div id='Vraag_".$counter."' class='VraagClass'>".$row['vraag']."</div>";
}
I have added a counter to prevent generating duplicate div with the same Id.
I would like to delete a picture using ajax, but im not sure where when wrong with my buttons.
this is my First page with the pictures.
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"> </script>
<script>
$(document).ready(function () {
$("#button").click(removecat);
});
function removecat() {
$("#resultDIV").load("removecat.php");
}
</script>
</head>
<body>
<div class="container">
<?php
require 'dbfunction.php';
require 'DBCategory.php';
$con = getDbConnect();
$categoryArr = getcategoryArrCon($con, "STATUS_ACTIVE");
foreach ($categoryArr as $Name => $InfoArr) {
?>
<div class="col-md-3">
<div class="title">
<h3><?php echo $Name; ?></h3>
</div>
<img src="<?php echo "img/" . $InfoArr['image'] ?>" class="img-rounded" width="250" height="220">
<a class="btn btn-danger" id="button" value="Delete" name="delete">Delete</a>
<br /><br />
<div id="resultDIV">
</div>
</div>
<?php } ?>
</div>
</body>
This is my removecat.php page,
<?php
require 'dbfunction.php';
$con = getDbConnect();
if (mysqli_connect_errno($con)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
} else {
mysqli_query($con, "UPDATE category SET status = 1 WHERE categoryid = '$Name'"); //$name from Firstpage
if (mysqli_affected_rows($con) > 0) {
echo "You have successfully remove $Name."; //$name from Firstpage
} else {
echo "NO changes were made.";
}
mysqli_close($con);
}
?>
I am not very sure about how to channel the $name from first page to the removecat.php page. Can someone help?