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¶m2=value2"
PHP Side:
$param1=$_GET['param1'];
$param2=$_GET['param2'];
use it and compile the html.
Related
Hi I'm just wondering if it's possible to store the custom data attribute of an element with PHP into a variable and use that variable after refreshing the page?
I am trying to store this <a> tag's data attribute within a variable when it is pressed.
Here is an example of the element:
<a href="/book-testdrive" class="addtocart" data-value="NH06LKO" title="Book Test Drive">
<span> Book Test Drive</span>
</a>
So I'm wondering if I can post the value of the data-value attribute when the <a> tag is clicked to a variable and store it on the server so it can be used after a page reload.
Is this possible with PHP?
Thanks, Nick
PHP can't control what happens to your page after it renders it, therefore you have no way of accessing any values from PHP alone.
To continue beyond your question:
What you could do is use javascript (preferably jquery) and send the data via XHR request and set up php code which will receive it. Or you could store the data into a cookie, localStorage, sessionStorage or url, but this depends on your target browser support. All these methods will work in IE8+ for sure.
I can't advise you on which method is best, since I do not know what is your intent. Perhaps the easiest (probably not the best) option would be to set up a script to read the data via $_GET/$_POST and store the value in $_SESSION. Then on the front-end send the data with javascript XHR request or jQuery's $.ajax (wrapper for much easier XHR requests) to your php script.
Since I do not know what you're trying to do, I will not attempt to write any code for this. I suggest you to read or watch some php and javascript tutorials and documentation to learn about what I've just mentioned, if you don't know about it already.
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.
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
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.
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?