Use data from view in javascript - php

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.

Related

How can I use php inside a jquery function?

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']; ?>"

Passing values through URL

component.php
<script>
jq142(document).ready( function() {
new Paginator('<?php echo HOST_URL; ?>component_ajax.php');
});
</script>
I need to pass the id from component.php file to component_ajax.php file. component_ajax.php will load on the component.php file.
component_ajax.php
$coid = $_GET['id'];
$products = $productObj->getFrontProductsbyComponents( $coid );
Explain me to that how to pass id to $coid?
You need to append the id to the component_ajax.php URL.
new Paginator('<?php echo HOST_URL; ?>component_ajax.php?id=<?php echo $your_id; ?>');
If the ID you need to pass to component_ajax.php is in JavaScript instead of PHP, append the ID to the URL with javascript.
<script>
var your_id = 123;
jq142(document).ready( function() {
new Paginator('<?php echo HOST_URL; ?>component_ajax.php?id=' + your_id);
});
</script>
Pass the ID in to your Paginator object as an argument.
var ajaxParams = {"id":<?php echo $your_id ?>};
new Paginator('<?php echo HOST_URL; ?>component_ajax.php', {"xParams": ajaxParams});
Source

how to post two values using functions in script tag?

**Here is my code**
Here is the code to post the selected value of drop downlist
Here am trying to add the PHP date function to post that in url
It is accepting the code written by me but it is not posting the value
<script type="text/javascript">
function function2(select1)
{
var add = (select1.options[select1.selectedIndex].value);
var week= <?php date('W')+1 ?>;
window.location = 'next.php?fname=' + add,'week='+week;
}
</script>
Try
window.location = 'next.php?fname=' + add +'&week='+week;
use
<?php echo date('W')+1; ?>;
and
window.location = 'next.php?fname=' + add + '&week='+week;

datetimepicker don't want my php-variable

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; ?>')

Can I convert a php $_POST variable into a jquery variable?

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 ?>';

Categories