My PHP HTML code is this :
<?php
foreach($questions as $question){
echo"
<div id='postShare'>
<div class='col-md-10 col-md-offset-1' id='listPost'>
<div class='list-group'>
<li class='list-group-item text-center '>
<span class='pull-left' id='question_author'>".$question->author."</span>
<span class='pull-right' id='question_time'><p>".time_ago($question->time)."</p></span>
<hr>
<span class='pull-right' style='color:red;font-size:11px;'>سوال</span>
<br>
<center><h4 id='question_title'>" .$question->title. "</h4></center>
<hr>
<span class='pull-right' style='color:blue;font-size:11px;'>توضیحات</span>
<br>
<h5 id='question_desc'>".$question->description."</h5>
<hr>
<span class='pull-right' style='color:GoldenRod;font-size:11px;'>کد برنامه</span>
<br>
<pre id='question_code'>".$question->code."</pre>
<hr>
<button class='btn btn-primary' id='send_answer'>پاسخگویی به این سوال</button>
</li>
</div>
</div>
</div>";
}
?>
and my Jquery file is this :
$(function(){
$("#send_answer").click(function(e){
alert("cliked");
e.preventDefault();
});
});
This PHP code just fetch and show the database data with a button.
I want to when I click at that button it shows an alert. its work for the latest data that fetch and do not work for all.
You need to create button with the same class. Creating with a same id will result in only one to be operated on.
PHP
<?php
foreach($questions as $question) {
echo "
<div id='postShare'>
<div class='col-md-10 col-md-offset-1' id='listPost'>
<div class='list-group'>
<li class='list-group-item text-center '>
<span class='pull-left' id='question_author'>".$question->author."</span>
<span class='pull-right' id='question_time'><p>".time_ago($question->time)."</p></span><hr>
<span class='pull-right' style='color:red;font-size:11px;'>سوال</span><br><center><h4 id='question_title'>" .$question->title. "</h4></center><hr>
<span class='pull-right' style='color:blue;font-size:11px;'>توضیحات</span><br><h5 id='question_desc'>".$question->description."</h5><hr>
<span class='pull-right' style='color:GoldenRod;font-size:11px;'>کد برنامه</span><br><pre id='question_code'>".$question->code."</pre><hr>
<button class='btn btn-primary send_answer'>پاسخگویی به این سوال</button>
</li>
</div>
</div>
</div>";
}
?>
jQuery
$(function() {
$(".send_answer").click(function(e) {
alert("cliked");
e.preventDefault();
});
});
Related
i have created a php page in which i want that whenever i refresh the page it should shuffle and in that data i want that whenever i click on data it should redirect to a new page where that data should be displayed for this I have used Session to store it.
home.php
<?php
$con = new mysqli("localhost","root","","writersplanet");
$sql = "SELECT * FROM books ORDER BY RAND()";
$execute = mysqli_query($con,$sql);
while ($row = mysqli_fetch_assoc($execute)) {
$_SESSION['uploadcoverimg'] = $row['uploadcoverimg'];
$_SESSION['bookname'] =$row['bookname'];
$_SESSION['uname'] =$row['uname'];
$_SESSION['summary'] =$row['summary'];
$_SESSION['thoughts'] =$row['thoughts'];
$_SESSION['pdf'] = $row['pdf'];
$_SESSION['date'] = $row['date'];
echo "<div class='container' align='center'>
<a href='readbook.php'>
<div class='box'>
<div class='imgbox'>
<img src='upload/".$row['uploadcoverimg']."'>
</div>
<div class='content'>
<h3>".$row['bookname']." <i class='fas fa-chevron-circle-right'></i>".$row['uname']."</h3>
<p>".$row['summary']."</p>
</div>
</div>
</a>
<div class='outcome'>
<div class='heart'>
<i class='fas fa-heart'></i>
</div>
<div class='eye'>
<i class='fas fa-eye'></i>
</div>
<div class='ratings'>
<i class='fas fa-star-half-alt'></i>
</div>
</div>
<div class='scrollmenu1'>
<a href='#home' style='border-radius: 10px;margin-top: 10px; background-color: black;color: white; width: 65px;height: 30px;box-sizing: border-box;'>Horror</a>
<a href='#news' style='border-radius: 10px;margin-top: 10px; width: 65px;height: 30px;box-sizing: border-box; background-color: #0a3da3;color: white;'>Sci-Fi</a>
<a href='#contact' style='border-radius: 10px;margin-top: 10px; background-color: #ff69b4;color: white; width: 65px;height: 30px;box-sizing: border-box;'>Love</a>
</div>
<div class='content1'>
<h2>Time</h2>
<p><br>
Writers Thoughts about this book:
".$row['thoughts']."
</p>
</div>
</div>";
}
?>
In my sql code I have used [id, uname, bookname, summary,thoughts,uploadcoverimg,pdf,date].
readbook.php
<img class='bookcover' src='Desert.jpg'>
<div class='heading'>
<h3><?php echo $_SESSION['bookname']; ?> <i class='fas fa-chevron-circle-right'></i><?php echo $_SESSION['uname'];?></h3>
</div>
<div class='container'>
<div class="half_star">
<i id='half_star' class='fas fa-star-half-alt'></i>
</div>
<div class="comment">
<i id='comment' class='fas fa-comments'></i>
</div>
<div class="like">
<i id='like' class='fas fa-thumbs-up'></i><br>
</div>
<div class="date">
<i id='date' class='fas fa-calendar-alt'></i><br>
<?php echo $_SESSION['date'];?>
</div>
<div class="share">
<i id='share' class='fas fa-share'></i>
</div>
<div class='scrollmenu1'>
<a class='horror' href='#home' color='white'>Horror</a>
<a class='sci-fi' href='#news'>Sci-Fi</a>
<a class='love' href='#contact'>Love</a>
</div>
</div>
<div class='summary'>
<h1>Summary of the Book</h1>
<p><?php echo $_SESSION['summary'];?></p>
</div>
<div class="thoughts2">
<h2 class="quotes_head">Thoughts of the Writer</h2>
<p><?php echo $_SESSION['thoughts'];?></p>
</div>
<div class="storyhead">
<h1>Let's start the story:</h1>
</div>
<div class="taskbar">
<button class="bookmark"><i id="bookmark" class="fas fa-bookmark"></i>Bookmark</button>
<button class="unlock"><i id="unlock" class="fas fa-unlock"></i>Unlock</button>
<button class="download">Download</button>
</div>
<div class="pages">
click here!
<iframe src="upload/<?php echo $_SESSION['pdf'];?>" width="1000px" height="500px"></iframe>
</div>
If anybody can help me then its a great thank you from my side because this is related to my career planning.
Thank You
Use this function:
https://www.php.net/manual/en/function.http-build-query.php
instead of this:
$_SESSION['uploadcoverimg'] = $row['uploadcoverimg'];
$_SESSION['bookname'] =$row['bookname'];
$_SESSION['uname'] =$row['uname'];
$_SESSION['summary'] =$row['summary'];
$_SESSION['thoughts'] =$row['thoughts'];
$_SESSION['pdf'] = $row['pdf'];
$_SESSION['date'] = $row['date'];
use this:
$query_string = http_build_query($row);
change a href url so you append '?$query_string' to the URL
<a href='readbook.php?<?php echo $query_string?>'>
and in your readbook.php use $_GET[] instead of $_SESSION[];
There are some limitation on how long a query string can be, but in your case, I think you have small enough dataset to pass.
Your solution done properly would be using a MySQL connection in the readbook.php while you would pass only a book id from the previous page so that you can select info for the exact book from the database using a query with where clause (i.e. select * from books where book_id = x).
here my problem is the values that are selected through append are not getting inserted in to the post value..here am pasting my code
<div id="update_new_<?php echo $value->id;?>" class="update_new_<?php echo $value->id;?>">
<div class="form-group mb0" style="margin-bottom: 0px">
<label for="field-1"
class="col-sm-4 control-label">Other Document</label>
<div class="col-sm-5">
<div class="fileinput fileinput-new" data-provides="fileinput">
<?php
if (!empty($value->documents)) {
$uploaded_file = json_decode($value->documents);
}
if (!empty($uploaded_file)):foreach ($uploaded_file as $v_files_image): ?>
<div class="">
<input type="hidden" name="fileName[]"
value="<?php echo $v_files_image ?>">
<span class=" btn btn-default btn-file">
<span class="fileinput-filename"> <?php echo $v_files_image ?></span>
×
</span>
<strong>
<a href="javascript:void(0);" class="RCF"><i
class="fa fa-times"></i> Remove</a></strong>
<p></p>
</div>
<?php endforeach; ?>
<?php else: ?>
<span class="btn btn-default btn-file"><span
class="fileinput-new">Select File</span>
<span class="fileinput-exists"><?= lang('change') ?></span>
<input type="file" name="files[]">
</span>
<span class="fileinput-filename"></span>
<a href="#" class="close fileinput-exists" data-dismiss="fileinput"
style="float: none;">×</a>
<?php endif; ?>
</div>
<div id="msg_pdf" style="color: #e11221"></div>
</div>
<div class="col-sm-3">
<strong><a href="javascript:void(0);" id="update_more" onclick="add_field('<?php echo $value->id;?>');" u_id="<?php echo $value->id;?>" class="addCF update_more_<?php echo $value->id;?>"><i
class="fa fa-plus"></i> Add More
</a></strong>
</div>
</div>
</div>
here is my script please have a look
<script type="text/javascript">
var maxAppends = 0;
function add_field(id)
{
var update_new = $('<div class="form-group" style="margin-bottom: 0px">\n\
<label for="field-1" class="col-sm-4 control-label">Documentsss</label>\n\
<div class="col-sm-5">\n\
<div class="fileinput fileinput-new" data-provides="fileinput">\n\
<span class="btn btn-default btn-file"><span class="fileinput-new">Select file</span><span class="fileinput-exists" >Change</span><input type="file" name="files[]" ></span> <span class="fileinput-filename"></span>×</div></div>\n\<div class="col-sm-2">\n\<strong>\n\<i class="fa fa-times"></i> Remove</strong></div>');
maxAppends++;
$(".update_new_"+id).append(update_new);
}
</script>
Here while clicking to add more fields its coming correctly but the values selected in that field is not getting while passing the value through post and i have a doubt that if it is caused by using dynamic way off adding the data.
The problem i figured out is the problem is the <div id="update_new_<?php echo $value->id;?>"> is inside a foreach loop so how can we done this with in a foreach loop.
In your script, change .update_new_ to #update_new_
I am working on a project where I came across where when I click plus sign the data will hide and the appear in the div next to it.
here is the code:
<fieldset class="col-md-4" >
<legend>Services</legend>
<div class="col-md-12" >
<?php
$id = 0;
foreach ($servicesname as $val) {
$id++;
?>
<div class="col-md-12" style="font-size: 16px;" id="itemservices<?php echo $id ?>">
<span style="float:left;" ><?php echo $val[0]['servicename']; ?></span>
<a style="float:right;" href onclick="return addSrvToCart('itemservices<?php echo $id ?>')" >
<strong>₹ <?php echo $val[0]['amount']; ?></strong>
<span class="glyphicon glyphicon-plus-sign" id="id_<?php echo $id; ?>"></span>
</a>
</div>
<?php } ?>
</div>
</fieldset>
and the code for next div:
<fieldset class="col-md-4" >
<legend>Cart</legend>
<div style="list-style:none;" class="no-left-padding" id="cart">
<div class="col-md-12" >
</div>
</div>
<div class="col-sm-12 no-right-padding" style="background-color:#f3f0f0; padding-top:6px; border:1px solid#ccc">
<label class="pull-right">/*what should i write here to show the sum */: ₨ </label>
</div>
<button style="margin-top:10px;" class="btn btn-primary btn-sm pull-right" onclick="bookNowAfterFilter()">Book Now</button>
</fieldset>
And This the Jquery code:
<script>
function addSrvToCart(elem){
alert($('#' + elem).html());
$('#' + elem).hide();
//return false;
//what should i write here the show that hidden div
document.getElementById('summery);
return false;
}
</script>
This is the picture and I want to display the data in the Cart div and show the amount
You need to change in both HTML and Script to achieve the result
Try like this
HTML
<fieldset class="col-md-4" >
<legend>Services</legend>
<div class="col-md-12" >
<?php
$id = 0;
foreach ($servicesname as $val) {
$id++;
?>
<div class="col-md-12" style="font-size: 16px;" id="itemservices<?php echo $id ?>">
<span style="float:left;" ><?php echo $val[0]['servicename']; ?></span>
<a style="float:right;" onclick=" addSrvToCart('itemservices<?php echo $id ?>')" >
₹<strong> <?php echo $val[0]['amount']; ?></strong>
<span class="glyphicon glyphicon-plus-sign" id="id_<?php echo $id; ?>"></span>
</a>
</div>
<?php } ?>
</div>
</fieldset>
and the code for next div:
<fieldset class="col-md-4" >
<legend>Cart</legend>
<div style="list-style:none;" class="no-left-padding">
<div class="col-md-12" id="cart" >
</div>
</div>
<div class="col-sm-12 no-right-padding pull-right" style="background-color:#f3f0f0; padding-top:6px; border:1px solid#ccc">
Total: ₨ <label class="" id="sumAmount">0</label>
</div>
<button style="margin-top:10px;" class="btn btn-primary btn-sm pull-right" onclick="bookNowAfterFilter()">Book Now</button>
</fieldset>
Script:
function addSrvToCart(elem){
var div=$('#' + elem);
div.hide();
$('#cart').append(div.html());
var total = parseInt(div.find('strong').html()) + parseInt($('#sumAmount').html());
$('#sumAmount').html(total);
}
It will produce output as
I think it will help you.
I'm trying to make a page that gets entries from a database and displays them in a <div> called "gallery-item". However the lightbox works, it only shows the first entry of the database. I hope someone can make sense of this code and may be able to help me with my problem.
My html/php/sql
<?php if (isset($_POST["Website"])) {
$query=$db->prepare("SELECT * FROM `portfoliodb`.`website`");
$query->execute();
while ( $row = $query->fetch()){
$website_id = $row['website_id'];
$website_name = $row ['website_name'];
$website_desc_en = $row ['website_desc_en'];
$website_tags = $row ['website_tags'];
$website_image = $row ['website_image'];
$website_type = $row['website_type'];
echo'
<div class="background hidden" id="background"></div>
<a>
<div class="lightbox hidden" id="lightbox">
<div class="box-title hidden" id="box-title">
<h4 class="hidden" id="box-title-h">' . htmlspecialchars($website_name) . '</h4>
<h4 class="close hidden" id="close">X</h4>
</div>
<div class="box-img hidden" id="box-img">
</div>
<div class="box-foot hidden" id="box-foot">
<div class="box-space hidden" id="box-space"></div>
<div class="box-desc hidden" id="box-desc">
<p class="desc-text hidden" id="box-text">'. htmlspecialchars($website_desc_en) .' </p>
</div>
<div class="tag-space-box hidden" id="box-tags-space"></div>
<div class="tag-box hidden" id="box-tags">
<small id="box-small" class="hidden">'. htmlspecialchars($website_tags) .'</small>
</div>
</div>
</div>
</a>
<a>
<div class="gallery-item-web button" id="button">
<p class="gallary-item-text">';
echo htmlspecialchars($website_name);
echo'</p>';
echo '<i class="fa fa-desktop fa-3x position-icon"></i>
</div></a>
';
}}
The gallery shows all entries in the database so that works fine, the only problem is that the lightbox shows the first entry, so if I were to have a database with the entries "Apples, Pears, Oranges", the gallery would display "Apples, Pears, Oranges". But the lightbox would display "Apples" on every single entry.
My Jquery
$(document).ready(function(){
$(".button").click(function(){
$("#background").toggleClass("hidden");
$("#lightbox").toggleClass("hidden");
$("#box-title").toggleClass("hidden");
$("#box-title-h").toggleClass("hidden");
$(".close").toggleClass("hidden");
$("#box-img").toggleClass("hidden");
$("#box-foot").toggleClass("hidden");
$("#box-space").toggleClass("hidden");
$("#box-desc").toggleClass("hidden");
$("#box-text").toggleClass("hidden");
$("#box-tags-space").toggleClass("hidden");
$("#box-tags").toggleClass("hidden");
$("#box-small").toggleClass("hidden");
});
$(".close").click(function(){
$("#background").toggleClass("hidden");
$("#lightbox").toggleClass("hidden");
$("#box-title").toggleClass("hidden");
$("#box-title-h").toggleClass("hidden");
$(".close").toggleClass("hidden");
$("#box-img").toggleClass("hidden");
$("#box-foot").toggleClass("hidden");
$("#box-space").toggleClass("hidden");
$("#box-desc").toggleClass("hidden");
$("#box-text").toggleClass("hidden");
$("#box-tags-space").toggleClass("hidden");
$("#box-tags").toggleClass("hidden");
$("#box-small").toggleClass("hidden");
});
$(".background").click(function(){
$("#background").toggleClass("hidden");
$("#lightbox").toggleClass("hidden");
$("#box-title").toggleClass("hidden");
$("#box-title-h").toggleClass("hidden");
$(".close").toggleClass("hidden");
$("#box-img").toggleClass("hidden");
$("#box-foot").toggleClass("hidden");
$("#box-space").toggleClass("hidden");
$("#box-desc").toggleClass("hidden");
$("#box-text").toggleClass("hidden");
$("#box-tags-space").toggleClass("hidden");
$("#box-tags").toggleClass("hidden");
$("#box-small").toggleClass("hidden");
});
});
(It's pretty bad I know, this is my first time using Jquery)
This is the Jquery for the lightbox, it toggles the class on every item of the lightbox to "hidden". Whih basically makes every item with that class invisible, great for a lightbox.
First off, you don't need to add class of "hidden" to every element. You can just add it to the container element and then all tags inside would also be hidden.
Your main mistake was to have the same ids and classes for all images, you will need to ensure that each image has an unique identifier.
Try my solution: https://jsfiddle.net/3vhv90ce/2/
HTML:
<div class="container1">
<div class="background" id="background"></div>
<a>
<div class="lightbox" id="lightbox">
<div class="box-title" id="box-title">
<h4 class="" id="box-title-h">name</h4>
<h4 class="close" id="close">X</h4>
</div>
<div class="box-img" id="box-img">
</div>
<div class="box-foot" id="box-foot">
<div class="box-space" id="box-space"></div>
<div class="box-desc" id="box-desc">
<p class="desc-text" id="box-text">description</p>
</div>
<div class="tag-space-box" id="box-tags-space"></div>
<div class="tag-box" id="box-tags">
<small id="box-small" class="">tags</small>
</div>
</div>
</div>
</a>
</div>
</div>
<div class="gallery-item-web button" data-id="1">
<p class="gallary-item-text">Click me</p>
<i class="fa fa-desktop fa-3x position-icon"></i>
</div>
<div class="container2">
<div class="background" id="background"></div>
<a>
<div class="lightbox" id="lightbox">
<div class="box-title" id="box-title">
<h4 class="" id="box-title-h">name</h4>
<h4 class="close" id="close">X</h4>
</div>
<div class="box-img" id="box-img">
</div>
<div class="box-foot" id="box-foot">
<div class="box-space" id="box-space"></div>
<div class="box-desc" id="box-desc">
<p class="desc-text" id="box-text">description</p>
</div>
<div class="tag-space-box" id="box-tags-space"></div>
<div class="tag-box" id="box-tags">
<small id="box-small" class="">tags</small>
</div>
</div>
</div>
</a>
</div>
</div>
<div class="gallery-item-web button" data-id="2">
<p class="gallary-item-text">Click me</p>
<i class="fa fa-desktop fa-3x position-icon"></i>
</div>
JS:
$(document).ready(function(){
$(".button").click(function(){
$('.container'+$(this).data('id')).toggleClass("hidden");
});
});
CSS:
.hidden {
display: none;
}
.gallery-item-web {
cursor: pointer;
}
I suppose your problem came from those lines:
<div class="background hidden" id="background"></div>
<div class="gallery-item-web button" id="button">
you have many controls with the same id, And this is ambiguous for jQuery.
I have this code snippet that is failing to work, don't know what am doing wrong, am getting content from MYSQL database and using if else statement with while loop to echo the contents.
<?php
if ($row_item['cat_item_id'] == ''){
echo '<div class="col-sm-6 col-md-3"">
</p>
<p>
No Item To Show
</p>
</div>
</div>';
}
else {
while ($row_item = mysql_fetch_assoc($item)){
echo
'
<div class="col-sm-6 col-md-3" style="'.$row_item['display'].'">
<div class="thumbnail">
<img src="myaccount/user_data/'.$row_item['file_name'].'" />
</div>
<div class="caption">
<h3>'.$row_item['item_name'].'</h3>
<p>
<a href="item_detail.php?item='.$row_item['cat_item_id'].'" class="btn btn-primary" role="button">
View Item
</a>
<a href="contact_seller.php?contact='.$row_item['cat_item_id'].'" class="btn btn-default" role="button">
Contact Owner
</a>
</p>
</div>
</div>';
}
}
?>
Help me figure out what am not doing right.
You have to call while ($row_item = mysql_fetch_assoc($item))
first place.
Otherwise the $row_items doesn't get initialized properly.
So you put all inside
while ($row_item = mysql_fetch_assoc($item))
And check for the if-else conditions inside the while:
if ($row_item['cat_item_id'] == '') // this goes inside the while. Otherwise $row_item is not initialized properly
You are checking if
if ($row_item['cat_item_id'] == ''){
before you actually call
while ($row_item = mysql_fetch_assoc($item)){
To get $row_item
Try changing it to something like this:
while ($row_item = mysql_fetch_assoc($item)){
if ($row_item['cat_item_id'] == ''){
echo '<div class="col-sm-6 col-md-3"">
</p>
<p>No Item To Show</p></div></div>';
}else {
echo
'
<div class="col-sm-6 col-md-3" style="'.$row_item['display'].'">
<div class="thumbnail">
<img src="myaccount/user_data/'.$row_item['file_name'].'" />
</div>
<div class="caption">
<h3>'.$row_item['item_name'].'</h3>
<p>
<a href="item_detail.php?item='.$row_item['cat_item_id'].'" class="btn btn-primary" role="button">
View Item
</a>
<a href="contact_seller.php?contact='.$row_item['cat_item_id'].'" class="btn btn-default" role="button">
Contact Owner
</a>
</p>
</div>
</div>';
}
}
Try mysql_fetch_array
while ($row_item = mysql_fetch_array($item)){
echo
'
<div class="col-sm-6 col-md-3" style="'.$row_item['display'].'">
<div class="thumbnail">
<img src="myaccount/user_data/'.$row_item['file_name'].'" />
</div>
<div class="caption">
<h3>'.$row_item['item_name'].'</h3>
<p>
<a href="item_detail.php?item='.$row_item['cat_item_id'].'" class="btn btn-primary" role="button">
View Item
</a>
<a href="contact_seller.php?contact='.$row_item['cat_item_id'].'" class="btn btn-default" role="button">
Contact Owner
</a>
</p>
</div>
</div>';
}