This question already has answers here:
How to get JavaScript function data into a PHP variable
(5 answers)
Closed 8 years ago.
<script type="text/javascript">
var a=10;
</script>
<?php echo $value; ?>
I want to get the value of variable "a" to PHP variable "$value" without ajax request.
javascript is variable value you can't store it to php variable the reason behind is php is a server site language and javascript is only a client language both are independent.
for this you can use ajax to send the ajax request to a page with the javascript variable value and there page you can use php code to get the value of that variable like $_GET['a'].
may this help you!
PHP runs on the server side, while JavaScript runs in the client's browser. This means that the PHP code gets executed before the JavaScript, so it isn't possible to directly pass a variable from JavaScript to PHP.
You should look into either sending it via a GET or POST request or, if you need to do it without a page reload, using AJAX. This tutorial might be helpful:
http://www.w3schools.com/jquery/jquery_ajax_get_post.asp
The Javascript is executing on the browser and the PHP is executing on the server so you will have to pass the value to the server somehow. Putting it in the querystring is one easy way, for example:
var url = 'http://mysite/file.php?value=' + escape(a);
It depends on what you are doing of course, but if you are making ajax calls or just posting form values, you need to send the value in the server requests.
You could do this in one of two ways, either by performing a POST or GET request, or by using a cookie. Both would require a the page to reload however because PHP runs before the page is loaded.
Related
I want to pass variable to C# method from JavaScript is it possible ?
I had tried below code code:=
<script type="text/javascript">
$(document).ready(function () {
debugger;
var Query = window.location.search;
var i = "<%=QueryStringModule.Decrypt(Query)%>"
});
</script>
but i am getting the name "Query" doesn't exist in the current context
Please help me in the same
This is not possible. C# is code behind, so runs on the server. Any input is generated before the page is build on screen. So passing values from Javascript to C# is the other way around. Look into ajax to send values to the server so they can be parsed.
You cannot pass JavaScript variables to the server side code in that way. JavaScript is client side, which means that the server side code has already run before JavaScript starts executing. To pass a variable back to the server, you will need to submit the variable to the server. You can use an AJAX request to do this without having to load the page again.
I want store clickede_id value into $id2[] give me some suggestions and also suggest some advance details
function yes(clicked_id)
{
var it1=clicked_id;
alert(it1);
var tt1=1;
var tt2= "<?php echo($id2[var it1]); ?>";
//var tt2=document.getElementById("idcheck").value;
alert(tt2);
var tt3=document.getElementById("idcheck1").value;
//alert(tt3);
}
When you develop a web application, you are creating tools to let client and server communicates (over the HTTP protocol). This communication is based on Requests and Responses.
The client send request to server and the server responds with a reponse. In your case, you choosed PHP as the server-side language that will create your responses as answers to client request. Thes responses are HTML (+ javascript). Anyways, the reponses are static stuff to be interpredted by the client.
So the code you have sent is seen by the browser as:
function yes(clicked_id)
{
var it1=clicked_id;
alert(it1);
var tt1=1;
var tt2= 3; // or whatever value returned by php
var tt2=document.getElementById("idcheck").value;
var tt3=document.getElementById("idcheck1").value;
// ajax call here
}
When you say : store data from javascript to php (even if it doesnt look as a correct senetence), you mean sending data from client to server. It can be done via a classical Post request (via form submit with page refresh) or via ajax without page refresh.
For ajax, please to check jQuery documentation for $.ajax function (if you want to have a cross browser compatible solution), or XMLHTTPRequest object if you want raw javascript and do the cross-browser compatibility yourself.
PHP executes in the server and send the result to your browser to display. Your JS executes at this browser stage. What you need to understand is that PHP has already been finished it's execution when your JS gets a chance to execute. Trying to change something in PHP through the JS is trying to access the past.
But, the good news is, that you can adopt a model where you feed your JS through the PHP script (look at this echo "<script>var s = 'from php'</script>") and JS feeds your NEXT php execution. You can use ajax or direct page calling for this.
Probably you should read this question: How to pass data from Javascript to PHP and vice versa?
This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 7 years ago.
My web app sends data from PHP to javascript as a JSON string.
To avoid writing the string as text in the rendered file, the architecture I thought of using is setting data on cookies with PHP, then reading with JS.
It works well so far, but I was thinking if maybe users have cookies disabled, then it won't work.
So I have two questions, one, if users do disable cookies much or if it's ok to use cookies as my data-keeping method.. is there a study about cookie-disabling behaviour? I googled but couldn't find any recend data.
Second, is there another way to send data from PHP (I'm using laravel) to javascript without having to write it out on the file? (I can't make another ajax request to the server and then load the data, as it would kill user interaction, the data must return with the first request)
Thanks
First almost no one disables cookies. If you disable cookies, half of the websites you visit won't work anymore.
If you want to "pass data to javascript" without writing to a file (usually you never write to a file to pass data to javascript anyway) you can simply do:
<script>
var mydata = '<?=$mydata?>'; # take care of escaping single quote of course
</script>
In this way you don't make an additional AJAX request.
If you are using Laravel's blade then:
<script>
var mydata = '{!! $mydata !!}'; # take care of escaping single quote of course
</script>
As pointed out in comments, if you are passing json then simply remove the quote:
<script>
var mydata = {!! $mydata !!};
</script>
This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 7 years ago.
I'm trying to include JavaScript variables into PHP code as PHP variables, but I'm having problems doing so. When a button is clicked, the following function is called:
<script type="text/javascript">
function addTraining(leve, name, date)
{
var level_var = document.getElementById(leve);
var training_name_var = document.getElementById(name);
var training_date_var = document.getElementById(date);
<?php
$result = "INSERT INTO training(level, school_name, training_date) VALUES('level_var', 'training_name_var', 'training_date_var')" or die("Query not possible.");
?>
</script>
Is it possible?
PHP is run server-side. JavaScript is run client-side in the browser of the user requesting the page. By the time the JavaScript is executed, there is no access to PHP on the server whatsoever. Please read this article with details about client-side vs server-side coding.
What happens in a nutshell is this:
You click a link in your browser on your computer under your desk
The browser creates an HTTP request and sends it to a server on the Internet
The server checks if he can handle the request
If the request is for a PHP page, the PHP interpreter is started
The PHP interpreter will run all PHP code in the page you requested
The PHP interpreter will NOT run any JS code, because it has no clue about it
The server will send the page assembled by the interpreter back to your browser
Your browser will render the page and show it to you
JavaScript is executed on your computer
In your case, PHP will write the JS code into the page, so it can be executed when the page is rendered in your browser. By that time, the PHP part in your JS snippet does no longer exist. It was executed on the server already. It created a variable $result that contained a SQL query string. You didn't use it, so when the page is send back to your browser, it's gone. Have a look at the sourcecode when the page is rendered in your browser. You will see that there is nothing at the position you put the PHP code.
The only way to do what you are looking to do is either:
do a redirect to a PHP script or
do an AJAX call to a PHP script
with the values you want to be insert into the database.
<script type="text/javascript">
var jvalue = 'this is javascript value';
<?php $abc = "<script>document.write(jvalue)</script>"?>
</script>
<?php echo 'php_'.$abc;?>
You seem to be confusing client-side and server side code. When the button is clicked you need to send (post, get) the variables to the server where the php can be executed. You can either submit the page or use an ajax call to submit just the data.
-don
PHP runs on the server. It outputs some text (usually). This is then parsed by the client.
During and after the parsing on the client, JavaScript runs. At this stage it is too late for the PHP script to do anything.
If you want to get anything back to PHP you need to make a new HTTP request and include the data in it (either in the query string (GET data) or message body (POST data).
You can do this by:
Setting location (GET only)
Submitting a form (with the FormElement.submit() method)
Using the XMLHttpRequest object (the technique commonly known as Ajax). Various libraries do some of the heavy lifting for you here, e.g. YUI or jQuery.
Which ever option you choose, the PHP is essentially the same. Read from $_GET or $_POST, run your database code, then return some data to the client.
I had the same problem a few weeks ago like yours; but I invented a brilliant solution for exchanging variables between PHP and JavaScript. It worked for me well:
Create a hidden form on a HTML page
Create a Textbox or Textarea in that hidden form
After all of your code written in the script, store the final value of your variable in that textbox
Use $_REQUEST['textbox name'] line in your PHP to gain access to value of your JavaScript variable.
I hope this trick works for you.
You can take all values like this:
$abc = "<script>document.getElementByID('yourid').value</script>";
You can do what you want, but not like that. What you need to do is make an AJAX request from JavaScript back to the server where a separate PHP script can do the database operation.
Client goes to example.com/form.html where a html POST form is displayed
Client fills the form with specific information and submit it to example.com/form.html
When example.com/form.html receives the POST request redirects the Client on example.com/redirected.html
Is possible to retrieve the variables that the client filled and POSTed to example.com/form using javascript ? The javaScript being deployed on example.com/redirected.html only . I presume that can be some "back" controls iframes and ajax involved but I couldn't find a reliable solution yet.
The operation will take place on the same domain so no cross domain issue is involved
Nope, I don't think this is possible.
You have to either
Store the posted value in a cookie
Store the posted value in a session variable on server side
Add the posted value as a GET parameter to the new URL
Use GET to post the original form, and painfully extract the posted value from document.referer on the new page
With HTML5, use localstorage. (The answer describes how to store object in localstorage- you could store an object of your form fields in there).
But you have to store the data on posting with js at example.com/form.html and then can get it on example.com/redirected.html. Without js at form.html, this is not possible with this method.
This works if you plan to use html5 and do not store too much data in it, iirc it has a limit of 5-10mb depending on the browser.
I don't think there is a way to do this by using plain html. With some server-side language (like PHP) it can be done with no problem.
I have been in a similar situation before, and the way I managed to give the data to JS is by including the data in a tag while preparing the output using PHP.
Assuming the redirected to php script receives the POST data from the script it's being redirected in. I would include this in the php code:
<?php
echo '<script type="text/javascript">';
echo 'var postData = '.json_encode($_POST).';';
echo '</script>'
?>
This will have the javascript know what the POST values contained.
To access the values from js:
//assuming you need the value for $_POST['foo']
var foo = postData.foo;
// or if json is encoded as just an associative array
var foo = postData['foo'];
If the POST data is not being passed to the redirected to script (haven't checked if this happens automatically), you could write the $_POST data in a $_SESSION variable, in the first php script:
<?php
$_SESSION['postdata']=$_POST;
?>
and then get it back from SESSION from the redirected to script.
<?php
$postdata = $_SESSION['postdata']; //use this to create the inline script in the html
unset($_SESSION['postdata']; //empty this from the SESSION variables
?>