Youtube url to php varible from click - php

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!

Related

Jquery load dymanic content with parameter

I'm trying to load div content based on the a href being clicked and pass in a parameter. For example, Link 1
Clicking on Link 1 will pass value "3" to process.php, and return back the value "apple is good for you".
However, I can't seem to be able to pass the value without having a submit button. Anyway I can pass the parameter to another php file to process, and return the value?
$(document).ready(function(){
$("#testing a").live("click", function(evt){
var id= $(this).attr('id');
$.post("process.php", { id: id },
function(data) {
alert(data);
$('#result').load(data);
});
})
});
Below is my HTML
<div id="testing">
hello
</div>
<div id="result"></div>
Appreciate your help, thanks a lot!
You shouldn't be using numbers for id values. Instead, prefix them with a letter, or consider adding them to a data- atribute on the element.
Additionally, $.live() is deprecated, and we are encouraged to use $.on() from here on out for event delegation. I've gone ahead and handled this in the code below, but the id issue remains.
Lastly, $.load() and $.html() aren't the same. If you want to load data into an element, you don't call the load method (though the name could lead to that confusion).
// Short-hand version of $(document).ready();
$(function(){
// Handle anchor clicks on #testing
$("#testing").on("click", "a", function(e){
// Prevent links from sending us away
e.preventDefault();
// POST our anchor ID to process.php, and handle the response
$.post("process.php", { 'id': $(this).attr("id") }, function(data){
// Set the response as the HTML content of #result
$("#result").html(data);
});
});
});
From your process.php file, you might have something like the following:
$msg = array(
"...chirp chirp...",
"This is response number 1",
"And I am the second guy you'll see!",
"Apples are good for you!"
);
$num = $_POST["id"] || 0;
echo $msg[ $num ];

Run PHP code when user clicks link and pass variables

I need to run a PHP code from external server when user clicks a link. Link can't lead directly to PHP file so I guess I need to use AJAX/jQuery to run the PHP? But how can I do it and how can I pass a variable to the link?
Something like this?
<a href="runcode.html?id=' + ID + '"> and then runcode.html will have an AJAX/jQuery code that will send that variable to PHP?
use something like this in you page with link
Some text
in the same page put this somewhere on top
<script language='javascript'>
$(function(){
$('.myClass').click(function(){
var data1 = 'someString';
var data2 = 5;//some integer
var data3 = "<?php echo $somephpVariable?>";
$.ajax({
url : "phpfile.php (where you want to pass datas or run some php code)",
data: "d1="+data1+"&d2="+data2+"&d3="+data3,
type : "post",//can be get or post
success: function(){
alert('success');//do something
}
});
return false;
});
});
</script>
on the url mentioned in url: in ajax submission
you can fetch those datas passed
for examlple
<?php
$data1 =$_POST['d1'];
$data2 =$_POST['d2'];
$data3 =$_POST['d3'];
//now you can perform actions as you wish
?>
hope that helps
You can do this with an ajax request too. The basic idea is:
Send ajax request to runcode.html
Configure another AJAX to trigger from that page
Considering, this as the markup
<a id="link" href="runcode.html'">Test</a>
JS
$("#link").on("click", function() {
$.get("runcode.html", { "id" : ID }, function(data) {
//on success
});
return false; //stop the navigation
});

jQuery AJAX - POST data to IMG tag

file.php
<?php
$variable = 'mainImage';
?>
<div>
<img src="image.php">
</div>
<input type="button" onClick="">
I am trying to do the following:
When the button is clicked $variable should be passed to image.php and the image should reload accordingly.
Yes, i know we can update the src to image.php?variable=mainImage. ( $_GET )
But is it possible to send $variable through $_POST without refreshing the page, but refreshing only the image?
You can probably accomplish this using data URIs to get something like:
// Instead of simply storing the URL as the src attribute,
// execute the post and shove the data into a data URL
$.post('image.php', {variable: 'value'}, function(data) {
image.src = "data:image/jpeg;base64," + data;
});
There might also be some useful information in the minimal image transport section of the ajaxTransport documentation about how to more tightly integrate such functionality.
Sure, you can use jQuery to accomplish this. I suggest giving your image an id before doing this, this way we can select that particular image and grab the source, so do this.
<img id="main-image" src="image.php">
$(function(){
var main_image = $('#main-image');
var url = main_image.attr('src');
$.post( url, { variable: '<?php echo $mainImage ?>' }, function( data ){
main_image.attr('src', data);
})
});

store id and transfer to another php page

I have a little question.
On my site i use for few jquery functions , they get some data and transfer by GET method to php helper. This is not work when i want to use the data in another php page. For example i have jquery that store id of the anchor:
$("#rightContent .users_list li a").live('click', function() {
var id = $(this).attr('id');
$.get("getHelper",{'userId':id},function(data){
$('#user_rightContent').html(data);
});
});
And in php file returned by "getHelper" i do:
if(isset($_GET['userId'])){
$id = $_GET['userId'];
//do something
);
The anchor of the live click lead to another page displayed by another php file where i want to use this id by using helper... This is work only if i stay on same page...
Can anybody help me in this problem?
thanks for advice
add return false at the end of live('click'....
live('click', function()
{
// code ...
return false;
}
or do that
live('click', function(e)
{
// code ...
e.preventDefault();
}
You're missing the extension on the filename.
Try it with:
$("#rightContent .users_list li a").live('click', function() {
var id = $(this).attr('id');
$.get("getHelper.php",{'userId':id},function(data){
$('#user_rightContent').html(data);
});
});

Get src of a link jQuery

I have div in which I have added 5 files, using appendTo, Now I want to get the src of all these files. How do I go about getting it using jQuery. can I use .get()?
Thanks
Jean
[edit]
<div id="some">
<img src="jquery_book.jpg">JQuery Book
</div>
I need src and href.
Something like
var arrsource = new Array();
$("#yourdivid a").each(function(){
arrSource.push ( $(this).attr("href") );
});
Not using an array
$("#yourdivid a").each(function(){
var currentElemSource = $(this).attr("href");
// write your code here
});
Edit
$("#some a").each(function(){
var currentElem = $(this);
var imageSource = currentElem.prev().attr("src");
var path = currentElem.attr("href");
});
I assumed you are using anchor tags and you have got the path in href.
Not sure what you meant by the file but you can get attribute like this:
$("selector").attr('src');
Based On Your Updated Answer:
$('#some a').each(function(){
alert ($(this).attr('href'));
});

Categories