Sending JavaScript variable to PHP - php

I have to send a value that is stored in a JavaScript variable to a PHP page. The PHP page is in a different folder than the JavaScript page.
This JavaScript variable is in a method that fires when we click a button.
How can I send that variable value to the PHP page?
(This is an Eclipse project.)

The easiest way to do this is to use jQuery's POST method to send serialized data to a PHP page that can process your request.
However, you didn't say if you want the client to cache the value and always remind server of the value for all future requests. If so, you can once again use JavaScript to store values inside the cookie, so that every time user click on something (GET / POST) it will also send out the cookie containing your javascript value to the server

Using Ajax

Do you submit a HTML form when the button is clicked. If yes then you can set the javascript variable value into a hidden field of the form on button click. After which your PHP code and read its value on the server-side.

Related

Getting selected text

I need to get a text that the user has select with pointer and put it to some variable in PHP. So, the user should select some text in one div, then click submit button (in another div), and then I need to store that part of the text to some variable. Can anybody help me?
You would have to set an onclick listener (I prefer to use JQuery for this) so that when the user clicks the submit button, you grab the selected text from the div. You could probably do that in a way found here:
Get the Highlighted/Selected text
Since javascript and HTML are front-end and PHP is back-end, you cannot simply place the selected value into a PHP variable. Instead, you can use an ajax request to send the data to a PHP function. I'm not sure what you are doing with it after that, but sending an ajax request and including the selected text in the data section of the request, and then retrieving it in your PHP file/function and storing it in a variable there. This question should help in that regard: Passing Javascript variable to PHP using Ajax

Change php variable value with jquery

I have the following issue. I am building a form in which a user can select a value from a select option and i want to save that value into a php variable. The form and everything are in the same php file. Is there anyway to achieve something like that with jquery?
I do not want to submit the form, just to change the value of php variable.
Thanks
Your script is no longer running, when the user received the HTML-Form, since the request ended. Thus, there is no variable which you could set.
However, you can send a new request via jQuery.ajax() with the value of your select field. How to handle this request, depends strongly on what you want to do with the value the user provided.
I think you can not. You can make an ajax call to PHP, but this will load the same page again in the background. This will not use the php variable of the current loaded page.
Maybe you can provide us some sample code to have a better understanding of what you try to achieve.
No, it's not possible without submitting the form. You can manipulate any elements thus form elements' value on a page with JS via the DOM and when the form is submitted they become PHP variables on the server-side. PHP variables can only be altered on the server-side.
PHP is a server-side language while Javascript is a client-side language only. Unless you make an AJAX (jQuery version) call or a form submit, your Javascript will never communicate with PHP.

Update a value of a JavaScript variable from another page

Is it possible to update the value of a variable from another page using JavaScript?
or
Is it possible to do this using form on submit, just update the other page, but remain on the page on which the form is located?
I think what you are searching for is AJAX (Asynchronous JavaScript and XML) which performs a get or post action and waiting for the result of the server. This makes it possible to submit a form without jumping to the page.
Have a look at this
Or if you use JQuery this
You can simply send all the data to be updated via Ajax and simply get the response on the current page You are in.

store value of input type="hidden" into a php variable without javascript

store value of input type="hidden" into a php variable.
The value is set by javascript into a hidden variable[input].
Now i need to save its value into a $myphpvariable.
I cant set the value into session with javascript and cookies is not an option.
After the page is submitted, hidden input elements will show up in the $_POST superglobal, just like any other form element. If the hidden form field were called coolsecret then the value would be in $_POST['coolsecret'] (or $_GET['coolsecret'] if you used method=get).
Before the page is submitted, there's no way to get a value from javascript into PHP, because your PHP code is all executed on the server, and the javascript isn't executed until the user has loaded the page on their computer. You should think about what you're trying to accomplish, and whether there's any way to compute the variable you want in PHP instead. If it's absolutely impossible to compute the variable ahead of time, then like Another Code said, you'll need to use something like an AJAX request.
What do you mean by save? When you submit that form you will be having value of hidden variable in $_POST or $_GET depending on the methodof form
You store php variable in js.but u cant stored js variable into php variable .because php run at server side and javescript at client side
The only ways to communicate Javascript results to a server-side script are:
Submit a form to the PHP script
Perform an asynchronous HTTP request with Javascript's AJAX functionality
You can't just mix the two, since Javascript operates inside the browser and PHP executes before a page is even sent to the browser.
Post it to the server then use $_POST["the_id"] to get its value.

jquery and php relations

how can i store jquery ajax result in a php variable?
i mean i want to get result of ajax jquery and store it in php variable and ECHO it later
Why do you want to do this?
What do you mean by "later"? Later, on the same page, or after the user navigates to another page?
If you want to echo it back to the page which has the ajax, then use js/jquery to echo it instead. If you want to save the variable in a session so that you can display it on another page, then you should be doing that on the php page you're presumably calling via ajax, rather than calling a php w/ ajax, having it return something, and then sending it back to the php again. However, if you really want to do that, you would have to stuff the variable into either a GET or POST variable so that it can be sent back to the server when the user navigates. i.e., put it into a hidden form and force the user to submit it, or put it into a link.... actually, you could do it with a 2nd ajax call, but now we're just going in circles aren't we?
why dont u use the data argument in the load function
syntax for load
$('selector').load(url,data,callback)
for data use {'key':'value'}
and then in php use $_REQUESTED

Categories