-1

After reading through quite a few posts here, (and even just straight copying code,) I still cannot figure out why this is not working.

On my main page, I have a textbox that a user pastes data into. Upon paste, this script runs (this and the ajax query below are in the same script function):

var lines = $('textarea').val().split('\n');  //create array from pasted data

I now have a JavaScript array with the pasted data. I'm trying to send this over to a separate PHP file that I will load into a div on this main page by doing the following:

$.ajax({
      type: 'POST',
      url: 'return.php',
      data: {lines:lines},        
        success:function(data){
          $("#info").load("return.php");
        }
  });

  for (i=0; i < lines.length; i++) {            // log the output
    if (lines[i]){
    console.log("line ",  i , lines[i]);
    }  
  }

In my return.php file, I have this:

$lines = $_REQUEST['lines'];

echo '<pre>';
echo($lines);
echo '</pre>';

My console.log is outputting the array perfectly, but for some reason it is not making its way over to return.php, as my echo is blank.

What am I doing wrong?

The response I get from return.php is:

 <pre>testArrayArray
(
    [0] => DL4004-13-F
    [1] => S1G-13-F
    [2] => ZXMP3A13FTA
    [3] => B260A-13-F
    [4] => S1J-13-F
    [5] => S3B-13-F
    [6] => SSN1N45BTA
    [7] => GBJ1010-F
    [8] => BPW20RF
    [9] => T3035H-6I
    [10] => ZXMP7A17KTC
    [11] => 
)
</pre>
6
  • 1
    Have you checked in the debugger whether the data is being sent to the server? Commented Jan 28, 2015 at 20:14
  • Looking at the php_error_log file I don't see anything.... Commented Jan 28, 2015 at 20:22
  • Check your browser's debugger (the networking tab in Chrome's dev tools, for example). Commented Jan 28, 2015 at 20:23
  • Oh ok, I see. I haven't used this before, but I think I found the right object. I do see a POST action to return.php with all of the variables that are part of this. I've updated my original question with the response I get from the server... Commented Jan 28, 2015 at 20:28
  • What do you expect data to be in your ajax callback if you type 1\n2\n3 and send the ajax request? Commented Jan 28, 2015 at 20:40

2 Answers 2

1

The success handler on your ajax request is calling return.php with nothing and loading it into #info.

Change success to instead do:

$("#info").html(data)
4
  • Hmmm - so the succcess portion of the AJAX code is not like an "on success, do this." That's what I thought I was saying: "on success of posting lines to return.php, load return.php" Commented Jan 28, 2015 at 20:51
  • The initial POST succeeds & you get back the data. Then the code immediately loads return.php again but this time without any parameters so you get back an empty set of pre tags that are loaded into the #info element Commented Jan 28, 2015 at 20:57
  • so the (data) in the line success:function(data), is the actual information inside of my lines array? I don't get how return.php actually gets loaded - where in the code am I telling it to load that page? I'm confused.... :/ Commented Jan 28, 2015 at 21:03
  • 'success' is the function handler when the call to $.ajax() completes successfully and contains the output of the POSTing to the url you specified. Checkout the documentation for all the details api.jquery.com/jquery.ajax Commented Jan 29, 2015 at 3:43
1

The $lines variable in your PHP code is an array, not a string. If you have the errors turned on, PHP should warn you that you're doing an "Array to string conversion" (at least it does for me using your code).

You can't echo an array like that, you have to iterate through the results somehow.

4
  • The echo did a good enough job of showing what the array contains, didn't it? Commented Jan 28, 2015 at 20:38
  • He did say that his AJAX request returns "blank" though. I don't know enough to explain why he has the output from his $.load (that's what I understand) and not from the AJAX request. Can you really echo an array like that .. ? @JuanMendes Commented Jan 28, 2015 at 20:40
  • That's true, echo($lines) outputs Array, what the OP showed is the output of print_r($lines) sandbox.onlinephpfunctions.com/code/… Commented Jan 28, 2015 at 20:42
  • totally - thank you for that. I used print_r instead, and I get the output. Thank you so much! Commented Jan 28, 2015 at 20:49

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.