how to store session when clicking LINK <a> - php

Is it possible to store session when clicking <a> in php??
If yes, how can I accomplished that? then that SESSION variable will be concatenate to JQUERY like this:
<script>
$(document).ready(function(){
serviceName = '<?php $_SESSION['sessname'] . ".php";?>';
if(serviceName!=""){
$('.main_content').load(serviceName);
}
</script>
is possible what I' trying to do?
Thank you in advance.!

Try to concatenate your value properly:
serviceName = '<?php echo $_SESSION["sessname"]' . '.php ;?>';

Related

How to reach php $_SESSION array in javascript?

I am trying to build some javascript function and I need to check if the users are logged in or not. When a user log in to my site I set a variable in session array named is_logged. I want to reach that variable in javascript is it possible???
I tried some ways but did not work like following:
var session = "<?php print_r $_SESSION['is_logged']; ?>";
alert(session);
and:
var session = '<?php echo json_encode($_SESSION['is_logged']) ?>';
alert(session);
It is either show a text or never alert at all
Just echo it:
var session = <?php echo $_SESSION['is_logged']?'true':'false'; ?>;
alert(session);
You need tertiary operator, as false is echoed as empty string so it would lead to var session = ; which is a JS syntax error.
If you want to reach all elements of $_SESSION in JavaScript you may use json_encode,
<?php
session_start();
$_SESSION["x"]="y";
?>
<script>
var session = eval('(<?php echo json_encode($_SESSION)?>)');
console.log(session);
//you may access session variable "x" as follows
alert(session.x);
</script>
But note that, exporting all $_SESSION variable to client is not safe at all.
Try the following code:
var session = "<?php echo $_SESSION['is_logged'] ?>";
In a js file you cant get the value of a php variable or php code doesnt works on a js file because php code will works in a .php extension file.
So one method is to set the session value as a hidden element value and in your js file get the value of the hidden element.
In the html:
<input type="hidden" id="sess_var" value="<?php echo $_SESSION['is_logged']; ?>"/>
In your js:
var session = document.getElementById('sess_var').value;
alert(session);
You can do things like this
Just insert the $_SESSION['is_logged'] in a hidden field like
<input type = "hidden" value = "<?php echo $_SESSION['is_logged']; ?>" id = "is_logged" />
Then you can access that in your jquery like this
var is_logged = jQuery.trim($('#is_logged').val());
//then do validation here
var get_session=<?php echo $_SESSION['is_login']
alert(get_session);
?>
## *Just try this * ##

Printing a javascript variable in php echo

To clarify:
I have an echo statement as follows:
$striptitle .= ' - <a onclick="getnewurl();" href="'.SGLink::album($aid). '">'. $namek .'</a></h1>
<a href="https://twitter.com/share" class="twitter-share-button" data-lang="en" data-via="jb_thehot" data-text="Pictures of '. $hashfinal .'" teens>Tweet</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>';
echo $striptitle ;
The onclick does this:
<script type="text/javascript">
function getnewurl()
{
var url = document.URL;
alert( url );
}
</script>
What I need is essentially to add the my anchor tag a data-url="INSERT_VAR_URL_HERE"
Is that possible?
Let me know if this isn't clear enough
Thanks!
Edit: to clarify, the alert in the function is only for testing. What I need is to be able to use the variable obtained in the function, in the same $striptitle variable.
My problem is that the url changes with AJAX, and the twitter button's data-url does not get updated. I was hoping to be able to get the new url by getting it everytime it is clicked. If there are other ways to do that, I'm open to suggestions!
Why not pass it as an argument?
$striptitle .= " - <a onclick=\"getnewurl('URL-HERE');\"...>";
Then in your script:
<script>
function getnewurl(url) {
alert(url);
}
</script>
Building on #Kolink's answer one way to not repeat the url in two places you could generate the link as:
<a onclick="getnewurl(this);" href="...">
then your JS could look like:
function getnewurl(link) {
link.setAttribute('data-url', location.href);
}
EDIT apparently jQuery.data doesn't update the dom properly, but setAttribute does

How to trigger href event on a jquery slider on a php template

Hello to all jQuery enthusiasts and experts!!
I currently use on a project this jquery slider http://spaceforaname.com/galleryview/.
The slider itself doesnt support on mouseover pause and on href click redirect.
The first part was easy to implement
dom.gv_panelWrap.bind('mouseover.galleryview',function(){
self.stopSlideshow();
}).bind('mouseout.galleryview',function(){
self.startSlideshow();
});
As for the links part i tried to do something like this
dom.gv_panelWrap.bind('click.galleryview',function(){
var href="page.php?lang=<#language#>&action=show&objectid=<#objectid#>";
window.location.href = href;
});
As you may understand this doesnt work since the link has php parametrs in it.
Of course when i change the href variable to page.php?lang=en&action=show&objectid=1 i get the page with the infromation about object n.1.
Any ideas of how i could pass the actual href through jQuery?
Thanks in advance for reading this!!!
dom.gv_panelWrap.bind('click.galleryview',
function(){
var href = 'page.php?lang=' + <?php echo "'" . $language . "'"; ?> + '&action=show&objectid=' + <?php echo "'" . $objectid . "'"; ?>;
window.location.href = href;
});
Change the language and objectid variables if needed
I'm not sure of what is what you truly want.
I don't know where are you going to get the id.
From the image thats being shown?
This is the src of the img that's been displaying.
$(".gv_panel img").attr("src")
I guess you can compare with your list of objects which is this image from.

PHP jQuery value problem

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.

var attribute echoed with php and not submitted from form

Using this
`$(function(){$(".signaler").click(function(){var element=$(this);var I = element.attr("id");var page = $('#page').attr('value');var info = "id="+I+"& page="+ page;$("#signaler"+I).hide();$("#load"+I).html('<img class="think" src="load.gif" >');$.ajax({type:"POST",url:"signaler.php",data:info,success:function(){$("#load"+I).empty();$("#ok"+I).fadeIn(200).show();}});return false;});});`
but have a form with hidden inputs only so i can echo current user info into it, was wondering if I could put it straight into jquery? Working fine though.
With a form you would use the following
var page=$('#page').attr('value');
I would like to set the id with php echoing user name/id..
Something like var id=$('<?php echo $user/$id... ?>');
If someone could point out the right jquery syntax, would be very greatful!
var id = $(<?= $_POST["whatever_it_was"] ?>)
pretty much what you used as an example...
<?php echo $user . '/' . $id ?>
As long as the file in question is being parsed as php by the server that is...
Firstly you should get the value of a form input using the val() method:
$('input#id').val();
Then on the PHP side if you POST to the page using AJAX then do:
'var id=$('<?php echo $_POST['user'] . '/' . $_POST['id'] ?>')';
What you have is essentially a string you wish to use for somewhere in you jQuery. Well in that case what you have is essentially correct. If in your PHP file you do (notice the quotation marks:
var id = "<?php echo $yourId ?>";
You can then use that variable (which holds your PHP string) normally. The following would for example select the element with the id in the variable id:
$('#' + id)

Categories