Php to echo returned value from javascript function

Asked
Active3 hr before
Viewed126 times

4 Answers

valuefunctionreturned
90%

Your approach does not work if javaScript function is async. On JavaScript I am doing async call which does not wait to return the return value in async call into result variable that causes to return undefined output @ZakariaAcharki – alper Feb 19 '19 at 11:46 , 2 PHP cannot "call" Javascript. Therefore, your Javascript function cannot "return" a value to PHP the way you are asking for. The PHP has already finished execution by the time any of the HTML/Javascript is processed. – Patrick Q Oct 31 '17 at 17:31 ,I am using the function getAddress() to get the address of the coordinates provided. How do I make sure that the returned value of the function could be echoed in php? , 3 I think you're fundamentally misunderstanding the relationship between PHP and Javascript – Patrick Q Oct 31 '17 at 17:27

The echo that you've will echo the JS script to your page and not the result so I suggest to you to add a result container then put the data inside it like :

<?php

    $lng = 103.72;
    $lat = 1.34628;

    echo "<p id='result'> </p>";

    echo "<script type='text/javascript'>".
            "document.getElementById('result').innerText = GetAddress(".$lat.",".$lng.");".
         "</script>";
?>
load more v
88%

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:, 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., 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.


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>
load more v
72%

A function can not return multiple values, but similar results can be obtained by returning an array. ,Example #2 Returning an array to get multiple values, To return a reference from a function, use the reference operator & in both the function declaration and when assigning the returned value to a variable: , Values are returned by using the optional return statement. Any type may be returned, including arrays and objects. This causes the function to end its execution immediately and pass control back to the line from which it was called. See return for more information.

load more v
65%

In the above example, the palindrome() function turns any given string into a palindrome by appending it to its reversed version. Now, instead of echoing the output directly, we assign it to a variable. We can now further manipulate the variable, but we'll just echo it in this example.,In the above example, we just passed a name to the greet() function, and it echoed a different output based on the specified name.,In the above example, we wanted the function to return a bunch of information about the name passed to it. So we used an array with mixed values. The passed values are then assigned to different variables using list().,The argument in r_palindrome() is passed by reference. This means that when we pass $word_b to r_palindrome(), the value of $word_b itself is changed to a palindrome.

Here is some basic code to define a function that outputs a greeting when we call the function later.

< ? php

function greet($name) {
   echo 'Hello '.$name.
   '!';
   echo 'Your name as '.strlen($name).
   ' letters.';
}

greet('Andrew');
// Hello Andrew!
// Your name as 6 letters.

greet('Adam');
// Hello Adam!
// Your name as 4 letters.
?
>
load more v

Other "value-function" queries related to "Php to echo returned value from javascript function"