How to pass Span id value through href to another page - php

I am trying to pass song_id to a modal then pass the value gotten from the song_id in the modal to another page through href. The song_id value is stored in the span id, I then want to pass the span id value through href stored in a php variable and display it in the next page, but the span value does not go through to the next page using the href, Please i need guide on how to pass the span id value stored in the variable to the next page using href.
This is my code:
// This is where am getting song_id from
<?php
require '../db.php';
$sql = "select * from songs order by song_id desc Limit 20";
$sql_query = mysqli_query($con,$sql);
while ($row = mysqli_fetch_array($sql_query)) {
$song_id = $row['song_id'];
?>
// This is the code triggering the modal and passing the song_id to the modal
<li><a data-id="<?php echo $row['song_id']; ?>" onclick="$('$playlist_id').text($(this).data('id')); $('#myModal3').modal('show');"><span class='icon icon_playlst'></span>Add To Playlist</a></li>
// This is the modal which would receive song_id and pass it to my_playlist.php through href.
<!-- Select Playlist Start-->
<div id="myModal3" class="modal centered-modal" role="dialog">
<div class="modal-dialog login_dialog">
<!-- Modal content-->
<div class="modal-content">
<button type="button" class="close" data-dismiss="modal">
<i class="fa_icon form_close"></i>
</button>
<div class="modal-body">
<form action="create_playlist.php" method="post">
<div class="ms_register_form">
<h2>Add to Playlist</h2>
<div class="ms_weekly_box">
<div class="weekly_left">
<div class="w_top_song">
<div class="w_tp_song_name">
//This is where i store the song_id in a php variable and pass it in the href of my_playlist.php.
<h2><?php $playlist_id = '<span id="playlist_id"/>'; echo $playlist_id; ?></h2>
<h3>Hip-Hop</h3>
</div>
</div>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>

To pass data through URL you have to use the "get" parameters :
in your HTML have this kind of link : www.mysite.com/mypage.html?data_name=data_value
in your PHP you can access this data with $_GET['data_name'] (here it has the value : 'data_value')

I saw this question earlier but it got closed.
Your title question can be solved using javascript. You can wrap your span tag and button in a form and access the id of the span tag on submission of the form. You can create as many forms as you wish.
<form onsubmit="playlistHandler(event)">
<h2><span id="playlist_id"/><?php echo $playlist_id ?></span></h2>
<button type="submit">Hip-Hop</button>
</form>
<script>
function playlistHandler(event)
{
event.preventDefault();
let link_id = encodeURI(event.target.children[0].children[0].getAttribute('id'));
window.location = "my_playlist.php?new_id=" + link_id;
}
</script>

Related

How to place content of a div inside link on php while loop

Please I'm new to coding and I have a project i am working on.
I have a php while loop code that diplay 5 posts
I also have a jQuery code to display modal for the while loop with post url
The problem I'm face here is that the displayed url is inside a div
<p id="url"></p> this show the url
I want to add it as url.
I have tried </p>'> Click to view
But the link will not show
I have tried echo it still not showing.
I have been on this for two days now
The modal that display the content is outside the while loop and working
I need help
My ajax code
<script>
function showModal(id)
{
$("#url").text($("#url_"+id).text());
}
</script>
Php loop code
`<?php
foreach ($posts as $key => $result) { ?>
<div href="#" data-toggle="modal" data-target="#myModal" onClick="showModal(<?php echo $id ; ?>)" class="side-icon share-btn">
<i class="fa fa-share share-icon"></i> View link
<?php } ?>
<div class="modal fade" id="myModal" role="dialog" >
<div class="modal-dialog" style="position:absolute;bottom:0;margin:0;width:100%;">
<div class="modal-content">
<div class="modal-header">
<center><h4>Share with friends</h4></center>
<?php $t='<p id="name"></p>';
$t1?>
. Click to view
</div>
`
The id containers the url from MySQL table
The issue I'm facing is that, <p id="url"></p> is just showing www. example.com as plain test on user view but can't make it work as link to click
A hyperlink a is not necessary to invoke the modal dialog - most DOM elements can invoke whatever function you like. You assign an event listener either to all nodes of interest or a delegated listener bound to a common parent element. The snippet below might be of use?
// add delegated listener on the containing node that has the content that is to be clicked
// by the user to invoke the modal dialog.
document.querySelector('div.urls').addEventListener('click', function(e) {
// Only process nodes that are not the specified target itself. ie: it's children
if (e.target != e.currentTarget) {
let modal=document.getElementById('myModal');
// add the content from the clicked node to the modal somewhere
let link = modal.querySelector('a[data-id="modal-target"]');
link.setAttribute('href', e.target.textContent);
link.innerHTML = 'Click to view';
}
});
.url:hover {
color: red
}
.url {
cursor: pointer;
}
<div class='urls'>
<p class='url'>https://www.example.com</p>
<p class='url'>https://www.banana.com</p>
<p class='url'>https://www.bogus.org</p>
</div>
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog" style="position:absolute;bottom:0;margin:0;width:100%;">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header"></div>
<a data-id='modal-target'></a>
</div>
</div>
</div>

Passing information to a Bootstrap Modal from Database

Just a simple question to ask if there is someone available to assist me. Please take a look at this section on my index page:
<?php foreach($galleries as $item) { ?>
<div class="col-md-2">
<p>Eagle Fruit <?= $item['gallery_name'] ?></p>
<img src="<?php echo base_url() . 'assets/img/site/' . $item['gallery_cover'] ?>" class="img-responsive">
View the Gallery
</div>
<?php } ?>
My Controller is setup with the following function
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Site extends MX_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('gallery/Gallery_m');
}
public function index($id = 'ID')
{
$data['title'] = 'Welcome to Eagle Fruit Alliance (Pty) Ltd';
$data['galleries'] = $this->Gallery_m->get_galleries();
$data['gallery'] = $this->Gallery_m->view($id);
$data['content'] = 'index_view';
$this->load->view('templates/site/template',$data);
}}
Now when I click on the link i would like the gallery to open in a modal how would i achieve this? I have added a modal with the following code where am I going wrong:
Modal
<!-- Modal -->
<div class="modal fade" id="gallery" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel"><?= $gallery['name'];?></h4>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<!-- ./Model -->
Because you are interacting with the user's click on an image, this isn't a PHP or HTML question. User interaction always involves javascript/jQuery.
AJAX is a two-part methodology. It is initiated in javascript, and sends data to a server-side PHP file that does something and returns a response, which is received/dealt with in javascript. See this SO post for more information on AJAX.
However, this question doesn't need to use AJAX either, because there is no additional data to fetch.
It's a bit difficult from your code to see exactly how to trigger the modal. Usually, we look for a commonality in the code, such as a class name, to use as the trigger. In your case, you could try:
$('p+img').click()
However, I recommend modifying your code like this:
<img class="mod_trig img-responsive" src="<?php echo base_url() . 'assets/img/site/' . $item['gallery_cover'] ?>" >
Note: I moved the class to front of tag only to make it very visible in this answer. Keep it where it is in your code, if you wish.
Next, when a trigger is clicked, you use CSS selectors (jQuery uses CSS selectors, so learn them -- and Bootstrap uses jQuery, so learn jQuery) to get the href attribute of the <a> tag -- this is the direct link to the desired image, correct? You would only need to use AJAX if you didn't already have some information available to you on the page, but in this case you have all you need.
Use jQuery's .html() method to create an <img> tag inside the Bootstrap modal, replacing whatever was in that particular DIV. Here is a very simple example where we demonstrate exactly this procedure, but instead of putting it into the Bootstrap modal's framework we simply create the <img> tag inside a pre-existing DIV called #msg.
jsFiddle Demo
$('p+img').click(function(){
var i = $(this).next('a').attr('href');
//alert(i);
$('#msg').html('<img src="'+i+'" class="msg" width="200" height="200" />');
});
#msg{position:absolute;top:30%;left:40%;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div>
<p>This is a test</p>
<img src="http://placeimg.com/98/100/animals" />
View the Image
</div>
<div>
<p>This is a test</p>
<img src="http://placeimg.com/100/95/animals" />
View the Image
</div>
<div>
<p>This is a test</p>
<img src="http://placeimg.com/95/95/animals" />
View the Image
</div>
<div id="msg"></div>
*It's not a perfect example, because the placeholder image website does not serve the same image when requesting identical parameters, so you can uncomment the alert() line to see that each time you click an image you get different parameters injected into the #msg div -- you get the correct parameters for the <img> on which you clicked.
Now, all you need to do is to inject (via html() method) the newly-created <img> tag into the appropriate Bootstrap div instead of into the #msg div:
$('.modal-body').html('<img src="'+i+'" class="msg" width="200" height="200" />');
And trigger the modal to display:
jsFiddle Next Demo
$('.mod-trigger').click(function(){
var i = $(this).next('a').attr('href');
$('.modal-body').html('<img src="'+i+'" class="msg" width="200" height="200" />');
$('#gallery-lightbox').modal('show');
});
Note how in this example I replaced the CSS selector with '.mod-trigger', and created a class on each image with that className
This new example shows you how to trigger the modal if a user clicks on the <a> tag:
jsFiddle Demo
Note that the <a> tag's default action is to navigate to another page. You don't want this, so the first thing you must do is suppress that default behavior:
$('.mod-atag').click(function(e){
e.preventDefault();
var i = $(this).attr('href');
$('.modal-body').html('<img src="'+i+'" class="msg" width="200" height="200" />');
$('#gallery-lightbox').modal('show');
});
Also note that I added a className to each <a> tag to make it easy to detect the click.

