This question already has answers here:
How do I pass JavaScript variables to PHP?
(16 answers)
Closed 8 years ago.
when I click the detail link the value of $record[id] has been sent to showUser the value store in variable e in javascript I need this value of e in php how can I get the value of e in php.
echo
"<td>"."
<a href='#?value=$record[id]' onClick='showUser($record[id])'; >Detail</a>
"."</td>";
<script>
function showUser(str) {
var e=str;
}
</script>
Try this
echo "<td>"."<a href='#?value=".$record[id]."' onClick='showUser(".$record[id])."'; >Detail</a> "."</td>";
That should get the variable inside the showUser onClick function.
Javascript variable can hold php varaible's value since js loads on client
var a = <?php echo $vari;?>
but php variable cant hold JavaScript(unless you are not sending with ajax to server) as php loads first and manipulated on server not on client
In your case you can pass $record[id]) in onClick function
"<td>"."<a href='#?value=$record[id]' onClick='showUser(".$record[id])."';
You cannot pass javascript variable value to the php variables on the same page, as PHP code runs at the server side and it knows nothing what is going on client side.
You should pass variables to PHP code from html-form using some other way, such as Ajax.
Ajax is the best way to do it.
Thanks....
you can pass it through an ajax call in this way:
- step 1:
$.get('name_of_php_file_you_want_to_passvalue.php?e='+e,function(data) {
//you can do anything here with the php returned value.
})
- step 2:
And then in the php file to get that variable:
$e=$_GET['e'];
Related
This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 6 years ago.
I have the php page with javascript code inside it.
If I have php variable retrieved by back-end server, can it be passed in javascript code?
For instance, if I want to check the flag(php variable) inside the javascript code, do I need to hardcode it inside the javascript?
Below are the "checkServiceCategory"? (Should it be Dynamic or static)
<?php
foreach ($result as $id=>$value) {
if($value[0]->isServiceCategory)
return true;
}
OR
isEmptyUploadFile(function(r)
{
var checkServiceCategory=document.getElementById('category-group').value;
if(checkServiceCategory!=85 && (fileList == null || fileList.length == 0))
{
$("#uploadImgError").html('<em><span style="color:red"> <i class="icon-cancel-1 fa"></i> Please Upload at least one image!</span></em>');
location.href = "#uploadImgError";
return false;
}
else
Yes you can use php variables inside the javascript code and php variable is dynamic as
<?php $your_variable = "some value"; ?>
<script>
var js_variable = '<?php echo $your_variable; ?>';
alert(js_variable);
</script>
You can write PHP data into JavaScript before that JavaScript is loaded. So, for example:
<script>
var count = 0;
<?php
echo 'count = 5;';
?>
console.log(count); // Will give 5
</script>
You can do this because the PHP is run on the page before it is ever sent to the client. However, once the code is sent to the client, JavaScript and PHP cannot talk to each other. What you can do at that point is make AJAX requests via JavaScript from your client back to your server, and use PHP to respond to the AJAX request.
You can pass value from php to js like this.
var js_var = '<?php echo $some_php_variable; ?>';
This question already has answers here:
How do I pass JavaScript variables to PHP?
(16 answers)
Closed 3 years ago.
First I thought that I had to convert JavaScript to PHP, but then I found out that I cannot because of server and client side executions. So now I simply want to send ONE variable
<script type="text/javascript">
function scriptvariable()
{
var theContents = "the variable";
}
</script>
to a PHP variable
<?php
$phpvariable
?>
That function in the JavaScript executes when let's say I click on a button.
Now I have no idea on how to assign that phpvariable to the JavaScript one to use the phpvariable to look up stuff in my database. I know I can add it to my url or some thing and refresh the page, but I would like to do it with AJAX as I might have to use this Ajax method further in my webpage.
So is there an easy way to do this without having to dump pages of code on my page to do one simple thing?
PHP runs on the server and Javascript runs on the client, so you can't set a PHP variable to equal a Javascript variable without sending the value to the server. You can, however, set a Javascript variable to equal a PHP variable:
<script type="text/javascript">
var foo = '<?php echo $foo ?>';
</script>
To send a Javascript value to PHP you'd need to use AJAX. With jQuery, it would look something like this (most basic example possible):
var variableToSend = 'foo';
$.post('file.php', {variable: variableToSend});
On your server, you would need to receive the variable sent in the post:
$variable = $_POST['variable'];
As Jordan already said you have to post back the javascript variable to your server before the server can handle the value. To do this you can either program a javascript function that submits a form - or you can use ajax / jquery. jQuery.post
Maybe the most easiest approach for you is something like this
function myJavascriptFunction() {
var javascriptVariable = "John";
window.location.href = "myphpfile.php?name=" + javascriptVariable;
}
On your myphpfile.php you can use $_GET['name'] after your javascript was executed.
It depends on the way your page behaves. If you want this to happens asynchronously, you have to use AJAX. Try out "jQuery post()" on Google to find some tuts.
In other case, if this will happen when a user submits a form, you can send the variable in an hidden field or append ?variableName=someValue" to then end of the URL you are opening. :
http://www.somesite.com/send.php?variableName=someValue
or
http://www.somesite.com/send.php?variableName=someValue&anotherVariable=anotherValue
This way, from PHP you can access this value as:
$phpVariableName = $_POST["variableName"];
for forms using POST method or:
$phpVariableName = $_GET["variableName"];
for forms using GET method or the append to url method I've mentioned above (querystring).
I use DHTMLX. I want to use the ajax component get the data from php, the php will create the xml. How to get this xml data into javascript.
enter code here
dhtmlxAjax.get("php/getUsername.php", function(r){
r = r.xmlDoc.responseXML; // will give you DOM object
//console.log(r.firstChild.tagName);
alert(r.firstChild.tagName("param1"));
});
thank you
Just use the php echo function to echo the php variable into a java script variable.
eg. If you have a php variable called $XMLData you would do the following
var XMLdata = <?php echo $XMLData; ?>
Then on page load your php variable becomes a javascript variable and can be used. from anywhere in javascript provided that the variable is within its scope.
This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 7 years ago.
I am facing issue regarding passing javascript variables to php within same function. My code looks like this
else if(msg_type[i] == 'code' ){
var code_action = 'test';
<?php
function foobar_func(){
return "<script>document.writeln(action[i]);</script>";
}
add_shortcode( 'foobar', 'foobar_func' );
?>
}
what i am doing is passing that code_action variable of javascript and returning it to function without using ajax or jquery...is there any possible method to do so...??
any possibilities will be appreciated. Thank you
When you make a request to invoke this page, your <?PHP ?> block is executed in the server. It creates a processed output in the web page (in your code) and send it to the browser. Now the browser executes your <script> </script> blocks.
I hope you understand that passing variables from <script></script> to <?PHP ?> is impossible since the latter happened in the past.
But you can use it other way around by trying to pass variables from the PHP to JS.
When you write
<?PHP
$fromServer = "from php";
echo "<script> var fromServer = " . $fromServer . " </script>
?>
.. it passes data from your PHP to JS.
You can, however, use HTTP POST or GET method to pass data from JS to PHP to be used in the next session.
This question already has answers here:
How do I pass JavaScript variables to PHP?
(16 answers)
Closed 3 years ago.
First I thought that I had to convert JavaScript to PHP, but then I found out that I cannot because of server and client side executions. So now I simply want to send ONE variable
<script type="text/javascript">
function scriptvariable()
{
var theContents = "the variable";
}
</script>
to a PHP variable
<?php
$phpvariable
?>
That function in the JavaScript executes when let's say I click on a button.
Now I have no idea on how to assign that phpvariable to the JavaScript one to use the phpvariable to look up stuff in my database. I know I can add it to my url or some thing and refresh the page, but I would like to do it with AJAX as I might have to use this Ajax method further in my webpage.
So is there an easy way to do this without having to dump pages of code on my page to do one simple thing?
PHP runs on the server and Javascript runs on the client, so you can't set a PHP variable to equal a Javascript variable without sending the value to the server. You can, however, set a Javascript variable to equal a PHP variable:
<script type="text/javascript">
var foo = '<?php echo $foo ?>';
</script>
To send a Javascript value to PHP you'd need to use AJAX. With jQuery, it would look something like this (most basic example possible):
var variableToSend = 'foo';
$.post('file.php', {variable: variableToSend});
On your server, you would need to receive the variable sent in the post:
$variable = $_POST['variable'];
As Jordan already said you have to post back the javascript variable to your server before the server can handle the value. To do this you can either program a javascript function that submits a form - or you can use ajax / jquery. jQuery.post
Maybe the most easiest approach for you is something like this
function myJavascriptFunction() {
var javascriptVariable = "John";
window.location.href = "myphpfile.php?name=" + javascriptVariable;
}
On your myphpfile.php you can use $_GET['name'] after your javascript was executed.
It depends on the way your page behaves. If you want this to happens asynchronously, you have to use AJAX. Try out "jQuery post()" on Google to find some tuts.
In other case, if this will happen when a user submits a form, you can send the variable in an hidden field or append ?variableName=someValue" to then end of the URL you are opening. :
http://www.somesite.com/send.php?variableName=someValue
or
http://www.somesite.com/send.php?variableName=someValue&anotherVariable=anotherValue
This way, from PHP you can access this value as:
$phpVariableName = $_POST["variableName"];
for forms using POST method or:
$phpVariableName = $_GET["variableName"];
for forms using GET method or the append to url method I've mentioned above (querystring).