Smudger
Smudger

Reputation: 10771

use popupWindow = window.open with javascript variables

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.

Upvotes: 0

Views: 8027

Answers (2)

James Brown
James Brown

Reputation: 1

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');

Upvotes: 0

Joel Etherton
Joel Etherton

Reputation: 37523

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>

Upvotes: 6

Related Questions