How to pass JS variable to php? - php

I have a javascript functions which returns a hash. I need to pass this hash to php to do stuff with it. Whats the best way to do that?

Assuming you mean JavaScript function returns a hash and sends it to PHP - then AJAX

You should give more info about what exactly you are trying to do. Like this all we can do is guess and you'll get no good answers. But the usual suspects in this case are:
AJAX (or JSON)
Cookies
Hidden form fields, where you set the value via JS
Give more info and we can be more specific.

You could use a cookie. How the exchange takes place (AJAX, page reload, whatever) is up to you.
in PHP: see setcookie()
in JS: see document.cookie - or perhaps a JS library such as Dojo / jQuery.

Use AJAX. But remember, never trust data coming in from GET or POST and always run the data through a security check before using or storing it.

look into jquery, this will make your this easier!
$.get('myphp.php?senddata='+javascriptdata,function(receivedata){
alert('this is what was received' + receivedata);
});
or you could set a hidden input's value in a form and submit.

Related

How to transfer javascript data between websites?

i simply want to transfer a javascript array from one page to another javascript array on another page within the same domain. Php's get does not work because the user should not see the data and i don't know whether/how this could be done with post.
How would you solve this?
Thanks.
Yes, this is trivially easy to do. The best way is to use localStorage, which is essentially cookies for the 21st century.
So on the first page:
localStorage.setItem('data', JSON.stringify(yourdata));
And on the second page:
var yourdata = JSON.parse(localStorage.getItem('data'));
You could use sessionStorage if you only need the data to work for the current browser session.
If you need to support older browsers, there are various shims that will add in the functionality. This one looks quite good to me.
You can try to store this array in cookie using js. After receiving this array from cookie you can delete it.
if you are using jquery you can do something like this
$.post("test.php", { name: "John", time: "2pm" } );
This would be cross-site scripting (XSS), which cannot be done reliably with JS.
If the pages are on the same domain, you can use PHP $_SESSION to keep data related to the user.
If pages of same domain:
You need to post data to the other page using a form / AJAX.
If Not , With JS you cannot
You need a server side code/process to handle data like that.

How to get a value from a JavaScript variable to a PHP variable without AJAX, Jscript, HTML hidden field, or a cookie?

How to get a value from a JavaScript variable to a PHP variable without AJAX, Jscript, HTML hidden field, or a cookie ?
(from PHP to JavaScript: var javaScriptVar = "<?php echo $someVar; ?>"; )
So is there nothing like that?
thanks
You can do it like in your example, but your javascript must either be inside a script tag which is in a PHP file, or in an external 'js' file that you save with a php extension.
Or, you can set your server up so that all files with a 'js' extension also get parsed by PHP, but that's probably more than overkill.
However, since the PHP will only be executed once before the file is returned to the client, there's no way to make that dynamic. If your objective is to do something simple with data that resides on the server (and more than likely will not change between page accesses) it won't be too horrible to accomplish what you want to do that way.
There is no way to get a Javascript value from PHP, because the response line goes from server to browser, never the other way around. AJAX is your best bet in this case.
There is nothing like that. The data needs to be transfered to the server, either by a form variable or a url variable. Either with an AJAX request or a normal request. I never thought about doing it with a cookie, but I guess that is an option too.
No.
PHP is a server sided language, javascript is a client sided language.
In order for PHP to receive information, you must talk to the server. The only way to do so is to submit a form, or use AJAX.
Short answer is you can't. There are two main ways to send content to the server from the client, which are cookies and submitted data (either posted or through the querystring). If you were so inclined you could add it to a request header but that is plain obscure and would require an AJAX request.

PHP and JS test

I need to test a value in the client side, using js, before the content is loaded. Then, I want to give this value to PHP, which then should show the appropriate content depending on the value. The way I am doing it, is by posting via $.post of jquery, obtaining the value with a PHP function, storing it in $_SESSION and displaying the appropriate content.
However, since I am using $_SESSION, if the client side value changes, it requires two refreshes to show the correct content (because it looks like when jQuery is posting, it keeps loading the content and the new $_SESSION value is already late).
Should I use $_SESSION? Is there a better way to do this?
Thanks!!!
Simply use $.get of jquery and request the page with variables passed to it using traditional GET method.
Javascript side:
fetch "./data.php?param1=value1&param2=value2"
PHP Side:
$param1=$_GET['param1'];
$param2=$_GET['param2'];
use it and compile the html.

Including a php file dynamically with javascript and jquery

I want to include a php file later(dynamically), rather than at the top. All this file does is get some contents from server and stores it in a javascript variable that i later use in jquery.
basically when you click the link, i get the info, then display it to save resources because there are many links that may not be used.
Should i just do $("body").load("phpfile.php"); ? this should work but I am trying to find a more proper way because it has nothing to do with html tag like body.
You are approaching the problem in an odd way. Not to say it wouldn't work, just that you would have a hard time getting to. I would recommend using jquery's ajax with json.
Something like:
<div id="output"></div>
$(function ()
{
//$.json('urltoRequestfrom?variable1=value1');
$.post('/echo/json/',
{json: '{"name":"test"}'},
function (data)
{
$('#output').html(data["name"]); //first json object
},'json');
});
Why would you want to do this?
jQuery does indeed have a load function where you can fetch a page fragment, which is an ajax call, just use AJAX to fetch the data dynamically whenever you want, javascript can be configured to handle the data fetched however and whenever you want.
Also include a better description of your objective, as what you have described is very unclear.
Thanks and good luck,
h
Always expect the worst from your visitors. If it where possible to include a php file with javascript, it would be a huge risk.
PHP is a server side language, and is not present in the browser. Javascript is a clientside language, and does not know anything about the PHP, only about the outputted HTML.
Use an AJAX call instead, check this page for more info: http://api.jquery.com/jQuery.ajax/
Simply, you can't. Php is ran server-side. But you could use AJAX or Jquery AJAX

How do I assign javascript variable to php variable? With out submitting the form

hi friends can some one tell me How do I assign javascript variable to php variable?
I am actually getting a value through javascript and I want the javascript values to be assigned to php variables. With out submitting the form.
Unless you are doing something really esoteric (such as running server side JS and PHP):
The PHP runs, generates a response, sends it to the browser. If that response includes JavaScript then that JS will then be executed.
You can't get data from the JS back to the PHP without issuing a new HTTP request.
This could be done by setting document.location, adding an <img> element to the document with the data passed via the src attribute (in both cases including the data in a query string, posting a <form> to an <iframe>, using an XMLHttpRequest object and a host of other methods.
It really depends on what you want to achieve.
You don't, directly.
You have to use AJAX, but beware that is far more complicated than "just assigning it".
Use the jQuery selector to do that. OR use a hidden form if you don't want the user to see it.
For example, do this.
<input type="hidden" name="YOUR_OWN_UNIQUE_NAME" id="UNIQUE_ID" value >
After that, just use jQuery selector
$("#UNIQUE_ID").val("VALUE")
That's about it, not much problems!
You need to send an AJAX request containing the information to a separate PHP script that processes the data.
You can assign PHP variable to a JavaScript one:
var some = <?php echo $other; ?>
But you can't do this backwards - it is a matter of that php is run BEFORE javascript. ;]
Best way will be to send the javascript variable value to the server asynchronously before the form is submitted using AJAX, but be aware that you will only be able to bring back a variable or store the data server side via a db or text file.
what do you want to do with the data that is sent to the server?

Categories