Hello,

This is a JavaScript/PHP question so I will go ahead and post it here.

I would like to know if it is possible to access (in a PHP class) a value/variable that has been returned by a JavaScript function. The function is in external .js class.

Here what I am trying to do:
(1) create a variable called 'userInfo' to store information we want to display
(2) if user selects 'Duplicate TR' for example the variable 'userInfo' contains text 'Fill in TR num ….'
(3) return the variable
(4) in the php code AnalyseFaultView I'm trying to echo the return value but it doesn't seem to work

If anybody could help me out it would be much appreciated.

Rgds,

Gary888


Utitlity.js
******************************************************
******************************************************

function AnalyseFaultCheck(){
 	var reasonDropdownList=document.getElementById("whatisthereasonforfaultslippage");
 	var statusDropdownList=document.getElementById("status");
 	var updateButton=document.getElementById("update");
 	var proposedActions=document.getElementById("proposedactions");
 		
	var userInfo='';
 	


 if("Duplicate TR"==reasonDropdownList.options[reasonDropdownList.selectedIndex].value && proposedActions.value=='')){
	userInfo="Fill in TR num of original TR";
	return userInfo; 

 	statusDropdownList.disabled=true;
	updateButton.disabled=true;
		
 }
 
 if("Duplicate TR"==reasonDropdownList.options[reasonDropdownList.selectedIndex].value && proposedActions.value!='')){
 	userInfo="Fill in TR num of original TR";
	return userInfo; 

 	statusDropdownList.disabled=false;
	updateButton.disabled=false;
 }
******************************************************
******************************************************


AnalyseFaultView.php
******************************************************
******************************************************
<!--  #4 Proposed action: -->
	<tr>
		<td class="admin"><b><font color='black' face='arial' size='2'><?php echo $dataRow->GetDataRowItemCaptionByName("Proposed_Actions");?></font></b></td>
		<td class="admin"><font color='black' face='arial' size='2'> <?php echo $dataRow->GetDataRowItemByName("Proposed_Actions");?></font>
		<br>
		 
		<?php echo '<script>AnalyseFaultCheck();</script>'; ?>		
		
		<textarea id="proposedactions" name="proposedactions" onkeyup="AnalyseFaultCheck()" rows="10"
			cols="30" style="width:100%"></textarea></td>
	</tr>

******************************************************
******************************************************

Recommended Answers

All 4 Replies

Is your problem not getting the function to work, or that PHP is not echoing the <script> tag?

It seems that you don't have correct closing brackets in your AnalyseFaultCheck() function. Here's how it looks spaced out:

function AnalyseFaultCheck(){
	var reasonDropdownList=document.getElementById("whatisthereasonforfaultslippage");
	var statusDropdownList=document.getElementById("status");
	var updateButton=document.getElementById("update");
	var proposedActions=document.getElementById("proposedactions");

	var userInfo='';



	if("Duplicate TR"==reasonDropdownList.options[reasonDropdownList.selectedIndex].value && proposedActions.value=='')){
		userInfo="Fill in TR num of original TR";
		return userInfo;

		statusDropdownList.disabled=true;
		updateButton.disabled=true;

	}

	if("Duplicate TR"==reasonDropdownList.options[reasonDropdownList.selectedIndex].value && proposedActions.value!='')){
		userInfo="Fill in TR num of original TR";
		return userInfo;

		statusDropdownList.disabled=false;
		updateButton.disabled=false;
	}

For one thing, in your conditional statements, the codes

statusDropdownList.disabled=false;
updateButton.disabled=false;

Won't run because they are placed AFTER the return statement. You should always place the return statement as the last line of your function because it halts the function processing and does not process the rest of the function. It just ends the function and returns the value.

Also, you don't have an end bracket for the entire function. Do you change what's in userInfo ? Otherwise, you don't have to change the variable in conditional.

You also added one too many parentheses in your conditionals.

Try changing your code to this:

