Some advice on jquery/javascript arrays and php sessions

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



dolythgoeAsked:
Who is Participating?
 
Julian MatzJoint ChairpersonCommented:
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
 
dolythgoeAuthor Commented:
Thanks for that I will give it a go :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.