0

I have the below script:

<script type="text/javascript">
        function train(roww){
        $.post ('getpeopleinjobs.php',{ 
        postvarposition: form["position"+roww].value,
        postvarjob: form["job"+roww].value,
        postvarperson: form["person"+roww].value, 
        postrow: roww},
            function(output){ 
                popupWindow = window.open('trainingneeded.php?position=postvarposition&amp;job=postvarjob&amp;person=postvarperson','popUpWindow','height=400,width=1000,left=10,top=10,resizable=yes,scrollbars=yes,toolbar=no,menubar=no,location=no,directories=no,status=yes')
                    });
                }
</script>

I have tested the script and it works 100%. my problem is that my variables, postvarposition,postvarjob and postvarperson are being passed as text in the URL string rather than the actual variables.

How do I format the line

popupWindow = window.open('trainingneeded.php?position=postvarposition&amp;job=postvarjob&amp;person=postvarperson','popUpWindow','height=400,width=1000,left=10,top=10,resizable=yes,scrollbars=yes,toolbar=no,menubar=no,location=no,directories=no,status=yes')

so that the variables are passed as variables and not as text.

Thanks in advance.

Smudger
  • 10,451
  • 29
  • 104
  • 179

2 Answers2

6

You need to concatenate the variables into the string:

'trainingneeded.php?position=' + postvarposition + '&amp;job=' + postvarjob + '&amp;person=' + postvarperson

Edit:

Using your code, you can see that the .post call is made up of 3 parts: a url (getpeopleinjobs.php), parameters to pass (enclosed in {}), and your success handler (the function). Each of these is separate in scope so none of them has access to any of the others. Basically, your success handler has no idea what a "postvarposition" is. In order for it to be visible, you would have to bring the variable outside of its container scope. In this case, you have to remove the assignment of these items from the {} and place them outside the method .post call.

<script type="text/javascript">
    function train(roww){
    $.post ('getpeopleinjobs.php',{ 
    // Because this is inside a {} these variables
    // can be considered "trapped" inside this scope
    postvarposition: form["position"+roww].value,
    postvarjob: form["job"+roww].value,
    postvarperson: form["person"+roww].value, 
    postrow: roww},
        function(output){ 
            popupWindow = window.open('trainingneeded.php?position=postvarposition&amp;job=postvarjob&amp;person=postvarperson','popUpWindow','height=400,width=1000,left=10,top=10,resizable=yes,scrollbars=yes,toolbar=no,menubar=no,location=no,directories=no,status=yes')
                });
            }
</script>

A possible solution to this would be:

<script type="text/javascript">
    function train(roww){

        // declare them and assign them out here
        var position = form["position"+roww].value;
        var job = form["job"+roww].value;
        var person = form["person"+roww].value;

        $.post ('getpeopleinjobs.php',{ 
            postvarposition: position,
            postvarjob: job,
            postvarperson: person, 
            postrow: roww },
        function(output){ 
            popupWindow = window.open('trainingneeded.php?position=' + position + '&amp;job=' + job + '&amp;person=' + person,'popUpWindow','height=400,width=1000,left=10,top=10,resizable=yes,scrollbars=yes,toolbar=no,menubar=no,location=no,directories=no,status=yes');
                });
            }
</script>
Joel Etherton
  • 37,325
  • 10
  • 89
  • 104
  • 1
    Don't forget to encode the parameters, see http://stackoverflow.com/questions/332872/how-to-encode-a-url-in-javascript – Barmar Jul 12 '12 at 17:15
  • Thanks Joel, I did try that but stillnot working. Chrome outputting error : 'Uncaught ReferenceError: postvarposition is not defined' any ideas – Smudger Jul 12 '12 at 17:29
  • 1
    @Smudger: It looks like it might be a scoping problem. Looking at it a second time, I don't know that `postvarposition` is available to your function because it's encapsulated in a private scope belonging to the post parameters. Perhaps if you move its assignment outside the post parameters it will be available. – Joel Etherton Jul 12 '12 at 17:32
  • Sorry Joel, I am very new to javascript, can you elaborate more? Thanks. – Smudger Jul 12 '12 at 17:34
  • Thanks, appreciate your time. – Smudger Jul 12 '12 at 17:36
  • Thanks Joel, work perfectly. I coped this script and modified it slightly, I do not need to post to getpeopleinjobs. do I still need the $.post statement? if not how do I declare the variables? Thanks again. – Smudger Jul 12 '12 at 17:57
  • @Smudger: I didn't understand your last question. If you don't need to post, then I'd think all of this would be fairly useless code. – Joel Etherton Jul 12 '12 at 17:59
  • Joel, one other question. your script works perfectly in IE and I can get the variables with $_GET[''];. However in chrome, I can see the URL data is present but only the first value is returned by $_GET[''];. as mentioned all three variables are returned in IE? – Smudger Jul 12 '12 at 18:01
  • Apologies, here is my explanation: I started experimenting with JQuery today and used the script to show the output from getpeopleinjobs without refresh. This worked perfectly. I then wanted to open the same variables in a popup page. so my function train still posts to getpeopleinjobs.php however I am only interested in passing the variables to trainingneeded.php? – Smudger Jul 12 '12 at 18:07
0

I was able to get this working, it was indeed a string concatenation that was required.

//some data taken from the google maps api.
resstring=results[0].geometry.location;

var new_window = 'http://www.trystingtrees.com/coordsc.php?coords='+resstring+'&listing_id=".$listing_id."';

popupWindow=window.open(new_window,'_SELF');
Oliver Spryn
  • 16,871
  • 33
  • 101
  • 195