Generating page information depending on variable selected - html

I am building a site with a select tag that has all 50 states. I want to be able to have a page generated for each state selected rather than having to write 50 separate pages. Any ideas on how I would accomplish this? Thank you.

You can either load all data on page load, or via ajax, and only show the data for the current state,
or if you have a server side database/data source to pull the data from you could also just have the page deliver data for a specific state, defined by a query/GET variable, the URL would look something like:
http://mysite.com/myPage.aspx?state=ca
or
http://mysite.com/myPage.php?state=az
When the page is requested you can then have your server page populate the correct data for the current state, which would then be sent over to the client.
Generally speaking I would lean toward the server side solution, especially if your visitors will likely only visit a handful of states then there's no reason to load ALL states data. On the flip side if the data for each state is very minimal it might not make much different either way.
EDIT
I'm not aware of any specific tutorials on the web, but since this encompasses putting a few things together I'll tell you the topics that might be useful in accomplishing this.
Depending on your level of knowledge of php, or c#, or other chosen language for your server side part, research the following topics:
Read a Get variable
Switch case statements
Add variable value into parts of html, or concatenation of html strings
Optionally various database topics, (if you don't want to hard code
data into your code)
For example, your server side php page could look something like this (untested code):
<?php
switch($_GET["state"]) {
case "ca":
$pageTitle = "California";
$pageContent = "stuff about CA";
break;
case "az":
$pageTitle = "Arizona";
$pageContent = "stuff about a really hot state";
break;
...
}
....
echo '<h1>' . $pageTitle . '</h1>' . '<p>' . $pageContent . '</p>';
?>
Along with that you'd want to handle when the visitor selects a different state, and upon selection of a state load the correct page. With jQuery you could do something with the change() method.
in your html body declare you drop down list (select input) with id="stateSelectDropDown", then in your javascript (untested code):
<script type="text/javascript">
$(function() {
$("#stateSelectDropDown").change(funciton() {
window.location = "nameOfThisPage.php?state=" + $(this).val();
});
}
</script>
I apologize for any typos or bad syntax, I hope this points you in the right direction.

Related

Is there a way to create a list that auto populates with the the required articles?

I am a UI/UX designer and I have taken on a new project to overhaul our Zendesk. I have no experience but I am learning on the fly, I have already built a custom sidebar navigation and now my problem is this:
Is there a way to have the sidebar auto populate with the categories and articles from our knowledge base? I assume there is a way to do this, otherwise that would mean every time I upload a new document to our knowledge base I will also have to go back into the code and add the href with the link and title to the sidebar nav html and I'm certain that's not the way this works. I'm happy to do the research but I don't know what to search for, I've tried researching auto populating lists etc. but nothing matches my need. I can manually input every article into the side bar and it functions beautifully, I just figured there was an actual way to automate this once the code is input.
Thank you in advance,
-Newbie
Why are you adding a list manually, you can call API for all categories of your Help Centre and make a sidebar:
1). Add the below code to your script.js file at the bottom.
$.getJSON('/api/v2/help_center/categories/' , function(el){
console.log(el)
var _x = '';
$.each(el.categories, function (idx, itm){
_x += '' + itm.name + '<br>';
});
if("ul.categories"){
$(".categories").html(_x);
}
});
2). document_head.hbs should have jquery CDN.
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" integrity="sha512-894YE6QWD5I59HgZOGReFYm4dnWc1Qt5NtvYSaNcOP+u1T9qYdvdihz0PPSiiqn/+/3e7Jo4EaG7TubfWGUrMQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
3). Add this HTML to your template where you want to show the categories.
Categories
The given solution is only for listing all categories not articles, you can add the articles inside the categories using the API inside it: https://developer.zendesk.com/api-reference/help_center/help-center-api/articles/
If you want to show only articles not categories then replace the API with the article's API.
As, I said in my comment, if you have a db in the backend with your info, you should use Ajax.
Ajax allows JavaScript to send requests to the backend. When this request is sent, the backend should reply with data in some form, such as JSON, and then your client side JavaScript will need to populate the sidebar using the data.
If you give some more info about your backend and html, I can give an example!

