Forum Moderators: coopster

Message Too Old, No Replies

Can't find a way to preserve a form-value in a Joomla-component

         

koda

10:47 am on Nov 29, 2007 (gmt 0)

10+ Year Member



Hello,

First - I am a complete noob at PHP and HTML, and that's why I'm stuck!

I'm trying to make a slight modification to a Joomla component called "Chordbase". What it does, is the ability to have a database of songs - with song lyrics and guitar/piano chords shown. It shows a list of songs (by name), and by clicking a songname - it displays the songlyrics (with chordnames above the songtext).

Now; I'm adding an extra functionality to Chordbase. The function I'm trying to make - is to have the chords shown [u]below[/u] the song, as chord-images. By the help of a dropdown-box (select-box), one can choose between one of these:
- dont show chord-images
- show guitar chord images
- show piano chord images

I've gotten so far where I it actually works- in sense of that I can choose i.e. "Show guitar chords" and press the "Update" button (behind the select-box). Then, the chords ARE shown below the song! The trouble comes when I press "Printer friendly".... I will come back to that in a minute!

Most of the "action" going on in this Joomla-component, is divided into these php-scripts:

chordbase.php (the "main" program)

chordbase.html.php (makes the top-screenpart of the songview - where it gives a "menu" with different options,
like "Printer friendly" or "Edit song".... and so on... IT'S HERE I ADDED MY DROPDOWN-BOX).

song.class.php (displays the songtext / chordnames. IT'S IN HERE I ADDED THE CODE TO DISPLAY
CHORD-IMAGES).

In a function called "function songHeader" in "Chordbase.html.php" I've added the code like this:

<form action="index.php?option=com_chordbase&Itemid=<?php echo $Itemid?>&task=viewSong&song_id=<?php echo $song_id?>&pop="0"" method="post">
<select name="showchords">

<option value="dontshow" >Don't show chords</option>
<option value="showguitar">Show guitar chords</option>
<option value="showpiano">Show piano chords</option> -->

<input type="submit" name="submit" value="Update">
</select>

So far, so good.... BUT --- and here's a HUGE trauma (yes, I'm almost going nuts here):
-> When I choose "Printer friendly" and the song shows up in a new window- the chords are gone!

I've so far found out this:
When I select "Printer friendly" - ALL variables I'm using for checking whether chords are going to be shown or not - they are RESET (null-value).

I've added the variable $_POST['showchords'] in function "song.class.php" to check/evaluate if the chord-images are going to be shown (it's in this function I've added some programming to show the chord-images). Each time "Printer friendly" is called - [u]this variable is EMPTY[/u].

Another thing (this is maybe one major reason for this problem):
- usually, when the song itself is shown - it calls up Joomla's "index.php" - i.e. like this:

index.php?option=com_chordbase&Itemid=<?php echo $Itemid?>&task=viewSong&song_id=<?php echo $song_id?>&pop="0"

But, when "Printer friendly" is called - Chordbase does this by calling Joomla's "index2.php", like this:

<?php echo $mosConfig_live_site?>/index2.php?option=com_chordbase&Itemid=<?php echo $Itemid?>&task=viewSong&showChords=<?php echo $showChords == "no"?"no":"yes"?>&song_id=<?php echo $song_id?>&pop=1" target="_blank">Printer friendly</a>]

--> this code is located in "chordbase.html.php" (as one of the menu-choices).

So, I assume that because of the different approach of calling the function for showing the song (in "song.class.php") - it looses all knowledge of the content of this variable i mentioned further above...
How to deal with this - is far, far beyond my knowledge and skills :(

HOW in the world can I get it to REMEMBER that I chose "Show guitar chords" when I select "Printer friendly"?

I've tried absolutely "everything" my ignorant mind could find on the internet (Google)...

- koda

Habtom

10:51 am on Nov 29, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Koda, Welcome to WebmasterWorld.

You could try storing the value in a SESSION.

Habtom

koda

11:17 am on Nov 29, 2007 (gmt 0)

10+ Year Member



Hi, thank you for a QUICK response! (I'm impressed)

I thought of using SESSIONS - but I have trouble with understanding how to use it. I have read several places about it - but so far I've not understood how to use it and where to place these SESSION-variables. I became a bit confused when I read about it - because it seemed to be different ways to use it... sorry about that, but I am still in an early learning process...

In which of the php-scripts do I have to put something in? I would be glad for some examples... :)

-koda

PHP_Chimp

11:54 am on Nov 29, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Couldnt you just use CSS for the printing? So that it all comes from the same page, just using a different stylesheet for print media?
That way you dont need to worry about 2 different pages for 2 different media.

It surprises me how many people bother to code 2 different complete pages, when they could just use different CSS (for the really lazy 1 CSS file with @media rules in). Sometimes there is a reason...most of the time there isnt, CSS may well save you a lot of hassle.

koda

12:57 pm on Nov 29, 2007 (gmt 0)

10+ Year Member



Thanks for your reply PHP_Chimp!

I'm sure it's possible to use CSS here, but then I'm into deep, deep water..... - this is something I haven't dug into yet... But, I appriciate your suggestion - I can understand that it many times would be a smart way of handling things like this...

As far as I can see in the original php code - where the "menu" link for "Printer friendly" is; it re-uses the same code for displaying the songtext - but it calls it thru "index2.php" (Joomla's php-script) - instead of "index.php" - which it normally uses (also a part of Joomla). I've found out that "index2.php" gives a page WITHOUT the Joomla template and menus (hence; a blank page)...

So, I thought it would be better for me not to hazzle too much with the original code - I would for sure make it a lot worse...

koda

5:06 pm on Dec 1, 2007 (gmt 0)

10+ Year Member



Hmmm.... I tried the SESSION-variable method. It worked only in 1. step (showing the song with chords). Step 2 (choosing "Printer friendly") didn't work out...the session variable was cleared (empty) - and I can't figure out why....
It's a possibility that I have been using SESSION wrong (I start each PHP-script with
<?php session_start() ?>

The variable $_POST['showchords'] I then save in $_SESSION['willshowchords'] --IF-- the $_POST['showchords'] is NOT empty.

I'm out of ideas.... such a thing SHOULD be possible (preserving a variable "no matter what")?

PHP_Chimp

6:55 pm on Dec 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I would honestly say that it would be well worth your time looking into CSS.
The problem with printer friendly pages it that they need to be kept up to date, not so bad if you are using a template system, but if you add something to your main page then you need to remember to add it to the printer friendly version as well.
There is a CSS forum [webmasterworld.com] here so im sure that they can help you.
Assuming that your site also uses some javascript (basically so that you have id's for different parts of the page, although you dont have to have them, it just gives you more control over the CSS) then there should be no problem in sorting out a very quick CSS script to run the print job for you.

The problem with trying to use php with printing is that printing is a client side thing. As php is a server side thing you have to jump through more hoops to use php for a client side thing. Using CSS will allow you to print out whatever is displayed on the page at the time when the person clicks print...no additional page calls, no worrying about displaying the correct thing. The click print and get what you want them to get.