2

i'm doing this in codeigniter

here is my php switch - case part

case 'check':
$balance = $this->Model_transactions->getUserBalance($this->input->post('userId'));
$needToPay = floatval($this->input->post('tuitionRate')) / 2; // 50%
if ($balance >= $needToPay) {
$results = '{"Result": "OK";"Balance":'.$balance.'}';
}
break;

here is the json code

$.ajax({
url: base_url + "api/hires/check/?callback=?",
type: "POST",
dataType: "jsonp",
data: {
userId: $(".navigation").data("login"),
tuitionRate: t.find("#txt-hire-rate").val()
}
}).done(function (a) {
if (a) if ("OK" != a.Result) {
alert (a.Balance);

what i want is to use php $balance variable in my jQuery. please help.

3 Answers 3

1

Do not try to build the string literal, instead, construct an array and use json_encode

$results = json_encode(array('Result' => 'OK', 'Balance' => $balance), true);

Then return it when you're ready.

return $results;
Sign up to request clarification or add additional context in comments.

1 Comment

got it Thank you! but how can i use this in jquery. here what i'm trying to do. .done(function (a) { var data = $.parseJSON(a); alert (data.Result);
1

Edit your php script

case 'check':
$balance = $this->Model_transactions->getUserBalance($this->input->post('userId'));
$needToPay = floatval($this->input->post('tuitionRate')) / 2; // 50%
if ($balance >= $needToPay) {
$data = array(
  'Result' => 'OK',
  'Balance' => $balance 
);
echo json_encode($data);
}
break;

Edit your jQuery Script

$.ajax({
 type: "POST",
 url: '<?php echo base_url()."api/hires/check/?callback=?";?>',
 dataType: 'json',
 data:{userId: $(".navigation").data("login"),
 tuitionRate: t.find("#txt-hire-rate").val()},
 success: function(data){
            if(data.Result != "OK"){
                    alert(data.Balance);
            }
           }
 });

Comments

1

php switch - case part

    case 'check':
    $balance = $this->Model_transactions->getUserBalance($this->input->post('userId'));
    $needToPay = floatval($this->input->post('tuitionRate')) / 2; // 50%
    if ($balance >= $needToPay) {
    $results = '{"Result": "OK";"Balance":'.$balance.'}';

    // add this in your code 
    <script>
       document.getelementbyId('balance').innerHTML=$balance;
    <script>
    }
    break;
    ?>
    <div id='balance' style='display:none;'></div> // added in the code

json code

var bal=$(#balance).html(); // use this variable where you want to
$.ajax({
url: base_url + "api/hires/check/?callback=?",
type: "POST",
dataType: "jsonp",
data: {
userId: $(".navigation").data("login"),
tuitionRate: t.find("#txt-hire-rate").val()
}
}).done(function (a) {
if (a) if ("OK" != a.Result) {
alert (a.Balance);

I did changes in php code and added the variable in js which can be used anywhere in js you want to.

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.