Hi all I am developing web app using php, jquery and bootstrap. Now in this app what i am doing is that i am getting a value from button on button click event and passing the value to the ajax, jquery and then jquery to the php file where it will be processed and this value is stored in an array. Every time new value comes value is passed through jquery and then to php where the value is pushed in array. Now let's assume that i have refreshed the page, will my array will be empty on page refresh or it will hold the value in that php file. The structure of files is like HTML/CSS file containing the form, jquery file containing all the jquery functions and php file processing the data. If html page gets refreshed will the array become empty or not ... ?
The php array will be empty and/or will only ever have one value in it.
This is because each request via ajax will be a new request, which is asynchronous, to the current request.
A solution would be to store the array in the PHP file in a session, and retrieve the array from the session.
That way the data will persist across multiple requests.
Related
Initially, as can be seen in this unsuccessful attempt: Undefined index error when sending variable from jquery to php and this unsuccessful attempt:Passing Jquery variable to php within a modal
I was trying to pass a variable from php, to jquery and back to php.
Basically I am generating images form a database in a php loop. When the user selects an image, I want the id of that image to be passed to the modal so that I can query the database again (based on that id) and get more information.
Is there another way of doing this other than the methods outlined in the posts above. For some reason those do not return the result.
The difference between Send PHP variable value to JavaScript popup window in same page and my question is that I want it as a heading.. and as a variable so I can query the database again. I am not using this to input values into a form
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.
I have a somewhat theoretical question this time:
The situation (on a PHP website):
members on a website can add cd's to their 'favorite list'
onClick of the like-button, jQuery/ajax ads the cd to the favorite
list
another file renews the session array [favCDS]
goal: the user always has his latest clicks updated real time,
because live-data is generated from stored arrays)
The question:
Would it be possible to update a SESSION array of personal member values with a function, through a file running on the background, called by and updated through jQuery/ajax?
I imagine it would be updating it all in one file, but I wonder if you guys have any thoughts/ideas on this.
The PHP session persists even after scripts are finished running, so you don't need a PHP file running in the background on the server.
Example PHP file (called by the ajax function, assuming the ajax function submits a POST request with the CD's id):
update-favorite.php
$_SESSION['favCDS'][] = $_POST['cd-id'];
That way, when the user navigates to a new page, that page can preserve the favorite CDs by accessing this session array and generating HTML accordingly.
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.
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.