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 ?>';
Related
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']; ?>"
How to store PHP session variable in JQuery variable.
I have one php file where i am using session variable as
$local_session = $_SESSION['sessionusername'];
and that PHP falls also using one .js file where i want to store this $local_session which is PHP variable to JQuery variable
It is as
session_start();
ob_start();
if(!$_SESSION['sessionusername'])
{
header("location:index.php");
}
else
{
include ('connection2.php');
include('PHP/combo_val.php');
$local_session = $_SESSION['sessionusername'];
}
and after this my HTML code starts
You might do something like this in your java script:
var sessionVar = '<?php echo $local_session; ?>';
and then use this global JS variable wherever in your page.
You can write some php-script which return session data, for example
JS:
//my.js
$.getJSON( "myssesiondata.php", function( data )
{
console.log(data);
}
PHP:
<?php
//mysession.php
return json_encode($_SESSION);
?>
# Axel Amthor
My JQuery Code is as
var lgn_usr = '<?php echo $_SESSION["sessionusername"] ?>';
alert(lgn_usr);
and it display
<?php echo $_SESSION["sessionusername"] ?>
as message
# Axel Amthor
My Jquery file code is as:
$(document).ready(function() {
$('#submit').click(function() {
submit_fxn();
$('#form_nuser').submit(function(e) {
return false;
});
});
function submit_fxn(){
var check_flag = 0;
var lgn_usr = '<?php echo $local_session; ?>';
alert(lgn_usr);
};
});
OKay, I got it. $_SESSION is a PHP array, we cannot set it on the client side via Javascript. The value has to be sent to the server to be stored in the array. Now, I am going for other method....
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.
How can I declare a global variable in a PHP file and access directly in jQuery?
HTML
<script type="text/javascript">
var date_selected = <?php echo $from; ?>;
</script>
JS
$(function() {
alert("dsdsadasdas" + date_selected);
var date = date_selected;
var startDate;
var endDate;
});
Because on my test its not working.
You are missing the quotes
var date_selected = '<?php echo $a; ?>';
I have two $_SESSION variables impossible to access in any script of my page but it's sure they exist in the PHP code of the same page when I use echo to display their values.
I can display in jQuery every classical PHP variables I want with the following code, but it's impossible when they are $_SESSION variables :
<?php if( isset($_SESSION['id']) ){
echo $_SESSION['id']; // it displays the value
} ?>
<script type="text/javascript">
$(document).ready(function() {
$("#some_button").click(function(){
var a = <?php echo $_SESSION['id']; ?>;
alert(a);
});
});
</script>
I don't understand why honestly...
If you are using PHP 5.2.0 or later, change this:
var a = <?php echo $_SESSION['id']; ?>;
To this:
var a = <?php echo json_encode($_SESSION['id']); ?>
That will put quotation marks around the result if necessary and escape characters for JavaScript as needed.
If you want to use something earlier than PHP 5.2.0, you can do something like this:
var a = '<?php echo $_SESSION['id']; ?>'
Ideally, though, you'd want to use a regexp and/or escaping/replacing functions unless you know that $_SESSION['id'] will only have safe characters. json_encode() has that stuff baked in already, so it's preferable.
I don't know if the example is in the same page but i suspect you 're missing session_start() so you can't use session variables
It is working to me:
Ready and test the code below, it is quite similar to your code, but I think you forgot to call jquery api.
<?php>
session_start();
$_SESSION['id'] = 1;
if ( isset($_SESSION['id']) )
{
echo $_SESSION['id'];
echo json_encode($_SESSION['id']);
}
?>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$("#some_button").click(function(){
var a = "Result: "+ <?php echo json_encode($_SESSION['id']); ?>;
//var a="<?php echo $_SESSION['id']; ?>";
alert(a);
});
});
</script>
<form method="post" name="form" action="#">
<input type="submit" id="some_button" value="Clique" />
</form>
If its a string:
var a= "<?php echo $_SESSION['id']; ?>";