Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

First to make it clear. I want to do this without using jQuery, in other words, yes I want to re-invent the wheel to understand how the wheel works. To my understanding this is possible via AJAX, but the net seems to only provide jQuery solutions to the problem. My goal is to generate an html document using javascript, but in turn after the page has loaded output only the non-script stub of the documents outerHTML via javascript to a new .php file which contains only the specified text.

index.php

    <!DOCTYPE html>

<?php
    include 'devScript.php';

    $div = new Div();
    $div->setBounds(0, 0, 100, 100);
?>

devScript.php

    <script type="text/javascript">

    var srcScript;
    var devScript;
    var stubScript;

    (function() {

        document.documentElement.appendChild(document.createElement('body'));

        srcScript = document.head.innerHTML;



    }());


    window.onload = function() {



        devScript = document.head.innerHTML.toString().replace(srcScript, '');
        document.documentElement.removeChild(document.documentElement.lastChild);

        stubScript = document.documentElement.outerHTML.toString().replace(srcScript, '').replace(devScript, '');

        alert("Full Script!");
        alert(document.documentElement.outerHTML);
        alert('Stub Script');
        alert(stubScript);

<?php
/*
  $file = fopen("iHateWritingHtmlTags.php", 'w');
  fwrite($file, stubScript);

  This DOES NOT WORK!!!!
 */
?>


    }

    function Div() {

        Div.STATIC = 'static';
        Div.ABSOLUTE = 'absolute';
        Div.RELATIVE = 'relative';
        Div.FIXED = 'fixed';
        Div.SOLID = 'solid';
        Div.DOTTED = 'dotted';
        Div.LEFT = 0;
        Div.CENTER = 1;
        Div.RIGHT = 2;
        Div.TOP = 0;
        Div.MIDDLE = 1;
        Div.BOTTOM = 2;

        var ELEMENT;
        var CSS;

        var horizontalAlign;
        var verticalAlign;

        var colorQueue;



        (function() {

            this.div = document.createElement('div');

            ELEMENT = this.div;
            CSS = this.div.style;

            CSS.border = '1px solid black';



            document.body.appendChild(this.div);



        }());

        this.setPosition = function(postype) {

            if (!horizontalAlign && !verticalAlign) {

                CSS.position = postype;

            }


        }

        this.setBounds = function(x, y, width, height) {

            CSS.left = x + 'px';
            CSS.top = y + 'px';
            CSS.width = width + 'px';
            CSS.height = height + 'px';

        }

        this.setColorQueue = function(r, g, b) {

            colorQueue = 'rgb(' + new Array(r, g, b) + ')';
            alert(colorQueue);

        }

        this.horizontalAlign = function(horiz) {

            var freeSpaceX = ((window.innerWidth - ELEMENT.offsetWidth) / 2);
            var defPadding = '8px';
            var defPaddingCenter;
            var defPaddingRight;
            var defPaddingLeft;

            horizontalAlign = true;

            this.setBounds(0, 0, 100, 100);

            if (CSS.position == 'relative' || CSS.position == 'absolute') {

                CSS.position = 'absolute';
                defPaddingCenter = 12;
                defPaddingRight = 4;
                defPaddingLeft = 8;



            } else if (CSS.position == 'fixed') {

                defPaddingCenter = 4;
                defPaddingRight = 4;
                defPaddingLeft = 8;

            }

            if (horiz == 0) {

                if (!verticalAlign) {
                    CSS.marginTop = defPadding;
                }
                CSS.marginLeft = defPaddingLeft + 'px';

            } else if (horiz == 1) {

                if (!verticalAlign) {
                    CSS.marginTop = defPadding;
                }
                CSS.marginLeft = freeSpaceX - defPaddingCenter + 'px';

            } else if (horiz == 2) {

                if (!verticalAlign) {
                    CSS.marginTop = defPadding;
                }
                CSS.marginLeft = (freeSpaceX - defPaddingRight) * 2 + 'px';

            }

        }

    }


</script>
<?php

class Div {

    public $obj_id;

    function __construct() {

        $this->obj_id = "_" . uniqid(rand());

        echo '<script>',
        'var ' . $this->obj_id . ' = new Div();',
        '</script>';
    }

    function setPosition() {

        echo '<script>',
        $this->obj_id . '.setPosition();',
        '</script>';
    }

    function setBounds($x, $y, $width, $height) {


        $parse_string = $x . ',' . $y . ',' . $width . ',' . $height;


        echo '<script>',
        $this->obj_id . '.setBounds(' . $parse_string . ');',
        '</script>';
    }

    function setColorQueue() {

        echo '<script>',
        $this->obj_id . '.setColorQueue();',
        '</script>';
    }

    function horizontalAlign() {

        echo '<script>',
        $this->obj_id . '.horizontalAlign();',
        '</script>';
    }

}
?>

NOTE: The Root of the problem is in index.php. Feel free to test these two out on firefox because the alert box has a scroll bar. :)

OUTPUT: iHateWritingHtmlTags.php only contains "stubScript" as its text not the actual javascript variable stubScript?!?!

share|improve this question
2  
    
@SLaks Do you happen to have cross-browser a code implementation of this? I want to skip the trial by error steps. :) –  StoneAgeCoder Jul 16 '14 at 2:32
1  
The root of your problem appears to be you're loading in JS and trying to execute it on the server side using PHP. JS is client side and PHP is server side, so the JS isn't going to have rendered until after PHP is done, you also just can't call straight up JS (your call to new Div(), which is a JS function) through PHP. –  Busches Jul 16 '14 at 2:45
1  
@StoneAgeCoder, you are confused about client- and server-side operations. –  Dwayne Towell Jul 16 '14 at 2:54
1  
@StoneAgeCoder you can have PHP spit out JS, because JS is only parsed after it's sent to the browser and PHP is done. If you want to sent something from JS to PHP, then you need to use AJAX. –  Busches Jul 16 '14 at 3:02

1 Answer 1

Let "variable" be the name of the Javascript variable

<form action="." method="get">

<input type='hidden' name='Javascript_Variable' value='<script>document.write(variable);</script>'>

</form>

Now, in PHP:

<?php

 $phpVariable = $_GET['Javascript_Variable'];

?>
share|improve this answer
    
This is good for storing a php variable into javascript, but i need vice-versa. –  StoneAgeCoder Jul 16 '14 at 2:45
    
Then you can reserve the process by creating a hidden input type with a form around it whose value is that of the javascript variable and then taking the PHP variable by taking the value of $_GET['name of hidden input'] –  user2938543 Jul 16 '14 at 2:50
    
Do you mind editing your answer with what you have suggested please. –  StoneAgeCoder Jul 16 '14 at 2:57
1  
Yes, I made the changes to the above example. –  user2938543 Jul 16 '14 at 3:02
    
I am having a hard time create the input via javascript. –  StoneAgeCoder Jul 16 '14 at 3:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.