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
Im trying to give a javascript variable a value from php, this way:
<script type="text/javascript">
var id = <?php echo json_encode( $_GET['id']); ?>;
window.alert('Javascript still works');
The probem is once include the line: var id = <?php echo json_encode( $_GET['id']); ?>;
My javascript breaks any ideas on the way around this?
I'm having a problem trying to use a PHP variable within JavaScript. I keep getting the following message.
"Invalid or unexpected token. Message: Undefined variable: example."
I'm unsure why example is being undefined, as it is defined within the php code. Here is my code:
<?php
$example = '2';
?>
<script type="text/javascript">
var php_var = "<?php echo json_encode($example); ?>";
</script>
Does anyone have any suggestions? I have also tried the following javascript that results in the same problem:
<script type="text/javascript">
var php_var = "<?php echo $example; ?>";
</script>
Firstly, your original code has a syntax error: $example = '2' needs a semicolon.
Secondly, the next piece of code is just assigning the string <?php echo $example; ?> to the JavaScript variable php_var where the $example PHP variable is first substituted. The $example variable should be initiated properly first, however, for this to work.
As a separate note: JS cannot execute PHP directly -- only a PHP server can do so. What you're most likely trying to do is this:
<?php
$example = '2';
?>
<script type="text/javascript">
var php_var = '<?php echo $example ;?>';
</script>
This should work, use single quotes
<?php
$example = '2';
?>
<script type="text/javascript">
var php_var = '<?php echo $example; ?>';
</script>
Tried and working both variant:
<?php
$example = '2';
?>
<script type="text/javascript">
var php_var = <?php echo json_encode($example); ?>;
console.log(php_var);
</script>
<script type="text/javascript">
var php_va = "<?php echo $example; ?>";
console.log(php_va);
</script>
I have a the code below in my js which obviously works if the js is in the php file
filter: '<?php echo $my_cat>'
how can i make a variable maybe in the php file and pull it in my external js file?
You have to place this below code at the top part of your test.php page
<script>
var filter = '<?php echo $my_cat>';
</script>
and simply you can check it on your external.js file by alert
alert(filter);
<?php
function showme(){
echo "HelloWorld";
}
?>
<script type="text/javascript">
var my_var = '<?php echo showme(); ?>' ;
</script>
You can also use short tags.
<script>
var filter = '<?=$my_cat?>';
</script>
so i want try pass variable php to function javascipt where variables is
<?php $code1=""; ?>
and for function is
<script type="text/javascript">
var $code1 = $(this);
$txt=new Array();
var $code1 = <?php echo $code1("code1"); ?>;
$(function(){
$('#go').on('click',function(){
console.log($('form').serialize());
})
$('body').on('keydown','.last',function(){
$('.last').removeClass('last');
$('#go','body').before(
'<table><tr><td><input class="last" type="text" id="code1" name="code'+(Number($(this).attr('name').match(/[0-9]+/g))+1)+'" value=" SHOULD BE HERE "></td></tr></table>');
})
})
</script>
should be value is ="$code1" so i try value= "<?php echo $code1; ?>" but it's cant can help me pls?
json_encode() is what you want, but all the quotes and what not are handled for you by the JSON-encoding. Don't add more quotes. Also, it's easiest if in your JavaScript, you assign stuff to their own variables.
var data = <?php echo json_encode($data); ?>;
Also, I recommend that you don't simply concatenate data into HTML like this. Otherwise, you're lacking escaping for HTML which can cause ambiguity and injection problems.
$('<input>').val(data);
My variable contains a color name. My example:
<?php
$myvar = blueColour;
?>
I need to add this value to body of a html page using jQuery:
<script type="text/javascript">
jQuery(body).addClass("<?php echo $myvar; ?>");
</script>
Can I use php inside jQuery this way? Thank you.
EDIT: its an wordpress site. I use this in a if to add that class on a specific page template.
<script type="text/javascript">
jQuery("body").css("color", "<?php echo $myvar; ?>");
</script>
yes, as long as your html file is interpreted by php (having .php/.phtml extension)
I did it. Worked.
Correct is:
jQuery('body').addClass("<?php echo $myvar; ?>");
with
jQuery('body')
not
jQuery(body)
if ( is_page_template('nameOfYouTemplate.php') ) {
echo '<body class='. $myvar .'>';
} else {
echo '<body>';
}