9 Replies - 1306 Views - Last Post: 10 October 2013 - 12:41 PM Rate Topic: ***-- 2 Votes

#1 brerallia   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 164
  • Joined: 21-January 13

If list in list menu is selected, or change event

Posted 05 October 2013 - 04:06 AM

I have a list menu.... this is also called as combobox or dropdown menu....
in this menu, i have two list --

Vacation Leave and Sick Leave....

I also have a check box that will be automatically be disabled when the page loads if the user is a Part-time employee...

So how can i have this event that when i click on vacation leave and she has 0 days in the database it will automatically be disabled with out clicking a button but just clicking a list menu??? can php do this or javascript will do?? either of them, then how to??

ANY HELP WILL BE APPRECIATED...

Is This A Good Question/Topic? 0
  • +

Replies To: If list in list menu is selected, or change event

#2 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3786
  • View blog
  • Posts: 13,715
  • Joined: 08-August 08

Re: If list in list menu is selected, or change event

Posted 05 October 2013 - 07:29 AM

You can use Ajax, which is Javascript sending a request to the server to run a PHP script which sends back information to Javascript. You could also use jQuery.
Was This Post Helpful? 1
  • +
  • -

#3 brerallia   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 164
  • Joined: 21-January 13

Re: If list in list menu is selected, or change event

Posted 05 October 2013 - 06:33 PM

View PostCTphpnwb, on 05 October 2013 - 07:29 AM, said:

You can use Ajax, which is Javascript sending a request to the server to run a PHP script which sends back information to Javascript. You could also use jQuery.



How?? what keywords should i use to find it in google...???
Was This Post Helpful? 0
  • +
  • -

#4 Atli   User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4240
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: If list in list menu is selected, or change event

Posted 05 October 2013 - 07:38 PM

"AJAX" would be a good start. Just Google'ing that word alone should give you a bunch of good resources on the first page. - It does require basic Javascript knowledge though, so if your Javascript fundamentals aren't strong, you may want to read a tutorial or two on the basics of that first. AJAX, after all, is just a tool used by Javascript code.
Was This Post Helpful? 1
  • +
  • -

#5 brerallia   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 164
  • Joined: 21-January 13

Re: If list in list menu is selected, or change event

Posted 09 October 2013 - 05:23 AM

I have a drop down menu that has 2 values:
vacation leave and sick leave...

i also have to retrieve data from the database to see if that particular employee/user has 0 vacation leave days or 0 sick leave days...

so if that user clicks on the drop down menu and selects the vacation leave and has 0 vacation leave days from the database then there will be a checkbox that will be disabled...

if that user clicks on the drop down menu and selects the sick leave and has 8 sick leave days from the database then that checkbox will be enabled...

i had so much trouble in creating this code... i havent even started cause its my 4th day as a beginner in javascript so i know little only about it....

this is my logic in php:

if ($_POST['leave_type'] == "Vacation Leave" and $result_credit['vacation_leave'] == '0')
							{
								echo 'disabled="disabled"';
								$type_msg = "<span class='error'>You have consumed your vacation leave days.</span>";
							}
else
							{
								echo 'checked'; 
								$type_msg = "Yes";
							}


$result_credit is a variable that holds the data from the database...
this code works fine but it isnt accurate or user friendly because, that condition will only work if i clicked a submit button...

when the page loads first and that particular employee has selected vacation leave and has 0 vacation leave days without pressing the submit button first then the checkbox will remain selected and it the disable wont function... but when i click the submit button then the error executes....

so in order for my system to be accurate then i need to set an onchange in the dropdown menu right??? i know how that condition works in javascript but how to insert the data retrieve in database...

this is my logic in javascript:

<select name="leave_type" id="leave_type" onchange="selectedleavetype()">
                        <option <?php if(($_POST['leave_type']) == "Vacation Leave") echo "selected='selected'";?> >Vacation Leave</option>
                        <option <?php if(($_POST['leave_type']) == "Sick Leave") echo "selected='selected'";?> >Sick Leave</option></select>


<input type="checkbox" name="pay" value="yes" id="pay">



this is just for the vacation leave....

