0

I want get value from php file, so I call json_encode function in my php script. but when i get value using javascript the result is undefined.

I am using codeigniter and javascript.

Javascript

var sisa = (persentasevco / 100) * (jumlahvco)
document.getElementById('periodetk').value = periodetk
var totalt = sisa * periodet
$.ajax({
  url: '<?=base_url(\'lahan/convertString/\')?>' + totalt,
  success: function (data) {
    document.getElementById('persediaanvco').value = data.hasil
  },
})

PHP

public function convertString($value){
    $hasil = number_format($value,2,',','.');
    echo json_encode($hasil);
}

I want send $hasil from php to javascript

1 Answer 1

1

If you are just sending a string (the output from number_format), you don't need to json_encode it.

PHP

public function convertString($value){
    echo number_format($value,2,',','.'); // Echo directly
    die(); // End script
}

JS

var sisa = (persentasevco / 100) * (jumlahvco)
document.getElementById('periodetk').value = periodetk
var totalt = sisa * periodet
$.ajax({
  url: '<?=base_url(\'lahan/convertString/\')?>' + totalt,
  success: function (data) {
    document.getElementById('persediaanvco').value = data
  },
})
Sign up to request clarification or add additional context in comments.

Comments

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.