Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Some advice on jquery/javascript arrays and php sessions

Posted on 2011-10-27
2
Medium Priority
?
395 Views
Last Modified: 2012-05-12
Hi all,

I have an intranet document search with a favorites feature where the user clicks a star next to the document and the doc_id is stored in a database table against that user id.

When the user logs in, I set a $_SESSION['favorites_array'] with all the values. There is a max of 2000.

I've built the functionality to add new favorites but now I'm working on the UI using javascript/jquery. With php I'm fine, I can output the dynamic search results and check if that doc_id is in the $_SESSION array before outputing either an onclick function to add or a 'Favorited' message.

My challenge is when the page is already loaded - use case:

- user clicks 'add to favorites'
- jquery modal dialog loads with some options
- user picks some and then clicks 'Save & Close'

When the dialog closes, I now need to update that result with the 'Favorited' message (or unfavorite etc) without the page being reloaded. I am able to assign the doc_id to the result like so:

<span id="doc'.$doc_id.' class="favorite" '.$onclick_code.'>'.$favorites_text.'</span>

Open in new window


Is it a case of storing favorites in a jquery array and running through these or being more specific with the code in the success javascript (below)? Any advice hugely appreciated!

 $('form[name=addnewfavoriteform]').submit(function (){
			$.post( $(this).attr("action"), $('form[name=addnewfavoriteform]').serialize(), function(data){
			
			if(data==0)
			{
				$("#addfavorite-msg").fadeTo(200,0.1,function() 
				{
					//add message and change the class of the box and start fading
					$(this).html('Please check the above errors.').removeClass().addClass('submit-msg-error').fadeTo(900,1);
				});
			}
			if(data==1) //Shouldn't get this 
			{
				$("#addfavorite-msg").fadeTo(200,0.1,function() 
				{
					//add message and change the class of the box and start fading
					$(this).html('An unexpected error has occured. Your login may have expired.').removeClass().addClass('submit-msg-error-').fadeTo(900,1);
				});
			}
			if(data==3) //Max number of favorites reached
			{
				$("#addfavorite-msg").fadeTo(200,0.1,function() 
				{
					//add message and change the class of the box and start fading
					$(this).html('Max number of favorites reached. Please delete some before adding new ones.').removeClass().addClass('submit-msg-error-').fadeTo(900,1);
				});

			}
			if(data==2) //Success
			{
				//Close window all saved
				$('#addfavorite').dialog('close');

			}
			
			});
			// prevent default action of form submission
			return false;
			
	 });

Open in new window



0
Comment
Question by:dolythgoe
2 Comments
 
LVL 21

Accepted Solution

by:
Julian Matz earned 2000 total points
ID: 37042670
I think you'll have to have the callback data tell your callback function whether a document was added to favourites or removed. Then, before you close the dialog window, you update the text on your page accordingly. The $doc_id variable could be passed to your PHP script via the form, and then back to your JS. If your callback data is in json format, you'd be able to pass back the id, the result code and a variable that tells your script whether a favourite was added or removed.

Something like this:

if(data.result==2) //Success
{
    id = 'doc_' + data.id;
    if ( data.action=='added' )
    {
        $(id).html( 'remove from favourites' );
    }
    else
    {
        $(id).html( 'add to favourites' );
    }
    //Close window all saved
    $('#addfavorite').dialog('close');
}

Open in new window


Just remember to tell jquery to expect json as the callback data to be safe, like this:

$.post( $(this).attr("action"), $('form[name=addnewfavoriteform]').serialize(), function(data){
    //...........
}, "json" );

Open in new window


Hope that helps.
0
 

Author Comment

by:dolythgoe
ID: 37043438
Thanks for that I will give it a go :)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question