LRG Posted October 13, 2016 Share Posted October 13, 2016 (edited) I posted this thread on a similar forum, however, it failed to receive an answer, so I'll ask it here instead. On the other forum, it got around 6 000 views, while other threads created around the same time got around two or three hundred views. I can think of three reasons why it would receive so many views yet no responses: it was incredibly easy, and no one wanted to answer; it was hard, and no one knew the answer; or, its answer required a lengthy explanation that no one had the time to give. Regardless, it's still a question that I can't answer, so here's the original: Here is the code I currently have: https://jsfiddle.net/88sxy0jg/15/ Basically, when I select 2 of the boxes, the alert for the second URL pops up - which is good. However, in the URL, it will end with "false&n= (etc.)" How do I get the value for when checked=1 in the second URL instead of "false"? The similar issue goes for when checked=3 and so on, should I add more checkboxes. Knowing me, I'm probably overlooking some minor detail. Edited October 13, 2016 by LRG Link to comment Share on other sites More sharing options...
Ingolme Posted October 13, 2016 Share Posted October 13, 2016 What are the exact rules for the resulting URL? Keep it short and simple, I need a list of rules that can apply to every possible scenario. I notice that if three checkboxes are selected, the "n" parameter appears twice in the URL, that's not right. Link to comment Share on other sites More sharing options...
LRG Posted October 15, 2016 Author Share Posted October 15, 2016 (edited) Thank you for answering The "&n="s in the resulting URLs are simply to delineate the terms, they're not really needed. I'll explain below: Rule 1: The number of boxes selected corresponds to the first part of the specific URL used. In this case, 1 box selected will bring the user to http://example.com/results?choice=. If 2 boxes are selected, the user will be brought to http://domain.com/results?choice=. If 3 boxes are selected, the user will be brought to http://url.com/results?choice=. Rule 2: The value of the boxes selected corresponds to the second part of the specific URL used. The second part of the URL consists of the value of the boxes selected. If the user selects 2 boxes with the values 2 and 3, then he will be redirected to http://domain.com/results?choice=2&n=3. Again, the "&n=" is just a way of separating the values. Please let me know if any further clarification is needed Edited October 15, 2016 by LRG Link to comment Share on other sites More sharing options...
Ingolme Posted October 15, 2016 Share Posted October 15, 2016 Why is n 3 rather than 2 when the values of the boxes are 2 and 3? Does it always select the highest value? Link to comment Share on other sites More sharing options...
LRG Posted October 15, 2016 Author Share Posted October 15, 2016 Really, the "choice" part of the URL should be "n". I've updated it here: https://jsfiddle.net/88sxy0jg/16/ n=(the numerically first value selected)&n=(the numerically second value selected) and so on. For example, if boxes with the values 2, 3, and 4, respectively, are chosen, then the resulting URL would be http://url.com/results?n=2&n=3&n=4 (using the updated script). All values selected should be represented in the URL with n=, not just the highest. Before it was "choice" but I changed it to "n" for simplicity. Link to comment Share on other sites More sharing options...
Ingolme Posted October 15, 2016 Share Posted October 15, 2016 (edited) But on the server-side, PHP would not be able to access all the values of n. I would recommend using n[] instead so that it would be accessible as an array. Anyways, here's what you have to do: 1. When the button is clicked, check all of the checkboxes to see which ones are active and which ones are not, keep a count and store their values. 2. Generate the URL with the data you have collected. Using jQuery for this is overkill. A whole library for just a few lines of basic code is inefficient. document.getElementById("btnSearch").addEventListener("click", generateURL, false); function generateURL(e) { // var urls = []; urls[1] = "http://example.com/results?choice="; urls[2] = "http://domain.com/results?choice="; urls[3] = "http://url.com/results?choice="; // Get all the checkboxes var checkboxes = document.getElementsByName("n"); // Compile information var count = 0; var values = []; var checkbox; for(var i = 0; i < checkboxes.length; i++) { checkbox = checkboxes[i]; if(checkbox.checked) { count++; values.push(checkbox.value); } } // Generate the URL if(count > 0 && count < urls.length) { // Get base URL var url = urls[count]; url += String(count); // Add Ns for(var j = 0; j < values.length; j++) { url += "&n[]=" + values[j]; } // Display the URL alert(url); } } Edited October 15, 2016 by Ingolme Fixing variable name in the code Link to comment Share on other sites More sharing options...
dsonesuk Posted October 15, 2016 Share Posted October 15, 2016 This is rather confusing? I thought choice value related to total selected checkboxes, and n represented value of checkboxes, but apparently not, I think option 1, from first desription var urls = []; urls[1] = "http://example.com/results"; urls[2] = "http://domain.com/results"; urls[3] = "http://url.com/results"; $(document).ready(function() { $("#btnSearch").click(function() { var TotalChecked = $("input:checkbox[name=n]:checked").length; var url = urls[TotalChecked] + "?choice="; var nQueryString = ""; if (urls[TotalChecked]) { $("input:checkbox[name=n]:checked").each(function(i) { if (i === 0) { nQueryString += $(this).val(); } else { nQueryString += "&n[]=" + $(this).val(); } }); alert(url + nQueryString); } else { alert("Invalid index URL ref"); } }); }); option 2, second jsfiddle method var urls = []; urls[1] = "http://example.com/results"; urls[2] = "http://domain.com/results"; urls[3] = "http://url.com/results"; $(document).ready(function() { $("#btnSearch").click(function() { var TotalChecked = $("input:checkbox[name=n]:checked").length; var url = urls[TotalChecked]; var nQueryString = ""; if (urls[TotalChecked]) { $("input:checkbox[name=n]:checked").each(function(i) { if (i === 0) { nQueryString += "?n[]=" + $(this).val(); } else { nQueryString += "&n[]=" + $(this).val(); } }); alert(url + nQueryString); } else { alert("Invalid index URL ref"); } }); }); Link to comment Share on other sites More sharing options...
dsonesuk Posted October 15, 2016 Share Posted October 15, 2016 @ingolme you need to correct this url += "&n[]=" + values[i]; Link to comment Share on other sites More sharing options...
LRG Posted October 15, 2016 Author Share Posted October 15, 2016 choice= was synonymous with n=, sorry I forgot to change it. A count in the resulting URL wasn't really necessary by my initial means. Thank you both for your help Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now