Setting PHP variable to JavaScript code? [duplicate] - php

This question already has answers here:
passing PHP objects to javascript [duplicate]
(4 answers)
Closed 9 years ago.
I need to change the value of a PHP variable to JavaScript code.
The value of the PHP variable has to be something out of (JavaScript) localStorage.
So it is something like this.
$variable = localStorage.anameofalocalstorage; //The localstorage part is js
How can I do this?

This is an example code using jquery for the ajax call.
<?php
$variable_that_should_be_set=isset($_GET['shoudBeSet'])?$_GET['shoudBeSet']:'notSet';
?>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
</head>
<body>
<div id="div1"><?php echo $variable_that_should_be_set;?></div>
Click here to Set
<script>
$("#button").click( function () {
$.get("?shouldBeSet=this_is_set")
})
</script>
</body>
</html>

Related

How to Read PHP Inside Javascript [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 8 years ago.
How do I get values of HTML form value and pass it in PHP code inside javascript?
I need to call a PHP function inside a javascript.
Please kindly help me fix my code.
Thank you very much.
<script>
function Send() {
var abc="<?php echo mail_message.val(); //Email functionality here.. ?>";
document.write(abc);
}
</script>
<body>
<form>
<label class="email_label" for="mail_message">Message</label>
<textarea rows="7" cols="78" name="mail_message" id="mail_message">Enter your message here...</textarea>
<form>
</body>
PHP runs before your JavaScript gets loaded, so if you need to send data to PHP you need an AJAX request:
$.post('script.php', {
value: $('#mail_message').val()
}, function() {
// success?
});
Your script.php will then read $_POST['value'] and process it.
You can't use javascript val() in php syntex
instead use the following code to get the php value in javascript
<script>
function Send() {
var abc=document.getElementById('mail_message').value;
document.write(abc);
}
</script>

sending javascript variable value to php within the same page [duplicate]

This question already has answers here:
How to get JavaScript variable value in PHP
(10 answers)
Closed 9 years ago.
I just want to know if sending value of a javascript to php is possible.
here is the simple code of what i am trying to say.
<script language="javascript" >
var id = "data"
</script>
<?php
$getthevalueofid = var id;
?>
thanks!
Not the way your code sample is structured, no. PHP is a server-side language and Javascript is a client-side language. PHP is out of scope by the time any client-side script executes.
If you need to pass an object from the browser to your server you can use XMLHttpRequest (commonly referred to as AJAX).
To send a Javascript variable back to PHP, you need to do an AJAX request:
<script language="javascript" >
xmlhttp.open("GET","getvalue.php?id="+id,true);
xmlhttp.send();
</script>
And in getvalue.php you'd have:
<?php
$getthevalueofid = $_GET['id'];
?>
No way to do this. Use Ajax to pass javascript variable to php

Pass Session Variable to javascript [duplicate]

This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 8 years ago.
I'm tring to pass a session variable to javascript but not have success.
Searching stackoverflow i have found this discussion:
Passing Session variables to javascript
in my custom.js i have on header
<?php session_start(); ?>
.. /// js code
strActionPage = CurrentPath + "upload_file.php?ation=store&session_user_id=<?php echo $_SESSION['session_user_id']; ?> "; //the ActionPage's file path
but i cant get session variable from upload_file.php page
where do I wrong?
Tks to all
Wait, you're using PHP on a JavaScript file (that's what your OP says)? Not gonna work. You're going to have to pass it to a JavaScript function as a parameter like so:
<?php
session_start();
// HTML and stuff
<script type="text/javascript" src="custom.js"></script>
<script type="text/javascript">
passSession("<? echo $_SESSION['session_user_id']; ?>");
</script>
It's hard to say without knowing more about your particular error, but I did notice what seems like a typo in the code you posted:
strActionPage = CurrentPath + "upload_file.php?ation=store&session_user_id=<?php echo $_SESSION['session_user_id']; ?> "; //the ActionPage's file path
You have ation-store -- did you mean to write action=store?
This is how I might do.
<script type="text/javascript">
var user_id=<?php echo $_SESSION['session_user_id']; ?>;
strActionPage = CurrentPath + "upload_file.php?action=store&session_user_id="+user_id;
</script>
There is also a possible mistake. You are using "ation" . Should that be 'action'?

Passing Vars from Javascript to PHP [duplicate]

This question already has an answer here:
Pass Javascript var into PHP var
(1 answer)
Closed 9 years ago.
Is this how you would pass a var from JS to PHP? or is there a better solution. Please could you help with the easiest and most efficient way.
<!DOCTYPE html>
<html>
<body>
<script>
var name="John Doe";
document.write(name + "<br>");
</script>
<?php
$name = <script>document.write(name);</script>
?>
</body>
</html>
I know this won't work, but how would I do it?
<?php
$name = <script>document.write(name);</script>
?>
You can not do this
As php is a server side language and js client side,
First it run in server then it output to client were it executes js. So you can not pass values from js to php like in the code above.
If you want to do that you need to use AJAX.
please try this:
<?php
echo "<script language=\"javascript\">
var name=\"John Doe\";
document.write(name + \"<br>\");
</script>";
?>
i hope this would be useful

Dynamically passing PHP variables [duplicate]

This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 8 years ago.
I'm trying to accomplish a JavaScript form that will load the result from a PHP request on an external website, after a text box is filled out.
Right now, I have
<script type="text/javascript"/>
function sendrequest() {
document.location="myphpsite.com/submit.php?tb="+document.getElementById("tb").value;
}
</script>
<form>
<input type="text" id="tb" onChange="sendrequest()" />
</form>
This code is going nowhere, because from my current knowledge, you can only load PHP variables on startup, by using "script src=... ", and by having the php page return some javascript code.
What I'm looking for is a way to request/receive php variables dynamically, something that would work like this:
<script type="text/javascript">
function getdata() {
return document.get("myphpsite.com/submit.php?tb=hi");
}
</script>
In other words, is there a function that dynamically "reads" a website, and returns the raw text?
http://w3schools.com/ajax/default.asp
You should go read this and then read it again, AJAX is what you need.
best example with PHP and AJAX -
http://www.w3schools.com/php/php_ajax_database.asp

Categories