How to retrieve specific value from Database(mysql) with php

Here I am displaying 4 images on my page from database.
Table equipments with columns:
equi_id, equi_name, equi_type, equi_descp, equi_img
Markup:
<!--Content-->
<center><b><u><font size="6em">Our Equipments</font></center></b></u>
<div class="container" id="content-img">
<div class = "row" id="img-div" data-toggle="modal" data-target="#content-modal">
<?php
include("database.php");
$q="select * from equipments WHERE eq_type = 'chest'";
$result=mysql_query($q,$con);
while($res=mysql_fetch_array($result))
{
echo "<div class='col-md-6' id='".$res['equi_name']."'<span id='equip_head'><b><center>".$res['equi_name']."</center></b></span> <br /> <img src='".$res['equi_img']."' class='img-responsive'></div>";
}
?>
</div>
</div>
I want to retrieve the description (equi_descp) from the same table when clicking on any image. This code is bootstrap enabled.
<!--Content Pop-Up-->
<!-- Modal -->
<div id="content-modal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<?php
include("database.php");
$q="select * from equipments WHERE eq_type = 'chest'";
$result=mysql_query($q,$con);
while($res=mysql_fetch_array($result))
{
echo "<h4 class='modal-title'>".$res['equi_name']."</h4>";
}
echo "</div>";
?>
<?php
include("database.php");
$q="select * from equipments WHERE eq_type = 'chest'";
$result=mysql_query($q,$con);
echo "<div class='modal-body'>";
while($res=mysql_fetch_array($result))
{
echo "<p>'".$res['equi_descp']."'</p>";
}
?>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
With my code, click on any image returns the description of all the images not just the one that is clicked on.
How can I fix my code to only display the description of the image that is clicked on?
Sorry for my bad English.
Thanks in advance
Modify the WHERE part of the query in your popup to target the specific item you wish to access. For example, as WHERE equi_id = 5. You obviously need to add the ID to the URL of the popup window. (How exactly, it depends on how you create your popups; your code doesn't demonstrate that.)
Don't forget to properly sanitize the ID variable from the URL before you stick it into your query. Also, you may want to switch to using mysqli_ in place of the deprecated mysql_ database driver.

Display data in div on click by data attribute which has secondary div with same data attribute

I have 3 divs, each with a different data attribute:
<div class="box">
<div class="item" data-category="#music"> CLICK </div>
</div>
<div class="box">
<div class="item" data-category="#movies"> CLICK </div>
</div>
.
.
.
I made script which activates a hidden dialog box:
$('.box').click(function() {
$('#dialogbox').show();
});
I have divs, with ids that are the same as a data-category attribute:
<div id="music"> ALL INFORMATION ABOUT MUSIC </div>
<div id="movies"> ALL INFORMATION ABOUT MOVIES </div>
.
.
.
My question:
How can I display information from all of the ALL INFORMATION divs in dialogbox by the clicked data-category?
Yes, you can
$('.box').click(function() {
var id = $(this).find('.item').data('category'); //replace with $(this).find('.item').attr('data-category') if fails
var message = $(id).html();
//now write your code here to push this message inside certain div of dialogbox, i assume message is the id
$("#message").html(message);
//then finally show
$('#dialogbox').show();
});

jQuery .bind only working on first element

I am trying to create a jQuery function that will allow me to add, edit, and delete articles on my site. Right now, I am trying to write the delete functionality. Basically, when the delete button is clicked, it will send the id of that specific article to a php script via ajax request. It gets the article ID from the ID attribute of the article element.
My problem is, that when the Delete button is clicked I get an alert box with the article ID on the first article, but not on the rest.
Here is my code:
HTML/PHP:
<div id="contentLeft">
<?php
foreach($data->blogPosts as $post){
?>
<article id="<?php print $post->ID; ?>" class="article">
<div id="options">
<div id="edit"><a class="btn btn-danger" href="#" id="editButton"><i class="fa fa-pencil"> Edit</i></a></div>
<div id="delete"><a class="btn btn-danger" href="#" id="deleteButton"><i class="fa fa-trash-o">Delete</a></i></div>
</div>
<div id="articleTitle"><?php echo $post->Title; ?></div>
<div id="content">
<?php echo $post->Content; ?>
</div>
<div id="date"><?php echo date('F jS, Y', strtotime($post->Date)); ?></div>
</article>
<?php
}
?>
</div>
My jQuery:
$("#deleteButton").bind('click', function(){
var id = $("article").closest("article").attr("id");
alert(id);
});
I appreciate the help!
In a given document ids should be unique; no more than one element should have the same ID. Please change your id's to classes and change your js accordingly:
HTML
<a class="btn btn-danger deleteButton" href="#"> ....
JS
$(".deleteButton").bind('click',...
You can add only one element with the same id.
Probably you should change id to class:
<div class="delete"><a class="btn btn-danger" href="#" class="deleteButton"><i class="fa fa-trash-o">Delete</a></i></div>
and in your js code:
$(".deleteButton").bind(...)

Categories