#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    15
    Rep Power
    0

    Javascript to PHP


    I am trying to figure out how to send a Javascript variable to PHP and have PHP return an array. In my javascript file, I have this:

    Code:
    //Let's check to see if UPC is already in the array
         if ($.inArray(barcode, contents) == -1){
              contents.push(barcode);
    	  $.ajax({
                   type: 'POST',
    		url: 'getBarcodeDetails.php',
    		data: 'value=barcode',
    		dataType: 'json',
    		cache: false,
    		success: function(result) {
        		     details = result;
    		     alert(details);
    		},
    	   });
    };
    getBarcodeDetails.php looks like this:

    Code:
    <?php
    	set_time_limit(0);
    	$upc = $_POST['value'];
    
    	//Database connection code
    
    	//Run a query against the database and return some results that are stored in an array called $details.
    	
    	echo json_encode($details));
    	
    	
    ?>
    The main issue, is that I can't seem to find anything that calls the getBarcodeDetails.php file. I tried placing some javascript in getBarcodeDetails.php to verify if it was being called, but it doesn't seem to be.

    I tried using this syntax in my javascript

    Code:
    $.post("getUPCDetails.php", { value: barcode });
    Nothing seems to trigger the PHP file.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    15
    Rep Power
    0
    Of all things. it was the extra ) at the end of echo json_encode($details));

    Works now. If I may hijack my own thread, once I get the 1 dimensional array back from php, how do I parse it out into something I can insert into an array? Or could I insert the whole thing into the array?
  4. #3
  5. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,143
    Rep Power
    9398
    It's probably an object and not an array, but regardless jQuery will "parse" the string for you. result/details in your callback should be an array or object already.

    So what's this about inserting stuff?
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    15
    Rep Power
    0
    Originally Posted by requinix
    It's probably an object and not an array, but regardless jQuery will "parse" the string for you. result/details in your callback should be an array or object already.

    So what's this about inserting stuff?
    The object I get back contains three pieces of data, for instance:
    "1","2","3"

    I want to take those values that are stored in the object and insert them as a new row in an existing array. I am wondering if I need to parse the values out first into separate variables which I can then insert in to the array.
  8. #5
  9. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,143
    Rep Power
    9398
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    15
    Rep Power
    0
    Originally Posted by requinix
    So merging? Or do you have an example?
    I have an existing array called details. It looks like this:

    1234 Car Pontiac Sunfire 2
    1235 VAN Plymouth Voyage 4
    1235 SUV Kia Sorento 1

    What I am getting back from PHP is:
    1237 Truck Dodge Ram 1

    I wasn't sure if PHP was returning an array or an object. You mention merge which seems to indicate that PHP is return an array.

    So basically I would just want to add the two arrays together to get one array that looks like this:
    1234 Car Pontiac Sunfire 2
    1235 VAN Plymouth Voyage 4
    1235 SUV Kia Sorento 1
    1237 Truck Dodge Ram 1

    I will take this array and loop through it, displaying each line on the screen.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    15
    Rep Power
    0
    Merge works. Thanks so much for the help.

    Another question. When I do merge it seems to be treating the array as a 1 dimensional array. So each record is all on the same line.
  14. #8
  15. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,143
    Rep Power
    9398
    Could really use some specifics now: what do you see in the browser's error console thing when you
    Code:
    console.log(result)
    (inside that callback) and
    Code:
    console.log(the variable you're merging into)

IMN logo majestic logo threadwatch logo seochat tools logo