Set JavaScript variable from PHP - php

How I can set the JavaScript variable strUser from PHP?
I am using the following code:
<script>
function val()
{
var e = document.getElementById("ali");
var strUser = e.options[e.selectedIndex].text;
}
</script>
brand<select id="ali" onChange="val()">
<?php
$brand=modsearchkhodroHelper::retrieve();
foreach($brand as $item)
{
?>
<option value="<?php echo $item['brand']?>" selected="<?php $id=$item['brand']?>">
<?php echo $item['brand']?>
</option>
<?php
}
echo "</select>";
?>

If you want to set the variable when the page loads, you could use something like this in the PHP code:
<script type="text/javascript">var strUser = <?php echo json_encode($someVariable); ?>;</script>
Just make sure to remove the later variable declaration from the JavaScript.
If you want to set the variable after the page loads, you'll have to use an AJAX call to ge the value from the server.

Use Cookie
in your javascript
<script type="text/javascript">
document.cookie = "cookieName=cookieValue";
</script>
in your php
<?php
$phpVar = $_COOKIE['cookieName'];
echo $phpVar;
?>

Related

How to pass a PHP variable to a .JS file using JQUERY?

I'm a JQUERY begginner. I'm trying to pass a PHP variable to a js file so that i can use it in JQUERY! But found no resource to learn to do so. I'm able to pass a JQUERY variable to a different php file but I couldn't pass PHP variable to a Jquery file. Is really possible to do so?
function.php:
<?php $variable = $fetch['username']; ?>
app.js : How can use this variable in app.js file below?
$(document).ready(function(){
var flwbtn = $(".findrowpplfollowbtn");
flwbtn.click(function(){
var selectflwusername = $(this).parent().prev().last().find(".findpplrowusername").text();
$.post("../php/flw.php",{flwusernameselected:selectflwusername})
});
});
Echo it to HTML temaplate. If it's some structure/array use for example json format.
<script>
<?php $variable = $fetch['username']; ?>
var js_variable = <?php echo json_encode($variable); ?>
</script>
or
echo some hidden element like
<input type="hidden" id="custId" name="custId" value="<?php echo($fetch['username']); ?>">
and grab if with Jquery like
<script>
$( "#custId" ).value();
</script>
with your code
JS:
$(document).ready(function(){ var flwbtn = $(".findrowpplfollowbtn");
flwbtn.click(function(){
var selectflwusername = $(this).parent().prev().last().find(".findpplrowusername").text();
$.post("../php/flw.php",{$( "#custId" ).value()})
});
});

how to access current session value from one page to other page in php

