Can JavaScript elements be recognized if they are within tags? So like for example:
<?php
$username = "maniacal";
echo "<p class='welcome' id='greeting'>Hi, Welcome to the site!!</p>"
?>
So thats what my php looks like. Then from that can I call the 'greeting' element from another part of the code like document.getElementById('greeting').innerHTML='Greetings ' + name + '!'; ?
yes you can
eg.
<?php
$username = "maniacal";
echo"<p class='welcome' id='greeting'>Hi, Welcome to the site!!</p>" ;
?>
<script>
document.getElementById('greeting').innerHTML="Hi <?php echo $username?>, welcome to the site";
</script>
yes ofourse you can do it but you should add your javascript code in document.ready tag because your
<p class='welcome' id='greeting'>Hi, Welcome to the site!!</p>
should be placed on your html page before you call it by --> getElementById
Please try below code :
Using PHP :
$username = "maniacal";
echo"<p class='welcome' id='greeting'>Hi, "$username" Welcome to the site!!</p>" ;
OR Using Javascript :
<script>
document.getElementById('greeting').innerHTML="Hi <?php echo $username?>, welcome to the site";
</script>
Related
i want to pass PHP variable to Javascript array.
It is a test code, but in further i want to pass the printed product id's and name's to this ajax function to send it to cart
The code above is working without printing the button with echo.Any better ideas are welcome :)
<html>
<?php
$num=3;
echo "<button onclick='funkt('<?php echo $num ?>');'>cc</button>
";
?>
</html>
<script>
function funkt(x){
var c=x;
alert(c);
}
</script>
You weren't properly escaping the quotes inside the echo statement. Also, HTML attributes use double quotes (onclick="..."), not single quotes.
Try this:
<!DOCTYPE html>
<html>
<head>
<script>
function funkt(x) {
var c = x;
alert(c);
}
</script>
</head>
<body>
<?php
$num = 3;
echo "<button onclick=\"funkt('" . $num . "');\">cc</button>";
?>
</body>
</html>
Better option is to add an input field of type hidden and pass the number/name/ID to it. On submission of form or button click, you can fetch it wtih the field Id and pass the value to ajax.
<input type="hidden" id="number" value="<?php echo $num;?>" />
<script>
function funkt(x) {
var c = document.querySelector("#number").value;
alert(c);
}
</script>
You can try to declare variable inside "script" tag, and then use this variable in the javascript afterwards like this:
<?php $num = 5; ?>
<script>
var link_label = '<?php echo $num ?>';
//use your link_label value to load it in ajax here or in other files that has been loaded after this script
</script>
Is there a Javascript way how to simple set multiple variables like in PHP?
example in PHP:
<?php
$var1 = 'first code or text';
$var2 = 'second code or text';
...
?>
and called enywhere simply by
<?php echo $var1; ?>
I founded for Javascript only "innerHTML" approach, associated with elements/id tags
<script>
var el = document.getElementById('variable1');
el.innerHTML = 'taste text';
</script>
with
<span id="variable1"></span>
Thanks
It is ok to do that
<script type="text/javascript">
var var1="first code or text";
var var2="second code or test";
</script>
Then you display it on your page by document.write() function which is in the core of javascript you have to call it at the place you want like this
<script type="text/javascript">
document.write(var1);
</script>
I have this line of code
<script type="text/javascript">
document.title = "<?php include('pot1.php');?>"
</script>
My pot1.php gives me an echo with one random number, but that code does not seem to work and I don't know why the echo doesn't appear in my title. Is anything wrong with the code?
pot1.php code:
<?php
#include_once('link1.php');
$cg = fetchinfo("value","info","name","current_game");
$cb = fetchinfo("cost","games","id",$cg);
$cb=round($cb,2);
echo '$'.$cb;
?>
This echo is working and is giving me values because I can see them in one div.
You should just do it like this:
<?php include('pot1.php');?>
Now we know that there is a variable set. Echo that into the title tag.
<script type="text/javascript">
document.title = "<? echo $cb ?>"
</script>
The way you're doing it now... I don't think you can include an entire PHP page and expect to behave like a simple string, even if that's all it returns.
Put the value you want inside a variable in the pot1.php file and use it on the title
<?php include('pot1.php');?>
<script type="text/javascript">
document.title = "<?php echo $yourVar;?>"
</script>
I've been trying to change my modal title remotely from php. The title that I want to be set is a variable and has already been assigned but it changes when the user inputs something new.
Here is my php code:
$Studname = $results['Studentname'];
echo '<script type="text/javascript">';
echo 'alert("'.$Msg.$Studname.'")';
echo '</script>';
echo "<script type='text/javascript'>
$(document).ready(function(){
$('#MyAdvertLabel').text(".$Studname.");
});
</script>";
echo "<script type='text/javascript'>
$(document).ready(function(){
$('#myAdvert').modal('show');
});
</script>";
There seems to be no error in the code according to the program i'm using but still when I run the code there is no change in the modal title.
Your PHP will generate a Javascript error:
<?php
$Studname = 'Sample text';
$('#MyAdvertLabel').text(".$Studname.");
?>
Will generate:
$('#MyAdvertLabel').text(sample text);
The contents of the text() function need to be wrapped in quotation marks.
For example:
<?php
$Studname = 'Sample text';
$('#MyAdvertLabel').text('".$Studname."'); // Notice the ' ' wrapping the variable
?>
How can I read the session data set in PHP from Javascript. I tried the following code, but it doesn't work.
<?php
session_start();
$_SESSION['test'] ="orange";
?>
<script>
var username = '<%= Session["test"] %>';
alert(username );
</script>
Thanks for your help.
Just echo it out:
<?php
session_start();
$_SESSION['test'] ="orange";
?>
<script>
var username = '<?php echo $_SESSION["test"]; ?>';
alert(username);
</script>
BTW, you used ASP.NET inline expression tags. You can use <?= ?> for PHP if you have short tags enabled.
use:
<script>
<?php echo "var username = '".$_SESSION["test"]."';";
alert (username);
</script>
Following are your mistakes
You have written Session["test"] instead of $_SESSION['test']
you are using % symbol instead of ?
Try this:
<?php
session_start();
$_SESSION['test'] ="orange";
?>
<script>
var username = '<?= $_SESSION["test"] ?>';
alert(username );
</script>