Jump to content

Recommended Posts

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!  ;)

Link to comment
https://forums.phpfreaks.com/topic/169208-compare-strings-and-count-duplicates/
Share on other sites

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.

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.

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.

 

 

 

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 :P

 

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!

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.