We’d like to remind Forumites to please avoid political debate on the Forum.

This is to keep it a safe and useful space for MoneySaving discussions. Threads that are – or become – political in nature may be removed in line with the Forum’s rules. Thank you for your understanding.

📨 Have you signed up to the Forum's new Email Digest yet? Get a selection of trending threads sent straight to your inbox daily, weekly or monthly!
The Forum now has a brand new text editor, adding a bunch of handy features to use when creating posts. Read more in our how-to guide

Insert Text From A Database Into A HTML Page???

Hi All,

Need a bit of help with a webpage, I know there are some very knowledgeable people on here, so hopefully someone will be able to help me out, or at least understand what i'm on about! lol

I have (or will have) about 100 HTML pages that are generally the same apart from one line of text at the top of the page.

That line of text will change every few weeks, and instead of having to manually update everyone of those pages, I would like to change just one file that the html pages takes the data from.

eg:

page001.html = "line of text for page001"
page002.html = "line of text for page002"
page003.html = "line of text for page003"
page004.html = "line of text for page004"
page005.html = "line of text for page005"
page006.html = "line of text for page006"
page007.html = "line of text for page007"
....and so on

So I can just change this file instead of all the html pages separately..

All the lines of text will be different and page specific, I know this is possible, but I just have no clue how it is done, I’m fine with html, but no good writing scripts.

I want to have a one off line of script to enter on each of the 100 pages, to grab text from a database file. So then I just have to update one file.

Any help would be greatly appreciated!

Kind Regards

Jody

Comments

  • jaydeeuk1
    jaydeeuk1 Posts: 7,714 Forumite
    Debt-free and Proud!
    Peasy. This assumes that each page is called 'Pagexxx', otherwise this won't work!
    In each page, have a javascript include file, called yourjspage.js.
    In this include file, have a variable for each page, eg

    page001="What ever text"
    page002="more text"

    or you could use an array but it wouldn't be much quicker to do.

    etc

    Then make a function called 'writepage' which will take a parameter (the html page)
    eg

    function writepage(thepage)
    !!
    !!
    ..
    .. code
    ..

    return thetext
    }

    Now, on each page, insert something like

    <SCRIPT LANGUAGE="javascript" SRC="includes/yourjspage.js"></SCRIPT>
    In the head

    The way I would do it, is pass document.location.href to the function writepage.
    As you are calling the variable names the same as the page name, it makes this dead easy. In the function, you need to first parse of the .htm part, and delete anything before the string 'page'. This will leave you with pageXXX where X is a number.

    Now, the variable name will match the string you have created, so just return that to the function.

    If you need help writing the function, let me know


    Now, on each page you will want JS to dynamically write out

    <script>
    document.write(writepage(document.location.href))
    </script>

    (search/replace will make this quicker!)

    There are easier ways, such as using a proper database/asp etc, but it works in theory ;)
    All you need to edit is the .js include file.

    Edit - The forum stops me from posting certain chars, so the code would look wrong, but hopefully you get the idea!
  • Thank you so much for spending the time to explain, it certainly gives me somethings to look into.

    You mention that you assume that each page is called 'pagexxx'... This will not be the case, each page will be called something different 'placename_year.html' or something similar.. What would you say the best option to look into in that circumstance?

    Thanks again for your time

    Jody
  • trcooke
    trcooke Posts: 309 Forumite
    For jaydeeuk1's solution there what you're doing is passing in the name of your html page as a parameter. So as long as your list of variables in your Javascript match the page names then it'll work fine.
This discussion has been closed.
Meet your Ambassadors

🚀 Getting Started

Hi new member!

Our Getting Started Guide will help you get the most out of the Forum

Categories

  • All Categories
  • 353.5K Banking & Borrowing
  • 254.1K Reduce Debt & Boost Income
  • 455K Spending & Discounts
  • 246.6K Work, Benefits & Business
  • 602.9K Mortgages, Homes & Bills
  • 178.1K Life & Family
  • 260.6K Travel & Transport
  • 1.5M Hobbies & Leisure
  • 16K Discuss & Feedback
  • 37.7K Read-Only Boards

Is this how you want to be seen?

We see you are using a default avatar. It takes only a few seconds to pick a picture.