function AnalyseFaultCheck(){
	var reasonDropdownList=document.getElementById("whatisthereasonforfaultslippage");
	var statusDropdownList=document.getElementById("status");
	var updateButton=document.getElementById("update");
	var proposedActions=document.getElementById("proposedactions");

	var userInfo="Fill in TR num of original TR";

	if("Duplicate TR"==reasonDropdownList.options[reasonDropdownList.selectedIndex].value){
		if (proposedActions.value == '') {
			statusDropdownList.disabled=true;
			updateButton.disabled=true;
		}
		else {
			statusDropdownList.disabled=false;
			updateButton.disabled=false;
		}
		return userInfo;
	}
}

itsjareds first of all thanks for your reply.

The main issue I have is echo-ing the return value 'userInfo' in the PHP class.

I have a feeling that the code <?php echo '<script>AnalyseFaultCheck();</script>'; ?> is causing the problem here. When I try to echo the javascript function (ie the return value) from the PHP class nothing happens.

I have added the closing bracket and tried your code but still no joy.

So to recap:

Im trying to use JavaScript to change a variable ('userInfo') depending on the selection made in the dropdown list (reasonDropdownList). If the reason 'Duplicate TR' is chosen, I want to change 'userInfo' string variable text to say 'enter TR num'. Then when this is changed I want to return the variable and be able to access the variable (ie the string text) in the PHP so I can output it in the web page.

Hopes this makes sense :-)

I have been searching the web and many forums state that Javascript is client side and PHP server side so it will not work.

Any other tips are welcome.

Cheers

Re-implement the following lines inside your utility.js:

var userInfo;
var AnalyseFaultCheck = function() { 
var reasonDropdownList = document.getElementById ("whatisthereasonforfaultslippage"); 
var statusDropdownList = document.getElementById ("status"); 
var updateButton = document.getElementById ("update"); 
var proposedActions = document.getElementById ("proposedactions"); 
var userInfo = '';
   if ( "Duplicate TR" === reasonDropdownList.options [ reasonDropdownList.selectedIndex ].value && proposedActions.value === "" ) { 
   statusDropdownList.disabled = true ; 
   updateButton.disabled = true;
   return userInfo = "Fill in TR num of original TR";  
   } else if ( "Duplicate TR" === reasonDropdownList.options [ reasonDropdownList.selectedIndex ].value && proposedActions.value !== "" ) {     statusDropdownList.disabled = false;    updateButton.disabled = false;
   return userInfo = "Fill in TR num of original TR";
 } else { return alert( "Cannot create this variable ( userInfo )." );
   }
};

and load it on the same spot : <?php echo '<script type="text/javascript" src="./utility.js"></script>'; ?> , or if the utility.js has already been loaded in the page, then you can just simply call it anywhere in page w/o using any of the <script></script> tag.

Let me know how things worked...

I have just solved this issue by taking a different approach.

Thanks anyhow for the replies !

Instead of trying to echo the returned JavaScript variable in the PHP class I have created a <textarea> element in the PHP class.
I then call this element in JavaScript using 'var userInfo = document.getElementById ("userInfo"); ' and depending on the user choice I change the value of the element, for example: userInfo.value="To be analysed";

This did the trick.


Here the code

function AnalyseFaultCheck(){
	var reasonDropdownList=document.getElementById("whatisthereasonforfaultslippage");
	var statusDropdownList=document.getElementById("status");
	var updateButton=document.getElementById("update");
	var proposedActions=document.getElementById("proposedactions");
	var userInfo=document.getElementById("userinfo");
	 	 
 	if("To be analysed"!=reasonDropdownList.options[reasonDropdownList.selectedIndex].value && proposedActions.value!=''){
 		statusDropdownList.disabled=false;
 		updateButton.disabled=false;
 	}
 	
 	if("To be analysed"==reasonDropdownList.options[reasonDropdownList.selectedIndex].value || proposedActions.value==''){
 		userInfo.value="To be analysed";
 		statusDropdownList.disabled=true;
 		updateButton.disabled=true;
 	}
 	 
 	
 	//inform user what to fill in depending on the chosen option 
 	if("Duplicate TR"==reasonDropdownList.options[reasonDropdownList.selectedIndex].value){
 	 	userInfo.value="Enter 'No Action Required' and incl original TR number";
 	 	}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.