I can't find/think of a way to solve this problem.
I have a list of links outputted by a php script and I need to get the text value from the item (link) that's been clicked.
Here is the php part that outputs data:
$query = mysql_query("SELECT NAME FROM ccm WHERE NAME LIKE '$value%'");
while( $run = mysql_fetch_array($query)){
$name = $run['NAME'];
echo '<a id="rez_link" onClick="klik();">'.$name.'</a>';
}
And here are some of my attempts to fetch the .text() from the link that's been clicked:
var value = $('a#rez_link').text(); //this one targets every text if there are multiple search result from the query
var value = jQuery(this).find("a").text(); //this one returns nothing
So how do I do it?
Maybe I should modify the php script so that it outputs new links with id=""+i and then target them like that in jQuery or something like that.
Is there a simple way to do this?
You can't have more anchor tags with same id
echo '<a id="rez_link" onClick="klik();">'.$name.'</a>';
it's better to use class instead
echo '<a class="rez_link">'.$name.'</a>';
$(function() {
$(".rez_link").on("click",function()
{
var text = $(this).text();
alert(text);
}
});
$(function() {
$('.rez_link').bind('click', function(e) {
e.preventDefault();
var linkText = $(this).text();
$('#show').text(linkText);
});
});
It's this what you try to do ?
Related
So i got a youtube url... which im trying to get into my database in some way...
So what i got is an image
<img src = "cat">
And i also got a youtube url
https://www.youtube.com/watch?v=AFGWnqNf6t0
Then what i want is when i click on my image i want the youtube link to be inserted into a php varible so I later can insert it into my database
$("#img").click(function(){
$.ajax({
url: "php file",
});
return false;
});
How am i supposed to call the varible since Im not posting the url?
For example like $username = $_POST['username']; when i post something but to get the youtube url instead!
You can simply use the attr property like this
HTML Code
<img src = "bilder/lenny.gif" data-action = "//https://www.youtube.com/watch?v=5dsGWM5XGdg"
alt = "lenny" id = "add" width = "40" height = "40">
jquery Code :-
$("#add").click(function(){
var dataprop = $(this).attr('data-action');
//SEND IT TO AJAX NOW
});
You can add parameters to a GET request with & and ?.
test.php?bookid=15&page=435
You can add your value with jQuery:
$("#add").click(function(){
$.ajax({
url: "spelista.php?value"= $('#idofelementwithvalue').val(),
});
return false;
});
Sending the href:
$("#add").click(function(){
$.ajax({
url: "spelista.php?value"= $('#idofelementwithhref').attr('href'),
});
return false;
});
Note: You can get teh href with attr('href')
I hope that awnsers your question!
Make sure you know what value comes from your Database the "action/href/value". Then, you need to use:
$("#add").attr("href")
In this way, you get the value of the action/href/value from the html element. Replace "href" with whatever is the attribute's name. After getting the value, you need to post it to your url by jQuery post method. Please see the following example:
$("#add").click(function(){
var videoValue = $("#add").attr("href");
$.post("spelista.php", {video: videoValue}, function(data){
alert("Data: " + data);
});
}
Hope this helps!
Just an addition to Agam's Answer:
Here, the image is wrapped inside a link tag. This is just to provide a better user experience. i.e making the image look clickable(the little click hand)
<a id="link_tag" href="https://www.youtube.com/watch?v=5dsGWM5XGdg">
<img src = "bilder/lenny.gif" alt = "lenny" id = "add" width = "40" height = "40"></a>
<script>
$("#link_tag").click(function(){
var link = $(this).attr('href');
alert('This link is '+link);
});
</script>
Also your link is invalid. remove the //
Here is the answer, so you get what i was trying to achieve..
<img src = "cat" id = "https://www.youtube.com/watch?v=AFGWnqNf6t0"
onclick = "saveVideo(this)">
Then the script.... To make the id go to php
function saveVideo(x)
{
var film = x.id;
$.post('spelista.php', {film: film},function(data){
alert(data);
})
}
And then last the php varible
$url = $_POST['film'];
var_dump($url);
And then $url is the youtube url ...
Cheers!
I am building a website in which left side is restaurant name and in right side there is anchor link button.All button are produced from a single link inside a while loop.
while($dt=mysqli_fetch_array($res,MYSQLI_ASSOC))
{
<a class='morebtn hvr-rectangle-in' href='#'>Menu!</a>
}
All anchor button is same so how to know which button is get selected so that I can display menu page for that restaurant.
Change your code to this
$i = 0;
while($dt=mysqli_fetch_array($res,MYSQLI_ASSOC))
{
echo "<a class='morebtn hvr-rectangle-in' id = 'a_'.$i href='#'>Menu!</a>";
$i++;
}
And then write javascript like this
<script type = "text/javascript">
$(document).ready(function(){
$('.morebtn hvr-rectangle-in').click(function(){
var myId = this.id;
//alert(myId);
});
});
</script>
then use that id.
I don't know if I understand your problem correctly.
Maybe you can do it in JS using scrollspy.
Example:
http://getbootstrap.com/javascript/#scrollspy
I've been trying to come up with a solution for this problem. I thought .live() would help but it isn't used in jquery 1.9.1
I have this function - the gist of which is this:
while($row = $result->fetch_assoc()) {
$currentID = $row['UserID'];
$query2="SELECT FirstName, LastName, UserID FROM Users WHERE UserID = $currentID";
$result2 = $mysqli->query($query2);
$row1 = $result2->fetch_assoc();
echo '<div class="commentEntry">';
echo "<h5><a href='user.php?User=".$row1['UserID']."'>".$row1['FirstName']." ".$row1['LastName']."</a></h5>";
echo "<p>".$row['Content']."</p>";
echo "<p class='agree'>Agree</p>";
echo "</div>";
}
This function works, it isn't the issue.
So essentially with jquery I want to get hold of all dynamically added instances of a.agreeWith.
To test, I am using this:
$(document).ready(function() {
$('a.agreeWith').click(function(event) {
event.preventDefault();
alert("test");
var agree = $(this).id;
var data = 'Agree=' + agree.val();
});
});
Now what I find isn't happening, is that the alert never appears. The a href link still triggers and so clearly there is no call.
I have tested it simply by writing whatever into the page, and it works, the alert appears.
So clearly there is an issue with dynamically loaded information and trying to apply jquery to it.
The question is. How do I get around this?
Thanks for your help.
This should work for you at least to get your alert working. Not sure what your trying to do after....
$(document).ready(function() {
$(document.body).on('click', 'a.agreeWith', function(event) {
event.preventDefault();
alert("test");
var agree = $(this).id;
var data = 'Agree=' + agree.val();
});
});
How to add the value of php variabele to jquery variable and adds them,i have a form that will take a value and post to second page where second page contains a div panel where the form checkboxes values are adding and subtracting,basically i want to add the submitted value from page1 to page2.Here is the code.I have 3 form values which will be redirected one by one.Whenever user submits the respective button
if($_POST['submit'])
{
$beg=$_POST['basic'];
}
function refreshPrices() {
var currentTotalValue = 0;
var beg=?????
$("#results div").each(function() {
if (!isNaN(parseInt($(this).find("span").text().substring(1)))) {
currentTotalValue += parseInt($(this).find("span").text().substring(1));
}
});
$("#totalValue").text("$" + currentTotalValue)
}
var beg=<?php echo $beg; ?>
Try this:
var beg = '<?php echo $beg ;?>';
if you want to add to the total value you can do this:
currentTotalValue = currentTotalValue + parseInt(beg);
its better to do it this way :
var js_var = '<?=json_encode($php_var);?>';
This way you will be able to transfer any type of data to js (arrays, objects, strings, etc.) and access it easily like this:
js_var['element']
var beg = '<?php echo $beg ;?>';
But this can be only one once during the load of the page. After that we cant assign a server side script to js.
Retrieve value by input class or id like dat
var beg = $("#basic").val();
I have problem to send value from php to jQuery script.
PHP looks like that:
echo "<a id='klik' value='".$row['id']."' onclick='help()' href='http://www.something.xx/tag/".$row['link']."'>".$row['name']."</a><br>";
and script jQuery:
function help(){
var j = jQuery.noConflict();
var zmienna = j('#klik').val();
alert(zmienna);
j.post('licznik.php',{id:zmienna}, function(data) {
alert(data);
});
}
licznik.php
$p=$_POST;
$id=$p['id'];
echo $id;
$wtf = "UPDATE tag_content SET wyswietlenia=wyswietlenia+1 WHERE id='$id'";
$result = mysql_query($wtf);
And as I tested, it has problem at the begining (alert(zmienna); doesn't work, shows nothing). How to fix it?
Thx for help and if u want more informations (like more code etc.) let me know.
{id:zmienna} is not JSON, {'id':'zmienna'} is. Fix that.
The a tag can't have a value. What you can do is pass the id as a parameter:
echo '<a id="klik" onclick="help(\''.$row['id'].'\')">...</a>';
and in Javascript:
function help(zmienna) {
alert(zmienna);
}
That's probably because the anchor tag has no value attribute (http://www.w3.org/TR/html4/struct/links.html). Try this instead:
var zmienna = j('#klik').attr('value');
I would also advice against using value. If I need additional data, I use a tag like data.