Click here to Skip to main content
15,885,141 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am building a search function for a sermon blog on squarespace. The format url for category search is www.mysite.org/blog?category=... I have drop down menus and a datepicker but need to figure out how to get my code to build the url and redirect to it. The output url should look like www.mysite.org/sermons?category=mySpeaker+myService+mySeries+mybook+mydate. Any help would be greatly appreciated. Here is my javascript so far.

JavaScript
function search() {
    var myRootSite = 'www.mysite.org/sermons?category=';
    var myNewTitle = document.getElementById('myTextField').value;
    var mySpeaker = document.getElementById('mySpeaker').value;
    var myService = document.getElementById('myService').value;
    var mySeries = document.getElementById('mySeries').value;

    var myBook = document.getElementById('myBook').value;
    var myDate = document.getElementById('myDate').value;

    if (myNewTitle == 0 && mySpeaker == 0 && myService == 0 && mySeries == 0 && myBook == 0 && myDate == 0) {
        alert('Please enter some search criteria');
        return;
    }

    var title = document.getElementById('title');
    title.innerHTML = mySpeaker;
}
Posted
Updated 21-Sep-14 2:32am
v5
Comments
Richard MacCutchan 21-Sep-14 3:20am    
This has nothing to do with Java, it is Javascript, a totally different system.
J.Sanders 21-Sep-14 8:33am    
Corrected and thank you, any help w the solution?
[no name] 21-Sep-14 8:36am    
I am sure that help would be appreciated but you would have to tell us what the problem is first.
J.Sanders 21-Sep-14 8:41am    
Problem: I do not know how to get my javascript to take the selected variable data and add it to the end of a url and then link to it. I need the user to be able to run through the drop down menues, select what they need and click find and for the script to take the blog url (www.mypage.com/sermons?category=) and add the appropriate query item to the url(sermons?category=(whatever value they selected for speaker)+(whatever value they selected for service)+(whatever value they selected for series)... and then redirect to that new url.
[no name] 21-Sep-14 9:04am    
You would use the + to concatenate your strings together to form your new URL and then use window.location to redirect to the new URL. I doubt your if statement would work though. In all likelihood, a string would never be equal to an integer 0.

1 solution

You can put all items in an array, join them and concatenate them to myRootSite:
JavaScript
function search() {
    var myRootSite = 'www.mysite.org/sermons?category=';
    var myNewTitle = document.getElementById('myTextField').value;
    var mySpeaker = document.getElementById('mySpeaker').value;
    var myService = document.getElementById('myService').value;
    var mySeries = document.getElementById('mySeries').value;
 
    var myBook = document.getElementById('myBook').value;
    var myDate = document.getElementById('myDate').value;
 
    if (myNewTitle == 0 && mySpeaker == 0 && myService == 0 && mySeries == 0 && myBook == 0 && myDate == 0) {
        alert('Please enter some search criteria');
        return;
    }
 
    var title = document.getElementById('title');
    title.innerHTML = mySpeaker;

    var valuesArr = [ myNewTitle, mySpeaker, myService, mySeries, myBook, myDate ]; // put the items in an array
    var valuesStr = valuesArr.join("+"); // join them
    var URL = myRootSite + valuesStr; // concatenate them to myRootSite
}
 
Share this answer
 
Comments
J.Sanders 22-Sep-14 20:22pm    
This seems to build the array but how do I get the button to send me to the URL it's created? Thanks for the help, I'm definitely closer than I was.
Thomas Daniels 23-Sep-14 12:09pm    
Create a button, and add the attribute onclick="search()". Also create an element in which you want to show the result and give it the ID result. In your search function, at the end, use document.getElementById("result").innerHTML = URL; to show the result in the element.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900