Link that forwards the URL's parameters

There is a main Sales Page: site.com/sales-page
That page receives visitors from different traffic sources, and they are mentioned on its URL parameters, such as: site.com/sales-page?utm_source=fbads&utm_campaign=banner
That Sales Page has a link pointing to BUY NOW
How do I pass all URL parameters from the current window, such as "?utm_source=fbads&utm_campaign=banner" automatically to the next page via the BUY NOW button?
I need this so the Checkout page will know where the traffic came from, based on that forwarded parameter.
PS: I want to pass all parameters available in the URL, not just pre-defined ones.
PPS: Solution must work on most browsers and on static pages, better to not use cookies/php.
Thanks a lot.
There is no way to achieve this using HTML. You have to use a programming language.
For example:
<?php
$url = "http://example.com/foo/?" . $_SERVER['QUERY_STRING'];
?>
link
If you don't want to use PHP, then other programming languages are available.
There is no way to do it with just html. Best solution is serverside code. Fallback is to use JavaScript, but this will not work if JavaScript is disabled. You can read the search from the url and add it to your link.
<a id="buyNow" href="http://www.example.com">Buy</a>
<script>
(function () {
var lnk = document.getElementById("buyNow");
lnk.href = lnk.href + window.location.search;
}())
</script>
This assumes there is no querystring already on the link. If there is, you need to replace the ? in the search with a &

SQL Query - extracting data between html tags in a text column into new table