function selectedleavetype(){
	if (document.getElementById('leave_type').value == "Vacation Leave" and //the data from the database if she has 0 vacation leave days)
		document.getElementById('pay').disabled = false;
        else
{
		document.getElementById('pay').disabled = true;
document.getElementById('pay').checked = true;
}
}


ANY HELP WILL BE APPRECIATED...

This post has been edited by brerallia: 09 October 2013 - 05:24 AM

Was This Post Helpful? 0
  • +
  • -

#6 Atli   User is offline

  • Enhance Your Calm
  • member icon

Reputation: 4240
  • View blog
  • Posts: 7,216
  • Joined: 08-June 10

Re: If list in list menu is selected, or change event

Posted 09 October 2013 - 06:25 AM

Duplicate threads merged. Please don't double post your questions.

Like CTphpnwb pointed out, if you need to get data from the server into Javascript after the page has been loaded, you need to use AJAX. It sends a new request to the server without having to reload the entire page. - The MDN page on AJAX has all the info you could ever need to use this in your code.

This post has been edited by Atli: 09 October 2013 - 06:25 AM

Was This Post Helpful? 1
  • +
  • -

#7 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3786
  • View blog
  • Posts: 13,715
  • Joined: 08-August 08

Re: If list in list menu is selected, or change event

Posted 09 October 2013 - 07:34 AM

Quote

this is my logic in javascript:
<select name="leave_type" id="leave_type" onchange="selectedleavetype()">
                        <option <?php if(($_POST['leave_type']) == "Vacation Leave") echo "selected='selected'";?> >Vacation Leave</option>
                        <option <?php if(($_POST['leave_type']) == "Sick Leave") echo "selected='selected'";?> >Sick Leave</option></select>

No, it is not. It is HTML embedded in PHP and written by the PHP interpreter.


You need to get straight in your head the difference between client and server. If you don't understand that these languages can be and often are separated by thousands of miles and many seconds/hours of time as well as completely different operating systems/computers then you have no hope of getting this.

This post has been edited by CTphpnwb: 09 October 2013 - 07:39 AM

Was This Post Helpful? 1
  • +
  • -

#8 brerallia   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 164
  • Joined: 21-January 13

Re: If list in list menu is selected, or change event

Posted 10 October 2013 - 05:51 AM

I have this code:

the drop down list
<select name="leave_type" id="leave_type" onchange="selectedleavetype()" class="input_list">
                        <option>Select a leave type</option>
                        <option <?php if(($_POST['leave_type']) == "Vacation Leave") echo "selected='selected'";?>>Vacation Leave</option>
                        <option <?php if(($_POST['leave_type']) == "Sick Leave") echo "selected='selected'";?>>Sick Leave</option></select>


the checkbox
<input type="checkbox" id="pay" name="pay" value="yes">

the script
 <script type="text/javascript">
	function selectedleavetype(){
		var vl = <?php echo $result_credit['vacation_leave']; ?>;
		var sl = <?php echo $result_credit['sick_leave']; ?>;
		var status = "<?php echo $result_credit['status']; ?>";
		if (status == "Regular Employee")
		{
			if (document.getElementById('leave_type').value == "Sick Leave" && sl == 0)
			{
				document.getElementById('pay').checked = false;
				document.getElementById('pay').disabled = true;	
			}
			else if (document.getElementById('leave_type').value == "Sick Leave" && sl != 0)
			{
				document.getElementById('pay').checked = true;
				document.getElementById('pay').disabled = false;
				var msg = "Yes";
			}
			else if (document.getElementById('leave_type').value == "Vacation Leave" && vl == 0)
			{
				document.getElementById('pay').checked = false;
				document.getElementById('pay').disabled = true;	
				var msg = "You have consume your vacation leave days.";
			}
			else if (document.getElementById('leave_type').value == "Vacation Leave" && vl != 0)
			{
				document.getElementById('pay').checked = true;
				document.getElementById('pay').disabled = false;
				var msg = "Yes";
			}
			else
				document.getElementById('pay').disabled = false;
		}
	}
	</script>


so, as you can see there is a drop down list, a checkbox and a variable $result_credit which holds the data from the database....

when the user selects vacation leave from the dropdown list and has a vacation leave days in the database of 0 then the checkbox will be disabled... this javascript works right....
but, beside the checkbox in the interface, i want the user to know why the checkbox is disabled.... so i search in google how to and i got this code:

for example in the first condtition:

if (status == "Regular Employee")
		{
			if (document.getElementById('leave_type').value == "Sick Leave" && sl == 0)
			{
				document.getElementById('pay').checked = false;
				document.getElementById('pay').disabled = true;	
var msg = "You have consume your sick leave days.";
<?php $a= "<script>document.write(msg)</script>"?>   
			}


you can see that i added a
var msg = "You have consume your sick leave days.";
<?php $a= "<script>document.write(msg)</script>"?>


and in the html it should be:
<input type="checkbox" id="pay" name="pay" value="yes"><?php echo $a; ?>


and call the variable $a...
but it isnt working....!!!! i viewed the page source of my interface and the value of <?php echo $a; ?> is:
<script>document.write(msg)</script>


whys isnt it working???
ANY HELP WILL BE APPRECIATED...
Was This Post Helpful? -1
  • +
  • -

#9 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: If list in list menu is selected, or change event

Posted 10 October 2013 - 06:04 AM

Merged topics. Stop opening new topics for the SAME QUESTION!

You've been given what to research here, but haven't shown an inkling of wanting to do that. Read what you've been told, do the research!
Was This Post Helpful? 0
  • +
  • -

#10 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3786
  • View blog
  • Posts: 13,715
  • Joined: 08-August 08

Re: If list in list menu is selected, or change event

Posted 10 October 2013 - 12:41 PM

brerallia, you're not going to get this working without knowing the answers to these questions.
Where does PHP run?
Where does Javascript run?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1