Update a value of a JavaScript variable from another page - php

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.

Related

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.

How to resubmit PHP form with javascript

I am wondering if it is possible to to resubmit a form with a button click that calls up a javascript command.
So this is basically what I'm trying to do -
page 1: form; action = page2.php
page 2: generate a randomized list according to parameters set by page 1
I would like to place a button on page 2 so that on click, it would be as if the user has hit F5, and a new list would be generated with the same parameters.
I found a lot of help on Google with people trying NOT to get this to happen, but I'm not sure how to actually get it to happen.....
Thank you!
You could use location.reload(true); to refresh the page.
<button onclick="location.reload(true);">refresh</button>
you only need to use. location.reload() instead of location.reload(true). since this will not disable caching for the reload but using true as the parameter will.
here is the reference. How to refresh a page in jquery?
EDIT: why not just use hidden input field and preserve the values and fetch it anytime you want to?

Sending JavaScript variable to 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.

how to post data from one php to another without reloading it?

I have a html page with two iframes in them .there is a button on first iframe which should reload second iframe with some data which is sent on the click of the button ..but the first iframe containing the button should not reload.how can i make it possible ?
<?php
// code to send data without reloading page but instead reloading another iframe with sent data?>
In the first iframe, use javascript to submit the form by some sort of ajax method:
<form [...] onsubmit="sendAjax(); return false;">
The return false part prevents the form from submitting (as long as you don't have some sort of errors in your sendAjax() function.
Since it is going to take a bit of time for the server to process the request that should show up when iframe 2 refreshes, you could possibly use evilone's suggestion and use sessions to see if some sort of session variable has been set. Then in frame 2, make the javascript wait say 500 ms before refreshing and then refresh the page. You could loop this refreshing process to make sure that the session variable has been set and that the correct output will be sent.

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