Algernop K.
Algernop K.

Reputation: 477

Return php variable?

I have made a little AJAX-script for my site, which executes a php-script in another file on submission. I managed to echo out the result in the original file with the AJAX-function, but I have not managed to transfer a variable from the php file to the original one.

I need this variable in order to add an event listener which will look for changes in that particular variable (not sure how to do that either).

Upvotes: 2

Views: 894

Answers (4)

Himanshu
Himanshu

Reputation: 281

Here's are what you are looking for it's working:- Put this in your forsok.php

<div id="input">
<input type="text" id="number" name="value">
<b id="show_result"></b>
</div>`
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
$('#number').on('keyup',function(e){
if(e.which == 13){
var get_var_name = $(this).val();
 $.get('result.php',{number:get_var_name},function(data,status){
if(status == 'success'){
 alert(data['show']);
 $('#show_result').text(data['show']);
}else{
 alert('Nothing');
}
});
}
});
</script>

For hej.php:-

<?php


$one=$_GET['number'];
if(empty($one)) {
    echo "Can't be blank";
    $a['result']='null';
    $a['error'] = 'No value!!';
} else {
    if(is_numeric($one)) {
        $show=$one*2;
        $arr = array(
        'show'=>$show
        );
        header('Content-Type:application/json');
        echo json_encode($arr);
        exit();
       // echo $show;
    } else {
        echo "NaN";
        $a['result']='null';
        $a['error']='nan';
    }



}
?>

Upvotes: 1

Stefan Dochow
Stefan Dochow

Reputation: 1454

Instead of echoing strings here and there in in hej.php it might better to return JSON data to your ajax call. so you can evaluate if an error occured, which error it is or which valid result has been returned.

hej.php:

<?php
    $one=$_GET['value'];
    if(empty($one)) {
        $a['result']='null';
        $a['error'] = 'No value!!';
    } else {
        if(is_numeric($one)) {
            $a['result']=$one*2;
            $a['error']='ok';
        } else {
            $a['result']='null';
            $a['error']='nan';
        }
    }
    die(json_encode ($a));
?>

if $value was 1 that would return

{"result":"2","error":"ok"}

In forsok.php you could check the reults and act accordingly

...
$.ajax({
    type: "GET",
    dataType: "json",
    url: url,
    data: $("#idForm").serialize(), // serializes the form's elements.
    success: function(response)
    {
        if (response.error=='ok'){
            $('#utmatning').html(response.result); // show response from the php script.
        }
        else{
            console.log(response.result); // handle the error
        }
    }
});
...

Regards, Stefan

Upvotes: 1

Niranjan N Raju
Niranjan N Raju

Reputation: 11987

At last of your php file, add,

json_encode($a);

In ajax success,

success: function(html) {
    $.each(html, function(index, element) {
        alert(element.result);
        alert(element.error);
        //append to which ever div you want.
    });
}

Now with this, you can get n number of array indexes from php

Upvotes: 1

Alvin Bakker
Alvin Bakker

Reputation: 1526

First create an array of what should be the output. JSON encode that array and then you can parse the output in your ajax success handler. Like in your php file output like:

echo json_encode(array(
    'result' => 'null',
    'error' => 'nan'
));

Then in you ajax success turn the json into an object and parse data as you want:

success: function (data, textStatus, jqXHR) {
    var obj = $.parseJSON(data);
    $('#utmatning').html(obj.result); // result value from your json return
    $('#utmatning').append(obj.error); // error value from your json return
}

Upvotes: 1

Related Questions