mayfair Posted August 7, 2009 Share Posted August 7, 2009 Hello guys Im trying to compare the values of a pair of select boxes "type1" and "currency1" that sit happily together in a table row. The problem is that the user is able to dynamically add a new table row at the click of a button - adding a new set of select boxes and renaming them "type2", "currency2" etc. up to a maximum of 4 pairs. Now I need to make sure the values of any "type" AND "currency" boxes do not occur more than once throughout the selection, and trying to be clever ive written a function which almost works without having to write a great list of compare statements. I say almost works because im stuck at the actual comparison part. Here's the code: function checkDup() { var identicalStrings = 0; var selected1; var selected2; var selected3; var selected4; selected1 = document.currency.type1.value + document.currency.currency1.value; selected2 = document.currency.type2.value + document.currency.currency2.value; selected3 = document.currency.type3.value + document.currency.currency3.value; selected4 = document.currency.type4.value + document.currency.currency4.value; } Now what im trying to do is concatenate the values of the pairs of select boxes into a single string "selected". The idea is to then count the amount of times the value of "selected" is identical and increment var identicalStrings by 1. I can then simply run an IF statement to display an error if identicalStrings is greater than zero. I hope ive explained myself well, any help as always would be greatly appreciated! Quote Link to comment Share on other sites More sharing options...
Bjom Posted August 7, 2009 Share Posted August 7, 2009 Put the values into an array and use array_unique() on it. That will strip the duplicates. If you need to check if there were duplicates, simply compare the count() on both arrays. Quote Link to comment Share on other sites More sharing options...
mayfair Posted August 7, 2009 Author Share Posted August 7, 2009 Thanks for your reply Bjom, but I would like to do this without using PHP if possible. Im guessing arrays is the way to go, are there any client-side options for counting duplicates? Quote Link to comment Share on other sites More sharing options...
haku Posted August 7, 2009 Share Posted August 7, 2009 When are you trying to do this check - before adding the new boxes, or after adding them and on submission, or when? Quote Link to comment Share on other sites More sharing options...
mayfair Posted August 7, 2009 Author Share Posted August 7, 2009 Ive got it coded into an onChange event for the select boxes themselves. If worst comes to the worst i'll bind it to the Submit button, but ideally id like to stop the duplicates before they happen, not after Quote Link to comment Share on other sites More sharing options...
haku Posted August 7, 2009 Share Posted August 7, 2009 I guess I didn't ask the right question. What are you trying to do with this function? Is this after the checkboxes have been added? Before? Are you trying to prevent the checkboxes from being added if they already exist, or trying to only take one submission if they are doubled? Or something else. Quote Link to comment Share on other sites More sharing options...
mayfair Posted August 7, 2009 Author Share Posted August 7, 2009 Oh sorry, im trying to prevent the user from selecting the same pair of select boxes twice. This is part of a foreign currency ordering system and the first select box "type" indicates the type of currency (either cash or travellers cheques) and the second select box "currency" is the type of currency to be ordered. The goal is to stop people from ordering for example 2 types of travellers cheques for the same currency. Quote Link to comment Share on other sites More sharing options...
haku Posted August 7, 2009 Share Posted August 7, 2009 I gotta admin, you've lost me. Not sure exactly what you mean. How can they select the same pair of checkboxes twice - once the check has been inserted, its inserted - how can they check a box that's already checked? Quote Link to comment Share on other sites More sharing options...
mayfair Posted August 7, 2009 Author Share Posted August 7, 2009 It's selectboxes not checkboxes. Sorry should have made myself more clear. Select box/drop down list. Quote Link to comment Share on other sites More sharing options...
RichardRotterdam Posted August 7, 2009 Share Posted August 7, 2009 Put the values into an array and use array_unique() on it. That will strip the duplicates. If you need to check if there were duplicates, simply compare the count() on both arrays. You could use php.js to make this easier it has a js equivalent for array_unique in js http://phpjs.org/functions/array_unique:346 You are probably aware of it but just making sure so I'm asking anyway. You do realize that you shouldn't rely on js validation always do serverside validation since js can be turned off. Hello guys Im trying to compare the values of a pair of select boxes "type1" and "currency1" that sit happily together in a table row. this would mean you would have HTML similar(without tables) to the following correct? <select name="type1"> <option value="val1">val1</option> <option value="val2">val2</option> <option value="etc">etc</option> </select> <select name="currency1"> <option value="val1">val1</option> <option value="val2">val2</option> <option value="etc">etc</option> </select> <select name="type2"> <option value="val1">val1</option> <option value="val2">val2</option> <option value="etc">etc</option> </select> <select name="currency2"> <option value="val1">val1</option> <option value="val2">val2</option> <option value="etc">etc</option> </select> <select name="type3"> <option value="val1">val1</option> <option value="val2">val2</option> <option value="etc">etc</option> </select> <select name="currency3"> <option value="val1">val1</option> <option value="val2">val2</option> <option value="etc">etc</option> </select> Selecting all select elements is a piece of cake with jQuery's like selector function But then again you might not want to use jQuery but it's there if you like. Quote Link to comment Share on other sites More sharing options...
mayfair Posted August 7, 2009 Author Share Posted August 7, 2009 Hi Dj Kat, thanks for your reply. I had looked at the php.js function but decided against using it because I thought there had to be an easier way. As I said before, it'd be just as quick to bash out multiple compare statements in JS but it looks a bit ugly and I felt up to a challenge You are probably aware of it but just making sure so I'm asking anyway. You do realize that you shouldn't rely on js validation always do serverside validation since js can be turned off. I realise this, but as the tables are created dynamically by JS anyway there won't be multiple fields to compare if JS is turned off - just one single row which is good enough to put a single order through. I'll keep experimenting anyway, bound to come up with something sooner or later! Quote Link to comment Share on other sites More sharing options...
RichardRotterdam Posted August 7, 2009 Share Posted August 7, 2009 You're prob not waiting for this but since I am a bit of a js junky I made a jQuery script that sorta does what you want. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ var typeSelectElements = $("select[name^='type']"); //console.log(typeSelectElements); typeSelectElements.live('change',function(){ // fetch all current element values var valuesToCheck = new Array(); $.each(typeSelectElements, function() { valuesToCheck.push($(this).val()); }); var valueCount=0; for(var i = 0; i < typeSelectElements.length; i++){ if($(typeSelectElements[i]).val() === $(this).val()){ valueCount++; } if(valueCount>1){ alert('error duplicate entries'); break; } } }); }); </script> <select name="type1"> <option value="">select one</option> <option value="val1">val1</option> <option value="val2">val2</option> <option value="etc">etc</option> </select> <br /> <select name="type2"> <option value="">select one</option> <option value="val1">val1</option> <option value="val2">val2</option> <option value="etc">etc</option> </select> <br /> <select name="type3"> <option value="">select one</option> <option value="val1">val1</option> <option value="val2">val2</option> <option value="etc">etc</option> </select> <br /> I realise this, but as the tables are created dynamically by JS anyway there won't be multiple fields to compare if JS is turned off - just one single row which is good enough to put a single order through. I'll keep experimenting anyway, bound to come up with something sooner or later! You can also just create a form and set the action to your page so it's still no safty Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.