Update a session array with jquery / ajax - 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.

Related

Dynamic document.title using a $_SESSSION variable

I have a page that uses ajax to show users their current assignments. Instead of having to refresh the page to see if there are any updates, I'm using ajax to update the data every 4 seconds. It's been requested that I change the document title to show something like "Number of Tasks: 4" and have that update as well when the user either completes a new task, or gets assigned another one. I tried using a simple "setInterval" javascript function, but since PHP is server side, the variable piece doesn't update...
I've also tried setting "document.title" from within the ajax code, but that just plain didn't work.
Is there a simple way to update the document title to show the number of tasks assigned to the user viewing the page?
Return the value from the $_SESSION in the data sent with the AJAX response to the client Javascript code. Once you have it on the client side set whatever you need to it with javascript.
You'd have to call with ajax a php dedicated to return you only the number of tasks (and other information you may want).
To change the title you can just call document.title = "the data returned in ajax";.
And put all this code (ajax call and title set) inside a function with setinterval as you mentioned.

How to design a script that will use many user inputs from another page using AJAX & PHP?

In a nutshell.... what i want to do is a install script for my website... but i want everything to be done in 1 page which will connect to another php file that will have all the server side processing code. In order to do that i use JQuery, Ajax & PHP...
Now my question lies in what are the best practices in order to do that. How do I make my server side PHP script to take multiple user inputs that will happen throughout a lengthy installation processes?
I was thinking on using switch statements that will trigger classes & functions and like that being able to access multiple processes (Classes and functions) that will be stored in the same file?... or people normally use other things?? never done this before and couldnt find a straight forward tutorial for it on how is best to design it.
I was thinking on maybe being able to access classes through AJAX in php but that doesn't seem to be possible.... can i trigger functions from AJAX in php?
I really appreciate the help in how normally this is approached
thx
It would be too lengthy to define the whole process in SO in detail. But the basic idea is:
User fills in form clicks NEXT button.
You get all the form values (in JavaScript), post it to a PHP page using AJAX
The PHP page gives you back a flag (SUCCESS/ERROR) [In the background, if it is success, PHP has persisted the data into a database]
If JavaScript receives ERROR, you show user the error message and ask him to correct the form
If java script receives SUCCESS, you call the next PHP script, which generates the next form and sends it to your JavaScript
JavaScript populates this form content within a DIV in your page, effectively wiping out the current form and showing the new form in its place.
..and the whole process restarts from the first step once again for the new form..

PHP autoupdate form data without pageload

I run a php script game site, and there's a section of it where a player can distribute his 'skill points' in 'Attack', 'Defence', or 'Stamina'.
At the moment, it's just a basic HTML/PHP form with a + next to each stat, so if the user had 100 points and wanted to put them all into ATTACK, he'd have to click (and pageload) 100 times. Obviously that's silly.
I want to be able to make it so the player can distribute them (without page reloads), then finally click SUBMIT once he's happy with it.
Could anyone point me in the direction of what I need to do this? Is it some ajax or javascript? I suppose I could do form fields and clicking the PLUS would increment each field..
It sounds like you need an onclick() to call a javascript function for the plus button, that increments the value in javascript, updates on the page and checks that the user has distributed the points correctly (i.e. distributed 100 points or under in your example) and then have another button at the bottom of your points distribution that sends the data to a PHP page that handles the storage of the values.
If you wanted you could then use ajax to send that data to the PHP page without having to reload the page. I've found the W3schools site adequate for teaching basic ajax usage: http://www.w3schools.com/ajax/default.asp
(But remember to check the user input on the server side as well, don't trust user input!)

Getting $_POST variable from table

I'm trying to build a sort of resource allocation form. I'd like to be able to print a table from a database, and then allow users to click on each cell that they would like to reserve. Also, being able to drag and select multiple cells. Then send all of this via $_POST to another php script.
Problem is, I have no idea where to start.
Any suggestions?
The first and most critical thing you're going to need from what you described is a bunch of hidden fields to store the information you're interested in. You would have to write javascript code on the client side to store the users interaction with your page into these hidden fields.
To receive data via POST, you will need <input type="hidden" name"some_field"> for every bit of data you wish to "know" about that was changed on your page. Table information is not transmitted in a POST operation if it's just text, so you can't see the layout of the modified table on post back to the server.
If you don't have to POST this data to another form, it is probably a better idea to make callbacks via XMLHTTPREQUEST as the user interacts with your page, but I don't know the requirements of what you're trying to do.
I wrote one for my school recently; the trick is to either use buttons/links or addEventListener the cells to JavaScript. If you want the source code to my app, download this zip file:
http://azabani.com/files/busbook.zip
Edit:
My system works in the following way:
addEventListener to cell clicks, calling book()
book() then sets location to book.php
book.php does the database work
book.php sets the location header to immediately go back to the viewer
The system knows which week view to go back to based on session variables.

Ajax problem not displaying data using multiple javascript calls

I'm writing an app that uses ajax to retrieve data from a mysql db using php. Because of the nature of the app, the user clicks an href link that has an "onclick" event used to call the javascript/ajax. I'm retrieving the data from mysql, then calling a separate php function which creates a small html table with the necessary data in it. The new table gets passed back to the responseText and is displayed inside a div tag. The tables only have around 10-20 rows of data in them. This functionality is working fine and displays the data in html form exactly as it needs to be on the page.
The problem is this. the HREF "onclick" event needs to run multiple scripts one right after the other. The first script updates the "existing" data and inside the "update_existing" function is a call to refresh a section of the page with the updated HTML from the responseText. Then when that is done a "display_html" function is called which also updates a different section of the page with it's newly created HTML table. The event looks like this:
Update
This string gets built dynamically using php with parameters supplied, but for this example I simply took the parameters out so it didn't get confusing.
The "update_existion() function actually calls the display_html() function which updates a section of the page as needed. I need to update a different section of the page on the same click of the mouse right after the update, which is why I'm calling the display_html() again, right after it. The problem is only the last call is being updated on my screen. In other words, the 2nd function call "display_html()" executes and displays the refreshed data just fine, but the previous call to update_existing() runs and updates the database properly, but doesn't display on the screen unless I press the browsers "refresh" button, which of course displays the new data exactly how I want it to, but I don't want the users to have to press the "refresh" button. I tried adding multiple "display_html() calls one right after the other, separating all of them with the semicolon and learned that only the very last function call actually refreshed the div element on the html page with the table information, although all the previous display_html() calls worked, they couldn't be seen on the page without a refresh of the browser.
Is this a problem with javascript, or the ajax call, or is this a limitation in the DOM that only allows one element to be updated at a time. The ajax call is asynchroneous, but I've tried both, only async works period. This is the same in both Firefox and Internet Explorer
Any ideas what's going on and how to get around it so I can run these multiple scripts?
I'd recomment you to use jQuery javascript library. It has some funcions, like live() that can "wait" for that table to appear on the browser and apply the remaining functions on it.
Also, it's a great set of functions that will certainly help you out reducing the ammount of code you write, making it more human-readable.

Categories