Pass variable from php to javascript function argument - javascript

I want a variable that is in php to javascript function argument, but I do not get :( anyone can help me ..
//file js.js
function ComputeTotalPrice(first_meters_price){
alert(first_meters_price);//result undefined
}
<!DOCTYPE html>
<?php
...
$first_meters_price = price_type(get_geo_id("Rural"), //returns 4
?>
<script src="js.js" >
ComputeTotalPrice(<? echo $first_meters_price; ?>);
</script>
<head>
...
</head>
<body>
<div id='reserva'>
...
</div>
</body>
</html>

Try something like this:
<?php $myVar = 5; ?>
<head>...</head>
<body>
<script>
var myVarFromPHP = <?php echo $myVar; ?>;
</script>
...
</body>
Try seeing this stackoverflow answer as well

As others have mentioned and linked to external answers, content inside a script tag is unreliable and even prohibited in some browsers when a src attribute is included; as such, it should be avoided.
For this to operate properly, one approach would be to pull the function from js.js into the script tag like below. It's undesirable but it does function as needed.
<?php $first_meters_price = 4; ?>
<script type="text/javascript">
function ComputeTotalPrice(first_meters_price) {
alert(first_meters_price);
}
ComputeTotalPrice(<?php echo json_encode($first_meters_price); ?>);
</script>

like this works.. but is ot the best way to do this..
<!DOCTYPE html>
<html>
<body>
<?php $var_to_js = function_php();?>
<input type='text' class='hide' id='var_to_js' value='<?php echo $var_to_js; ?>'/>
<script>
var var_from_php = document.getElementById('var_to_js').value;
</script>
</body>
</html>

Related

How to append javascript variable inside PHP tag?

I am trying to write a javascript function inside PHP tag, but I do not know the exact syntax. Can anyone help? I am trying to concat the variable named "var1". But I am getting error.
index.php
<?php
echo '<script>
demo(1);
function demo(var){
const var1=1;
const ptag=document.getElementById("content"+var1);
}
</script>';
?>
I think you want this code :
<!DOCTYPE html>
<html>
<body>
<p id="content1">1th</p>
<p id="content2">2th</p>
<p id="content3">3th</p>
<p id="content4">4th</p>
<p id="content5">5th</p>
<?php
echo "
<script type=\"text/javascript\">
function demo(num){
const paragraphTag = document.getElementById('content'+ num);
}
demo(1)
</script>
";
?>
</body>
</html>
what is your mistakes :
it's better to use function after declaration
you can't use var as a name of variable it's reserved key
I think you want to pass number to function and use that number for getting specific p element, if it is so you don't need var1
finally use single quotes " for the php echo and single quotes ' for content
and maybe you need this code:
<!DOCTYPE html>
<html>
<body>
<p id="content1">1th</p>
<p id="content2">2th</p>
<p id="content3">3th</p>
<p id="content4">4th</p>
<p id="content5">5th</p>
<button onclick="demo()">click here</button>
<?php
$num = 1;
echo "
<script type=\"text/javascript\">
function demo(){
const paragraphTag = document.getElementById('content'+ $num);
paragraphTag.textContent = 'selected'
}
</script>
";
?>
</body>
</html>

Cannot update html text from script in php echo

I have some code like this:
<?php
echo "<script> alert('hello world!!!'); </script>";
echo "<script> document.getElementById('updatetext').innerHTML='heyatest'; </script>";
?>
<html>
<head>
</head>
<body>
<p id="updatetext">Some Text Here</p>
</body>
</html>
I need it to work this way and update the text at certain point through PHP code. In above the alert popup shows, but my text does not get updated with "heyatest"
You're calling the javascript that tries to find your id updatetext before that object exists. It cannot find it. Call the php after the html object you're looking for.
<html>
<head>
</head>
<body>
<p id="updatetext">Some Text Here</p>
</body>
</html>
<?php
echo "<script> alert('hello world!!!'); </script>";
echo "<script> document.getElementById('updatetext').innerHTML='heyatest'; </script>";
?>
The Javascript isn't calling an initiated object. Try it like this:
<html>
<head>
</head>
<body>
<p id="updatetext">Some Text Here</p>
<?php
echo "<script> alert('hello world!!!'); </script>";
echo "<script> document.getElementById('updatetext').innerHTML='heyatest'; </script>";
?>
</body>
</html>

self submit form acces variables javascript

I'm making a site for some peaple of my class and I want them to be able to login. I don't want to make an extra loginpage, so I created this bit of code for my login system, but it doesn't work. I looked al over the internet to find other soloutions, but with no success... I want the person to type in their username and password and I want to store that information in a javascript function, so that I can do a function like tis:
function passCheck () {
if (pass="johndohpassword1234") // userbname johndoe and passsword password1234
{
document.getElementById('h').innerHTML= "welcome, John!";
}
this is my code:
<?php
$id = $_POST[username] . $_POST[password];
?>
<html>
<head>
<title>Login</title>
<script type="text/javascript" src="script.js">
var pass=" <?php echo "$id"; ?>";
window.alert( "welkom, " + pass );
</script>
</head>
<body>
<h1 id="h">Login</h1> <br>
<form action="http://timkast.tk" method="post">
<b>Username:</b><input type="text" name="username"> <br>
<b>Password:</b><input type="password" name="password"><br>
<input type="submit"></input>
</form>
</body>
</html>
everything works fine, but the code for window.alert does nothing and I dont know why, please can someone explain/fix this?
When you specify a src for the script tag, it ignores the contents of the script tag.
As specified in the documentation:
If a script element has a src attribute specified, it should not have a script embedded inside its tags.
You can't write code into a <script> tag with reference to external file <script src="...">.
You have:
<script type="text/javascript" src="script.js">
var pass=" <?php echo "$id"; ?>";
window.alert( "welkom, " + pass );
</script>
And you need:
<script type="text/javascript" src="script.js"></script>
<script>
var pass=" <?php echo "$id"; ?>";
window.alert( "welkom, " + pass );
</script>

JQuery POST data to php, direct to php, data not exist anymore in php

I passed along data via POST to php using JQuery, but when the page direct to the php file, the POST["createVal"] in php doesn't seem to exit after the call back. I showed this in demo below, notice that the $name is undefined when the callback result is clicked. Any idea how to fix this?
I'm trying to make a function that when the returned result was clicked, html page could redirect to the php page in which user input in html file could be printed out.
HTML file
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>
<body>
<input type="text" id="userinput">
<div id="result">
</div>
</body>
<script>
$("input").keyup(function(){
var input=$("input").val();
$.post("process.php",{createVal:input},function(data){
$("#result").html("<a href='process.php'>"+data+"</a>");
})
})
</script>
</html>
php file(process.php)
<?php
if(isset($_POST["createVal"])){
$name=$_POST["createVal"];
echo $name;
}
?>
<?php
echo $name;
?>
change
$("#result").html("<a href='process.php'>"+data+"</a>");
to
$("#result").html("<a href='process.php?createVal="+data+"'>"+data+"</a>");
and
process.php
if(isset($_REQUEST["createVal"])){
$name=$_REQUEST["createVal"];
echo $name;
}
Use this Html Code:
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
</script>
</head>
<body>
<input type="text" id="userinput">
<div id="result"> </div>
</body>
<script>
$("input").keyup(function(){
var input=$("input").val();
$.ajax({
type: "POST",
url: "http://localhost/stackoverflow/process.php",
data: {'createVal': input},
success: function(data){
$("#result").html("<a href='http://localhost/stackoverflow/process.php?createVal="+data+"'>"+data+"</a>");
}
});
});
</script>
</html>
PHP Code:
<?php
if(!empty($_REQUEST['createVal']) || !empty($_GET['createVal'])){
$name = $_REQUEST['createVal'];
echo $name;
}elseif(!empty($_GET['createVal'])){
$name = $_GET['createVal'];
echo $name;
}
return 1;
?>
I have run and checked this too.
localhost: if you are running this code on localhost
stackoverflow: is the folder name, if you have any folder in localhost for it so replace the name by this.

How to pass php variable to my script code and use document.write to display it?

This is my html code.
<!DOCTYPE HTML>
<?php
$php_var = "Hello world from PHP";
?>
<html style="margin-top:-8px; margin-left:-8px;">
<head>
<meta charset="utf-8">
<title>Pure-Convert</title>
<script src="jquery.js"></script>
<link rel="stylesheet" type="text/css" href="calchome.css">
<script src="4func.js" type="text/javascript"></script>
<script src="converter.js" type="text/javascript"></script>
<script>
var js_var = <?php echo json_encode($php_var); ?>;
document.write(js_var);
</script>
</head>
<body >
<form action="cool.php" method="post">
<input name ="username" placeholder="Name">
<input name= "comment" placeholder="Make your mind heard..." />
<input type ="submit">
</form>
</body>
</html>
Basically what I want to happen is I want $php_var to be converted to javascript. This what I have attempted. Can someone help me accomplish this?
<script>
var js_var = <?php echo json_encode($php_var); ?>;
document.write(js_var);
</script>
Try
<script>
var js_var = "<?php echo $php_var; ?>";
document.write(js_var);
</script>
You don't need document.write(js_var);. What it does is writes that value to the DOM/HTML, possibly messing up everything (depends on the value).
<? php echo( '<script>var my_var=' . $my_value . ';</script>'); ?>
// In further js:
console.log( my_var );

Categories

Resources