0

In my project I need to call PHP function inside Javascript file. I have a file called Test.php. Inside this file I have function called getValue().This function will return some value:

<? php
function getValue()
{
echo json_encode('84');
}
?>

Also, I have Javascript file called myScript.js. Inside this file I want to call detVale(); and save the return value in var knobValue. I tried this but it did not work

var knobValue;

$.post('Test.php', function(result) {
     knobValue=result;
}, 'json');


$.get('Test.php', function(result) {
  knobValue=result;
});

$.ajax({
      type: 'POST',
      url: 'Test.php',
      dataType: 'json',
      cache: false,
      success: function(result) {
         knobValue=result;
      }
 });
4
  • 1
    Try adding some error handling to your AJAX call in example 3. You can add statusCode: { 404: function(){alert('404');}, 500: function(){alert('500');}} to start. This is part of how you can identify what the error is. Commented Apr 23, 2015 at 17:14
  • I tried what you said, at first I got 404 error then I changed the url. the error stop appearing but still the code does not work. Commented Apr 23, 2015 at 17:39
  • Can you expand upon how it doesn't work? Commented Apr 23, 2015 at 18:59
  • After I get the information from the php I want to show the result in input text field. $(".dial-div").append('<div class="inline" name="'+item.options_index+'"><div class="m-r-md inline"><input type="text" value="'+knobValue+'" class="dial m-r-sm" data-width="85" data-height="85" disabled="true" /></div><h5>'+this.form_field.options[item.options_index].value+'</h5></div>');. Put I got NAN. Commented Apr 24, 2015 at 13:06

1 Answer 1

6

My guess on the limited information provided is you've not called the getValue() function.

<?php
    function getValue()
    {
        echo json_encode('84');
    }

    getValue();
?>
Sign up to request clarification or add additional context in comments.

3 Comments

I tried that put still no result. What kind of information do you need to help me?
In your browsers development console does the ajax call have any data returned?
No it does not have any data

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.