0

I want to pass array myWorkout to 'play_workout.php'. I want 'play_workout.php' to open and display the contents of myWorkout (for this example). (Once I see that this is working I will parse the data from myWorkout and write it back to a database). I'm not getting any errors in firebug, but play_workout is not being opened nor is is capturing the array object myWorkout.

I would appreciate a second glance at this. Thanks as always!

page workout_now.php

<div id="playworkout"><button onClick="playWorkout()">Play Workout</button></div>

JAVASCRIPT

function playWorkout(){

  var arr = $("#dropTargetframe > li").map(function(){
  return $(this).attr('data-id');}).get();

     var myRoutine = arr;
     var myWorkout = new Array();
        for (var i in myRoutine){
           if (myRoutine[i])
             myWorkout.push(myRoutine[i]);
            }
          //array appears like ["4", "5", "1", "4"]
          JSON.stringify(myWorkout);
          encodeURIComponent(myWorkout);

         var url = "http://localhost/RealCardio/play_workout.php";

         $.get(url, myWorkout); 

page play_workout.php

<?php
 ...
 $arrayWorkout = json_decode($_REQUEST['myWorkout']); 
 print_r($arrayWorkout);     
...
?>
1

4 Answers 4

1

Assuming, that as in your comment the array elements doesn't contain any special chars, just numbers, simply do

var myWorkoutPost=myWorkout.join('x');

Transport this to the server the way you want (form hidden field, AJAX, ..) and in PHP do

$myWorkout=explode('x',$_REQUEST['myWorkoutPost']);
Sign up to request clarification or add additional context in comments.

2 Comments

What would I populate in .join(x)? What value goes in 'x'?
Sorry, was a typo, should have been 'x' , not x , I have edited my answer.
1

Both PHP and Javascript use JSON encoding so I would say the best way would be to JSON encode the array and then POST it as a hidden field to the PHP page and use JSON decode.

http://www.php.net/manual/en/function.json-decode.php

Comments

0

Encode this array to JSON, send it to php script and recieve it with $_POST. Finally, decode JSON in php script.

Comments

0

A work flow for almost every structured values 99% of the time it will do.

  • Use JSON.stringify([1,2,3]) it will give you a string "[1,2,3]".
  • Encode it with encodeURIComponent("[1,2,3]") you'll have something like this "%5B1%2C2%2C3%5D"
  • Now it's save to be sent to PHP or any other server side language try to use XMLHttpRequest or better jQuery.get() to send the values to PHP.

In php you'd do this: - $arrayOfValues = json_decode($_REQUEST['name_of_parameter']); - print_r($arrayOfValues);

Normally you'd need to include JSON library to support old browsers. More info on JSON.

2 Comments

Is this how I would pass it in javascript - $.get('play_workout.php',myWorkout,'','json');
I revised the question a little. Can you take another look?

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.