How to give the current page url to HTML element property? - javascript

I have a HTML element
<a class="toggle-tell-friend" id='share-button-1' rel="sidebar" data-id='share-content-1' data-title='<?php echo CHtml::decode($job->getTitle());?>' data-link="CURRENT URL">Friend</a>
How can I set the value of data-link to the current url.

Do you want it in javascript? Since you didn't put PHP in your tags, I would assume so.
In that case, you can use window.location.href to do it in javascript.
document.getElementById("share-button-1").setAttribute("data-link", window.location.href);
or in jquery:
$('#share-button-1').attr("data-link", window.location.href);
in case you did mean through PHP, you could use the following:
http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]. In this case I'd use it like this:
<?php
$dataUrl = "http://".$_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
?>
<a class="toggle-tell-friend" id='share-button-1' rel="sidebar" data-id='share-content-1' data-title='<?php echo CHtml::decode($job->getTitle());?>' data-link="$dataUrl">Friend</a>

You can do like this using location interface
var _getCurUrl = location.href;
$("#share-button-1").attr('data-link',_getCurUrl);
JSFIDDLE

Related

Get current page URL in href

Currently working on a share button for Vk however having a dynamic site, I need the button to automatically use the correct page URL.
This is the code snippet they offer for a static site, how could I adjust this for multiple pages ?
<a href="http://vk.com/share.php?url=http://example.com" target="_blank">
Easiest way is by JavaScript:
document.getElementById("vkShare").href = 'http://vk.com/share.php?url='+document.location.href;
<a id="vkShare" target="_blank">Share to VK</a>
You can get the current page's URL by using a window object like this: window.location.href.
simple
works on all sites
function share(){
function copyToClipboard(text) {
window.prompt('share us',text);
}
copyToClipboard(window.location.href);
}
<button onclick="javascript:share();">share </button>
Using php to get the url, try to use $_SERVER['HTTP_HOST'] to get the host and $_SERVER['REQUEST_URI'] to get any query string attached to the url.
<?php
$uri = $_SERVER['REQUEST_URI'];
$host=$_SERVER['HTTP_HOST'];
//$complete_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
//To use whether HTTP OR HTTPS
if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === "on"){
$htp = "https";
}else{
$htp = "http";
}
$complete_url = $htp."//:".$host.$uri;
?>
<a href="<?=$complete_url?>" target="_blank">

How to display the value of a php variable by clicking on a hyperlink

