0

I dont know if i'm doing this the wrong way but I can't think of another way to do it.

I have a function in php that i'm calling with $.post and I want it to return an array. It sounds like I need to use a callback function and i'm trying but it just wont work.

Here is the code:

Jquery:

$("#e_del_date_btn").click(function(){
    var new_date = $("#edited_del_date").val();
    if (new_date == '') {new_date = $("#edited_del_date").attr("placeholder");}
    var new_time = $("#edited_del_time").val();
    var non = $("#non").html();
    function getarray(new_date, new_time, non, callback) {
        $.post('http://localhost:8080/wsad2/post.php',{'new_date':new_date,'new_time':new_time,'non':non}, function(data) {
            callback(data);
        });
    }
    getarray(new_date,new_time,non, function(data) {
        alert(data[0]);
        $("#odi_span").html(data[0]);
        $("#e_del_date_pencil").attr("value",data[1]);
        $("#e_del_date_pencil").attr("value2",data[2]);
    });
    $("#e_del_date").hide();
    $("#e_del_date").css("z-index","0");
    $("#po_main_div_test").css({ opacity : 1.0 });
});

PHP Code:

$returndata = array();
$returndate = $returntime = $returntext = '';

if ($fail == "false") {if ($row['complete'] == "no") {$returntext .= '(When Completed) ';}}
$returntext .= 'This order is scheduled to be delivered on<br>';
if ($fail == "false") {$returntext .= $new_date_2;$returndate = $new_date_2;} else {$returntext .= $orig_deldate;$returndate = $orig_deldate;}
$returntext .= ' between ';
if ($fail == "false") {$returntext .= $new_time_2;$returntime = $new_time_2;} else {$returntext .= $orig_time;$returntime = $orig_time;}
if ($fail == "true") {$returntext .= '<br>The New Delivery Date must be tomorrow or later.';}

$returndata[0] = $returntext; 
$returndata[1] = $returndate; 
$returndata[2] = $returntime; 

//echo ($returntext);

return $returndata;

from some of the things I've read I might be trying to use $.post improperly, but basically I need to pass the date/time variables and then have the php return the value but they can be changed in the PHP code (which is the point of this, to edit a delivery date/time) so i need the value sent from the php back to the JavaScript in case it was changed, so i can update the attribute in case they decide to change it again w/out refreshing the page.

My problem is, the alert is always blank (the alert is for testing purposes) and basically it SEEMS to be working, but the 3 jquery calls in the getarray() function do not seem to be firing, or if they are they aren't working. right now the HTML field i'm updating doesn't change no matter what I do.

am I using the callback function improperly? I really need $.post to return an array, not just echo data so that I can update multiple fields instead of just one.

Also, i've left out some of the php code as I didn't think it was relevant. i just wanted to show the creation of the array and it being filled with data.

1
  • you aren't printing anything by using return $returndata , try echo json_encode($returndata); Commented Nov 18, 2014 at 21:18

2 Answers 2

2

You can echo an array with PHP with JSON:

PHP: http://php.net/manual/en/function.json-encode.php

Javascript:

http://api.jquery.com/jquery.parsejson/

Furthermore: How to send a proper POST request with JQuery:

$.ajax({
   url: "test.php",
   data: {my_$_Post1: "value"}
}).done(function() {
   //whatever you want to do
});

Or(NOT RECOMMANDED):

Answer = $.ajax({
   url: "test.php",
   async: false
   data: {my_$_Post1: "value"}
});
alert(Answer);

http://api.jquery.com/jquery.ajax/

Sign up to request clarification or add additional context in comments.

1 Comment

async: false is pretty much NEVER recommended as it just locks up the browser during the ajax call and is not a good design pattern for browser/javascript development.
1

One option is to print the data from your PHP code as a JSON (JavaScript) array. Then use JSON.parse() in your JavaScript code to parse the text and turn it into an array.

1 Comment

the json_encode echo with a JSON.parse solution worked great. Took me a bit to figure it out exactly but i got a working version. Thanks!!

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.