A web form was developed and instead of saving individual fields the whole form was saved in a text column as html. I need to extract data between various html tags so want to create a query that writes each set of tags to a table for me to then use - if this is possible please can someone advise how this can be achieved.
Thank you.
Ok, i've noticed you weren't happy with my last answer, however i am still certain you need server side code to handle SQL queries. Basically without having any sort of server side code you wanna do something like "here's a TV i bought, and here's the DVD release of the "John Wick" movie, i wanna watch it on this TV. Without a DVD player you can't really do it tho', that is the role of the PHP or ASP.NET in this case. Since I am not familiar with PHP, I am only able to show a solution in ASP.NET C# which i put together some time ago.
Here's how I solved this in a site i've built some time ago. It is not the cleanest, but it most certainly worked.
In ASP.NET you have the page file, which is similar to a HTML or an XML file, using a lot of pointy brakcets. Create one like this:
page file, body:
<asp:TextBox ID="HiddenTextBox" style="display: none;" runat="server"
onclick="OnStuff" OnTextChanged="TheUserChangedMe"
AutoPostBack="true"></asp:TextBox>
scroll up a bit, and in the section add some javascript, where you can handle on text change instantly. So as soon as something happens, like the user clicks on an image, or... well does anything you want him to do (and respond to that) you need the ID of that element he clicked on.
page file, head:
<script type="text/javascript">
function MarkItems_onclick() {
var Sender = window.event.srcElement;
document.getElementById('<%= HiddenTextBox.ClientID%>').value = Sender.id;
__doPostBack('<%= HiddenTextBox.ClientID%>', 'TextChanged');
}
</script>
page's .cs file, the C# code behind
//
//add these on top:
//
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
//
// later somewhere write this:
//
protected void TheUserChangeMe(object sender, EventArgs e)
{
SqlConnection conn1 = new SqlConnection(ConfigurationManager.ConnectionStrings["UserRegConnectionString"].ConnectionString);
//
Note: the connection string has to be set up earlier. make sure you make one, visual studio will let you do that in no time. Do not forget, that this connection string defines where your DB is located, and grants the required information for the site how to even reach it at the first place.
//
// somewhere you need to read out what you have in your HiddenTextbox:
//
String stringToProcess = HiddenTextBox.Text;
process your stuff, here i assume you cut it up accordingly and you will have an insertQ variable with a proper syntax. in order to add these values it should look something like this:
String insertQ = "insert into OrdersTable(OrderId, Type, Quantity) " +
"values (#OrderId, #StuffType, #StuffQuantity)";
How to access the database in asp.net C#:
conn1.Open();
SqlCommand insertComm = new SqlCommand(insertQ, conn1);
insertComm.Parameters.AddWithValue("#OrderId", nonStringVariable.ToString());
insertComm.Parameters.AddWithValue("#StuffType", aStringVariable);
insertComm.Parameters.AddWithValue("#StuffQuantity", "some random text");
insertComm.ExecuteNonQuery();
conn1.Close();
that's pretty much it. Your SQL database will have 3 fields filled up every time this function runs. It's a bit messy, but for my site it was crucial to handle any onclick event, and with this you can flush out 23 checkboxes, 10 pictures and whatnot as page elements, yet you'll know what happened every time the user clicked something.
i'm not a professional either, however i think you're gonna need something on the server side to process this query, like asp.net or php. Basically the server side code would have no problem generating your page's content according to what comes back from the DB.

Server Side Includes with variable (IIS 7)

I am trying to build a universal header file that I can include in each .html file on my site. My header contains several dropdown tabs, and one of the tabs is always highlighted (depending on which page the user is on). So I want to do something like a server side include for the header, but I also want to give it a variable so that it knows which tab to highlight, something like this:
<div class="topmenu">
<ul>
<someScript>
if (variable=="home") {
print "<li class='current'>";
} else {
print "<li>";
}
</someScript>
...
My server is IIS 7 and doesn't support PHP, and I don't want to rename all my files to *.asp so that I can use ASP. How could I go about this?
By the extension I guess you would use Classic ASP. Then something like this should work:
<!--#include file="header.asp"-->
You can put this in each file you want to have a header.
Of couse, you should create that "header.asp" page first ;)
For highligthing the tab of the page you're in, there're several methods.
IMHO, I suggest a clientside script to do that. JS or jQuery of course.
You could check the file name of the URL you are in and give the proper class to the tab so it will be highligthed.
Example ( jQuery needed ):
var currentPage = window.location.pathname.substring(url.lastIndexOf('/')+1);
if(currentPage == 'default.asp') $('li.homepage a').addClass('current');
This simple code retrives the file name and, by it, add a class to the corresponding element in your navigation.
Of course this is a conceptual script, you'd better adapt it to your page.

Animating JSON data?

Dumb question time. I was trying to integrate my JSON data with a flipbook plugin, using a Mustache templating system. Needless to say, this wasn't working at all.
I'm a jQuery noobie, so is there any easy way to bind and animate the JSON data to/with a plugin (with or without the Mustache tags)??
From your question it is a bit hard to deduce what you want, but I feel you got already all the pieces together. First the example you have been linking to in a comment: https://github.com/blasten/turn.js/wiki/Making-pages-dynamically-with-Ajax
This fetches not yet loaded pages via Ajax, and the sample code assumes the Ajax call gets HTML back from the server, as can be seen in the code snippet from there (after adding a missing '}':
$.ajax({url: "app?method=get-page-content&page="+page})
.done(function(data) {
element.html(data);
});
Here the done function processes the data it got back from the server by straight injecting it into the element, which is expected to contain the current page.
I assume next that you do have a server side method, but that method returns JSON instead. Let me assume for the moment that it returns the following structure:
{ "title" : "the title of page N",
"text" : "here is some text for the page N." }
The next thing is to render this JSON into into html in the done funktion, before inserting the result into the page. Looking at the short tutorial on the README.md this might look like:
$.ajax({url: "app?method=get-page-content&page="+page})
.done(function(data) {
var pageHtml = Mustache.render("<h2>{{title}}</h2><p>{{text}}</p>", data);
element.html(pageHtml);
});
If the server returns the proper data you should now see a
<h2>the title of page N</h2><p>here is some text for the page N.</p>
appear on the page.