With my code I can change my text by click:
$('.SeeMore').click(function(){
var $this = $(this);
$this.toggleClass('SeeMore');
if($this.hasClass('SeeMore')){
$this.text('+ more');
} else {
$this.text('- less');
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li class="SeeMore" style="cursor:pointer">+ more</li>
This is working well so far. But because I have different language versions of my page I want to exchange the words with php:
<script>
$('.SeeMore').click(function(){
var more = <?php echo $lang['MORE']; ?>
var less = <?php echo $lang['LESS']; ?>
var $this = $(this);
$this.toggleClass('SeeMore');
if($this.hasClass('SeeMore')){
$this.text(more);
} else {
$this.text(less);
}
});</script>
Unfortunately now my code is not working anymore and I do not know why.
There are two things that is missed by you-
1. Semi-colon at the end of the statement.
2. Double / single quotation around the PHP statement inside the jQuery.
You need to use ", Ex: var more = "<?php echo $lang['MORE']; ?>";
You forgot the semicolons and quotes:
var more = "<?php echo $lang['MORE']; ?>";
var less = "<?php echo $lang['LESS']; ?>";
writing code-generating-code is hard and can be confusing. Better use a decent template system (e.g. Smarty).
You are forgetting the quotes:
var more = "<?php echo $lang['MORE']; ?>";
var less = "<?php echo $lang['LESS']; ?>";
Use data attributes
html:
<li class="SeeMore" data-less="<?php echo $lang['LESS']; ?>" data-more="<?php echo $lang['MORE']; ?>" style="cursor:pointer">+ more</li>
js:
$('.SeeMore').click(function(){
var more = $(this).attr('data-more');
var less = $(this).attr('data-less');
var $this = $(this);
$this.toggleClass('SeeMore');
if($this.hasClass('SeeMore')){
$this.text(more);
} else {
$this.text(less);
}
});
PS: don't forget to wrap you click event in a document ready statement
You can use your php variables inside jquery with the following code:
token = "<?php echo $_SESSION['token']; ?>"
Related
How do I parse a php variable through .load in ajax?
Assuming I want to parse $x through .load so it can be used by the php file being loaded ?
<script>
$( "#searchTitle" ).load( "<?php echo $baseUrl ?>/hotels/hotelSearchTitle.php", "$x");
</script>
Do you want get the variable $x from a PHP page? If yes the best way is:
on PHP:
<?php
$x = "Hello World!";
print '{"phpvarx" : "'.$x.'"}';
?>
on jQuery Sender page:
<script>
$( "#searchTitle" ).load( "<?php echo $baseUrl ?>/hotels/hotelSearchTitle.php", function(data){
var myData = $.parseJSON(data);
var varx = myData.phpvarx;
alert(varx);
});
</script>
If you are trying to send the variable $X to a php page use it:
on PHP page:
<?php
$x = $_GET["x"];
echo $x;
?>
on jQuery Sender page:
<script>
$( "#searchTitle" ).load( "<?php echo $baseUrl ?>/hotels/hotelSearchTitle.php?x=<?php echo $x; ?>");
</script>
you must have the values of $baseUrl and $x. PHP executes on server side and then shows the page.
Probably should work like this:
<script>
$( "#searchTitle" ).load( "<?php echo $baseUrl ?>/hotels/hotelSearchTitle.php", "<?php echo $x; ?>");
</script>
I was wondering if it's possible to use data from the view in my javascript code? This is what I have:
View "index.phtml":
<?php $event = $this->event; ?>
<script>
// HERE I WANT TO USE $event
var data = <?=$event?>;
</script>
But that doesn't work!
If it contain a string value:
<script>
var data = '<?php echo $event; ?>';
</script>
If it contain a numeric value:
<script>
var data = <?php echo $event; ?>;
</script>
Also php short tag <?= should be On in php.ini to use it.
I have a big problem which really confused me the last five hours.
I use datetimepicker.
<?php
$tabname = "frequency";
$datumstart = "'#datumstart".$tabname."'";
?>
<script type ="text/javascript">
$(function(){$(<?php echo $datumstart; ?>).datetimepicker();
</script>
<input type="text" name="datumstart<?php echo $tabname; ?/>
I want to get the value of the input-field with
$(function load() {
var datumstart = $(<? php echo $datumstart; ?>).val(); });
So everytime I want to run the file with this line
var datumstart = $(<? php echo $datumstart; ?>).val(); });
the alert-output of var datumstart is blank.
But with this
var datumstart = $('#datumstartfrequency').val(); });
I get the value!
What can be wrong?
You are missing apostrophes:
$('<?php echo $datumstart; ?>')
I have this,
$(document).ready(function(){
$('#link').click(function(){
var user_login = <?php $base_url; ?>;
window.location = user_login + '/login';
});
});
note that $base_url is global variable. This works in FF but not on Chrome and IE. Thanks.
You need to add quotes around the php tags to designate that the value of $base_url is a string:
$(document).ready(function(){
$('#link').click(function(){
var user_login = "<?php echo $base_url; ?>";
window.location = user_login + '/login';
});
});
That way, when the browser gets this block, it will look something like:
$(document).ready(function(){
$('#link').click(function(){
var user_login = "http://www.example.com"; // after php is executed
window.location = user_login + '/login';
});
});
You need to echo the variable, not just simply have it in a code block. And for safety, to prevent introducing JS syntax errors, you should json-encode the value:
var login = <?php echo json_encode($base_url); ?>;
See below, note the quotes around the PHP echo
$(document).ready(function(){
$('#link').click(function(){
var login = '<?php echo $base_url; ?>';
window.location = user_login + '/login';
});
});
I suppose its a PHP file, you need to surround your variable contents by quotes to javascript
$(document).ready(function(){
$('#link').click(function(){
var login = <?php '\''.$base_url.'\''; ?>;
window.location = user_login + '/login';
});
});
Run this script in a PHP file, not in a .js file, and don't forget tho ECHO your variable and add quotes (in javascript), as said in other answers !
I want to know can I convert a php $_POST variable into a jquery variable. I have php variable below:
<?php $postduration = $_POST['durationChosen']; ?>
Can I convert the php variable above into a jquery variable?
Thanks
Yep, of course!
Just write/output your PHP code right into your JavaScript ... block.
Kinda like this:
<script>
var myVar = <? echo $postduration; ?>;
</script>
var durationChosen = "<?php echo $_POST['durationChosen']?>";
<script type="text/javascript">
var postDuration = '<?php echo $_POST['durationChosen']; ?>';
</script>
Just echo it into the Javascript on the page, just like any other dynamic content.
It is simple as...
var myVar = '<? echo $postduration; ?>';
You can convert it into a javascript variable (jquery framework helps with DOM manipulation)
<?php $postduration = $_POST['durationChosen']; ?>
var myVar = '<?php echo $postduration ?>';