Page 1 of 1

PHP and javascript

Posted: Wed Jun 09, 2010 5:23 am
by anbarasu.ravi
Hi all,

I used a php for loop to create a block.the block has six fields.in javascript page i have to validate that block.(i.e) if any one field among six is empy
then it should show alert message.i have written conditions as follows

Code: Select all

var from_month = $("select[id='from_month']").map(function(){return $(this).val();}).get();
		var to_month = $("select[id='to_month']").map(function(){return $(this).val();}).get();
		var from_year = $("input[id='from_year']").map(function(){return $(this).val();}).get();
		var to_year = $("input[id='to_year']").map(function(){return $(this).val();}).get();
		var designation = $("input[id='designation']").map(function(){return $(this).val();}).get();
		var company = $("input[id='company']").map(function(){return $(this).val();}).get();
the above are the six fields which has multiple values (ie) array.

now i am writing condition as

Code: Select all

for(var i=0;i<3;i++)
{
if(from_month[i] != 'Month' && to_month[i] == 'Month' && from_year[i] == 'Year' && to_year[i] == 'Year' && designation[i] == 'Designation' && company[i] == 'Company')
{	alert("Enter all details for Experience "+(i+1));
	break;
}
else if(from_month[i] != 'Month' && to_month[i] != 'Month' && from_year[i] == 'Year' && to_year[i] == 'Year' && designation[i] == 'Designation' && company[i] == 'Company')
{	alert("Enter all details for Experience "+(i+1));
		break;
}
else if(from_month[i] != 'Month' && to_month[i] != 'Month' && from_year[i] != 'Year' && to_year[i] == 'Year' && designation[i] == 'Designation' && company[i] == 'Company')
{
					alert("Enter all details for Experience "+(i+1));
					break;
}
etc
}

if i write conditions like this the number of lines of code is more.so any one have idea to make it simple using any loop..?

Re: PHP and javascript

Posted: Wed Jun 09, 2010 6:54 am
by anbarasu.ravi
I think this wil help u..

Code: Select all

<select title="State" name = "state" id = "state" class="mandatory_field">
			<option value="State">State</option>
			<?foreach($state as $var){?>
			<?if($var['state_name'] == $data['state']){?>
			<option value="<?=$var['state_name']?>"  selected="selected" ><?=$var['state_name']?></option>
			<?php }else{?>
			<option value="<?=$var['state_name']?>" ><?=$var['state_name']?></option>
			<?php }}?>
</select>