I want to display the balance of the user when he clicks on the hyperlink. the balance of the user is stored in a php variable. I am hoping that this can be done using javascript but I dont have much experience in javascript.
Below is my code:
$userbalance = $xyz[2];
echo "Your balance stands at ".$xyz[2];
This thing is working, but I want to display $userbalance by clicking on a link
I don't know what to put in the anchor tag.
Is it possible that by clicking on the hyperlink the value of the php varibale gets displayed?
Can I pass a function to href? or can I use onclick in some way?
Try this:
<a href="somepage.php?balance=<?php $balance = $xyz[2]; echo "Your balance stands at ". $balance; ?>" >Click</a>
By above method you can display balance in url at any pages. Then if you need to use that balance there just get that value with super global variable $_GET['balance'];
In the most simplest form, this can be done via storing the value in the "data-" attribute that HTML supports and then using Javascript to alert the value upon user's click.
<p>
Click to see your account
balance.
</p>
The PHP part of this would be just to echo the correct balance into the correct place in the HTML code above.
balance.
Example of the final HTML: https://jsfiddle.net/oh8jnmg2/1/
Niltish has given the right hint. But I wouldn't recommend putting a output string into a link, which gets displayed in the url. Since HTML is rendered through PHP, you can simply attach your value to the link by typing:
<a href="yourprintsite.php?balance=$userbalance>Click here to see your balance</a>
And then on yourprintsite.php, you just type:
if (!empty($_GET['balance')) {
echo "Your balance stands at " . $_GET['balance'];
}
And you're done.
This is a solution if you don't want to actually use two pages but still show new content and hide everything else:
$userbalance = $xyz[2];
echo `Your balance stands at <a href='javascript:{document.documentElement.innerHTML = ""; document.write(` . $xyz[2] . `);}'>see variable</a>`;
If you want just to display a new value below, then use:
$userbalance = $xyz[2];
echo `Your balance stands at <div id="myDiv"><a href='javascript:{document.getElementById("myDiv").innerHTML = ` . $xyz[2] . `;}'>see variable</a></div>`;
Do you can write next code, function getAttribute must you help
<script type="text/javascript">
function clickHanler(d){
alert(d.getAttribute("data-userbalance"));
}
</script>
<a
data-userbalance="21"
href="#"
onclick="clickHanler(this);">
Click to do something
</a>
First, let's start with the HTML, you need to create a <a></a> that routes to no where :
Click to see Balance
Then, in the Javascript part :
We will assign a method to handle the click on our link.
we need to use an Ajax call to get the balance from the balance.php page :
$("#linktobalance").on("click", function(){
$.post( "balance.php", function(balance) {
//In this part, the balance is the value returned from th PHP, you can do whatever with it, I'll alert it
alert( "The user balance is "+balance );
});
});
Note : we used jQuery, you can add it to your project by doing this :
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
$("#showbalance").click(function(){
$("#balance").show();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>
Show Balance
<div id="balance" style="display: none;">
Balance = 1000
</div>

Getting DOM elements after div updated from load php file

I have an html file with a div that gets updated by a php file. The php file puts a single book's title and author into the div, that works.
I am trying to use getElementById to get the book's title getElementById('btitle') and author name getElementById('authr') when I click a button because I need to use that information. I am not getting the value of the btitle or authr ids. If I assign the whole div to a variable I get all of the div but I want them separate and the getElementByID() is not working.
Any help is appreciated.
a.html
'<div id='books'> </div>
addbooks.php
echo <stitle id='btitle'>Foundation</stitle>
echo <author id='authr'>Asimov</author>
Try putting your javascript just before the closing </body> tag. Otherwise, if your javascript is in the head, it is looking for an element that has yet to be rendered to the DOM.
I tested the following and it returns the elements as you would expect.
<?php
echo "<stitle id='btitle'>Foundation</stitle>";
echo "<author id='authr'>Asimov</author>";
?>
<script type="text/javascript">
var title = document.getElementById('btitle').textContent,
author = document.getElementById('authr').textContent;
alert(title + " by: " + author);
</script>

How do I embed Javascript in PHP code?

I have a HTML form and PHP code. In my form, I have a textbox and a textarea. Within both, I have included the "disabled" option. I want both textboxes to remain disabled until the user decides to click the "Edit" button, in which case both textboxes should be enabled so changes can be made and the output once again saved. According to the research I have done, the only way to do this is to use javascript, so I have included the following code within my PHP;
if (isset($_POST['edit']))
{
echo "<script type=\"text/javascript\">";
echo "var oldnotes = document.getElementById('oldnotes');";
echo "oldnotes.disabled = false;";
echo "var record = document.getElementById('record');";
echo "record.disabled = false;";
echo "</script>";
}
I have also tried;
if (isset($_POST['edit']))
{
echo "<script type=\"text/javascript\">";
echo "$('#oldnotes').removeAttr('disabled')";
echo "$('#record').removeAttr('disabled')";
echo "</script>";
}
But no luck :(
I am not receiving any errors, the textboxes just remain disabled after I click the Edit button. Could anyone help me with this? Thanks in advance.
This is a better approach for these kind of problems :
if (isset($_POST['edit'])){
?>
<script type="text/javascript">
var oldnotes = document.getElementById('oldnotes');
oldnotes.disabled = '';
var record = document.getElementById('record');
record.disabled = '';
</script>
<?php
}
<?
<script language='javascript'>
(javascript code here)
</script>
?>
Try use onclick on your Button
<input type="button" name="edit" id="edit" onclick="document.getElementById('oldnotes').disabled=false; document.getElementById('record').disabled=false; return false;">
I hope it helps.
The second approach seems correct, but you're missing ; there. Also, you haven't put any newline characters.
Here's what I suggest:
<?php
if (isset($_POST['edit'])) {
?>
<script type="text/javascript">
alert("Check entry"); // Use this for checking if your script is reaching here or not
$('#oldnotes, #record').removeAttr('disabled');
</script>
<?php
}
?>
You don't need to echo the whole thing. You can simply put it out of the PHP and keep it inside a if block.
Also, I've kept the alert to check if your code structure is proper. If you're getting the alert on clicking the edit button, means you're on the right path. You just need to workout on the JS.
If not, something wrong with your edit button and the form POST
Use single quotes for script type
echo "<script type='text/javascript'>\n";
//javascript goes here
echo "</script>";
use this jquery code:
<script>
$('#oldnotes,#record').attr('disabled','')
</script>
If you want to use JS with PHP you can read here:
how to write javascript code inside php
However: it seems you want the JS to be called on it own accord upon evaluation in the php logic as per the implementation provided.
Technically speaking your JS simply states it should re-enable the controls. Rather add the JS functionality to the OnClick of the submitting control and it should work fine. This is a quicker way of testing the functionality as well.
<a href="javascript:void(0);" onclick="document.getElementById('oldnotes').disabled=false;" >
Once it is clicked it will hide the appropriate control instead of trying to write new JS on the fly that is not executed. This is tried and tested and should work fine for you.
Hope it helps!
you can always make a cookie
use document.cookie()
set the onclick=“” to changing the cookie
and make a script that runs and gets the cookie when you open the site (use window.onload = function(){/*code goes here*/})

include variable in javascript?

I need some help with the following.
I have a script that close down the web page using the following:
<a href=\"Javascript:void(0);\" onclick=\"parent.location.reload();window.parent.Shadowbox.close();\">
It works as it should. My problem is that I need to change parent to another page. For this I used the following:
<a href=\"Javascript:void(0);\" onclick=\"parent.location='http://www.google.com';window.parent.Shadowbox.close();\">
It also works, but now I do not want it to go to google but one of my pages where it should include a variable. Normally I would use:
<?=$array["id"]?>
But since it is a javascript then I cant use PHP and the following solution does not work:
<a href=\"Javascript:void(0);\" onclick=\"parent.location='new-new.php?$array[‘id’];window.parent.Shadowbox.close();\">
So I tried this:
function getNewURL() {
var root="http://minside.net";
var id="topic/#entry667119";
var nURL=root+"/"+id;
return nURL;
}
With the javascript:
<a href=\"Javascript:void(0);\" onclick=\"parent.location.replace(getNewURL);window.parent.Shadowbox.close();\">
For in this way to get my variables with me, but it does not work.
Someone who can help me? Or have another idea for how to resolve this?
you can assign a php variable to javascript variable.
var a=<?php echo $array[‘id’]?>;
you are using echo for that right since you use \ for ", so it should be like this:
<a href=\"Javascript:void(0);\" onclick=\"parent.location='new-new.php?'.$array[‘id’]'.window.parent.Shadowbox.close();\">
The best way to assign a variable in Javascript is
var id = <?php echo json_encode($array["id"]); ?>
You need to use echo:
<? echo $array["id"]; ?>
Edit
var id = <?php echo $array["id"]; ?>;
var link = "<a href=\"Javascript:void(0);\" onclick=\"parent.location='new-new.php?id="+id+";window.parent.Shadowbox.close();\">";
assuming you use $_GET['id'] in new-new.php (you omitted it in the URL)
Edit
if(strlen($large_photo_exists) > 0 && strlen($thumb_photo_exists) > 0)
{
echo $thumb_photo_exists;
echo "<p>Use this photo as my profile photo</p>";
echo "<br> OR <br>";
echo "<p>Upload and use another photo</p>";
}

Categories