I am Setting session in one a.php like this
<?php
session_start();
$text = rand(10000,99999);
$_SESSION["vercode"] = $text;
And accessing this session in another php form like this
<script type="text/javascript">
var Cpttext = "";
function validate_test1(pricequote){
if(pricequote.vercode.value=='')
{
alert(Cpttext);
alert("please enter correct captcha");
pricequote.vercode.focus();
return false;
}
if(pricequote.vercode.value != Cpttext){
alert(Cpttext);
alert("please enter correct captcha");
pricequote.vercode.focus();
return false;
}
}
</script>
My captcha image code is
<tr>
<td><label for="captcha">Enter Code:</label><img src="../include/captcha.php"></td>
<td>
<input type="text" name="vercode" />
<td>
</tr>
Intialised session in second form b.php like this
<?php #session_start(); ?>
<script type="text/javascript">
Cpttext = "<?php echo htmlspecialchars(json_encode($_SESSION['vercode'])); ?>";
</script>`
but i am not getting the current session. i am getting the previous session. My aim is get current session and check whether it is correct or not
Here captcha image showing one code session taking previous code value.how to get both same
You are setting the session key "vercode" in a.php, but in b.php you are echoing another session key "versessioncode". Also you should be escaping generated javascript code.
<script type="text/javascript">
var Cpttext = "<?php echo htmlspecialchars(json_encode($_SESSION['vercode'])); ?>";
</script>
You need to start session before use it:
<?php session_start(); ?>
<script type="text/javascript">
Cpttext = "<?php echo $_SESSION['vercode'];?>";
</script>

how to php variable value assign as java script

<script>
var id=`$`php;
</script>
I've added php variable store as JavaScript.
but some thing wrong in my code. what wrong with there please help me
You need PHP tags around the variable name and you need to use echo to print out the value
<script>
var id = <?php echo $whatever; ?>;
</script>
Use quotes if it's a string
<script type="text/java script">
var id='<?php echo $id;?>';
</script>
Try in this way
<script>
var id=<?php echo $var;?>
</script>
Try this
<script>
var id= <?php echo $php;?>;
</script>

Javascript variables to php variables

I have string value in input box
<input name="to" value="hi.rand(1,10). my.rand(10,100).name is .rand(23,54).rand(1,4)" >
How to use $_GET['to'] if i want have
hi5 my54name is 335
Now i have got string "hi.rand(1,10). my.rand(10,100).name is .rand(23,54).rand(1,4)" but not execute like php function.
So how to format string in input box so PHP take it like variables.
You can create javascript using php. Something like this:
<script type="text/javascript">
<?php
$anyVar = "BONJOUR";
?>
var value = "<?php echo $anyVar; ?>" ;
alert(value);
</script>
In the example I'm passing the value of my var $anyVar in PHP to the value var in javascript. So after the page loads and the script is parsed, you'll end up with something like this:
<script type="text/javascript">
var value = "BONJOUR" ;
alert(value);
</script>
You can as well write js code like this:
<script type="text/javascript">
<?php
$anyVar = "BONJOUR";
?>
var value = "<?php echo $anyVar; ?>" ;
<?php echo 'alert(value);' ?>
</script>
Hope this helps.

Passing JavaScript Variables to PHP Session

Why I do not get any printing on the screen (m1, m2 or m3)?
i.e. how can I pass JavaScript var to PHP Session.
<?php session_start(); ?>
<html>
<head>
<script type="text/javascript">
function disp_text()
{
var p = document.myform.mylist.value;
<?php $_SESSION['color'] ?> = p;
<?php echo $_SESSION['color'] ?>;
}
</script>
</head>
<body>
<FORM NAME="myform">
<SELECT NAME="mylist" onChange="disp_text()">
<OPTION VALUE="m1">Red
<OPTION VALUE="m2">Blue
<OPTION VALUE="m3">Green
</SELECT>
</FORM>
Thanks for any help.
And another one for the count... JavaScript runs on the user's computer, PHP runs on the server. View the page source (right-click, View Source) and you will see exactly why it doesn't work:
<script type="text/javascript">
function disp_text()
{
var p = document.myform.mylist.value;
= p;
;
}
</script>
The only way to get JS variables into PHP is via a form (which I am using loosely to include AJAX).
Try this
<html>
<head>
<script type="text/javascript">
function disp_text()
{
var e = document.getElementsByName("mylist")[0];
var p = e.options[e.selectedIndex].value;
sessionStorage.color = p;
document.write(p);
}
</script>
</head>
<body>
<FORM NAME="myform">
<SELECT NAME="mylist" onChange="disp_text()">
<OPTION VALUE="m1">Red</OPTION>
<OPTION VALUE="m2">Blue</OPTION>
<OPTION VALUE="m3">Green</OPTION>
</SELECT>
</FORM>
</body>
</html>
index.php:
<script type="text/javascript">
var time = "OK then, let's do this!";
sessionStorage.setItem("time", time);
window.location.href = "test.php";
</script>
test.php
<script type="text/javascript">
var time = sessionStorage.getItem("time");
console.log(time);
<?php $abc = "<script>document.write(time)</script>"?>
</script>
<?php
echo $abc;
?>

Categories