-2

I have this code:

<?php       $html=file_get_contents('testmaker_html.html');
        echo $html;
?>



<script type="text/javascript">


    document.getElementById('save_finaly_TEST').addEventListener("click", function(){
        cover = document.getElementById('cover').value;
        keywords = document.getElementById('keywords').value.split(",");

        notificationAboutElement = "Ok!";
        notifyMe(notificationAboutElement);
      html_saver();

    <?php $test_html = "<script>document.write(html)</script>"?>   


    }); 

</script>
<?php 
        $new_test = rand().".html";
        $myfile = fopen($new_test, "w") or die("Unable to create file!");
        write($myfile, $test_html)  or die("Can't write to file");
        fclose($myfile)  or die("Can't close the file!");
        echo $new_test;
?>


<script type="text/javascript">
    console.log("$test_html: " + <?php echo $test_html; ?>);
    console.log("$new_test: " + <?php echo $new_test; ?>);
</script> 

Why is $test_html empty? I know the php and javascript is not on same server but this methodd to get values many times worked for many people. Than what can be wrong with this?

  • PHP and JS run at different times. You need to rethink this. – evolutionxbox Jul 21 '17 at 11:21
  • 2
    I can't see this having worked for anyone. The JS isn't run until it's sent to the client. It's just meaningless text to PHP. – Carcigenicate Jul 21 '17 at 11:22
  • 1
    You had the factory (server/PHP) build you a car, and ship it to your door. Now in your driveway (client) you put a red sticker on the wind shield ... and expect the factory to automatically know about this? No, of course that does not work. You would need to _send_ this information to the factory - and that means, you need to make a new HTTP request, either directly (submitting a form, loading a new page with URL parameters), or in the background via AJAX. – CBroe Jul 21 '17 at 11:25
  • Than this is working because the php runs first? : `` – Kronologia it Vme Jul 21 '17 at 11:33
  • @KronologiaitVme The PHP part of that will run, and it will echo the Javascript code, but the Javascript won't actually run until it's already been sent to the client. – Carcigenicate Jul 21 '17 at 11:48

2 Answers2

0

PHP runs on the server, and Javascript runs on the client. So when the Javascript is running, PHP has already ran.

<?php 
    $new_test = rand().".html";
    $myfile = fopen($new_test, "w") or die("Unable to create file!");
    fwrite($myfile, $test_html)  or die("Can't write to file");
    fclose($myfile)  or die("Can't close the file!");
    echo $new_test;
?>
<script>
    var new_test = "<?php echo $new_test; ?>";

    console.log("$new_test: " + new_test);
</script>

Here you're taking the file's contents, and JSON encoding it so that it is sent to the client as a valid Javascript object. Look a view source to see what I mean.

For other situations:

  • Create a POST API if you want the client to submit things to the server
  • Use Javascript if you just want to display things from Javascript on the page
rubenwardy
  • 679
  • 5
  • 21
0

try this

you have to use fwrite not write only for file write

<?php       
        $html=file_get_contents('testmaker_html.html');
        echo $html;
?>
<script type="text/javascript">
    document.getElementById('save_finaly_TEST').addEventListener("click", function(){
        cover = document.getElementById('cover').value;
        keywords = document.getElementById('keywords').value.split(",");

        notificationAboutElement = "Ok!";
        notifyMe(notificationAboutElement);
      html_saver();

    <?php $test_html = "<script>document.write(html)</script>"?>   
    }); 
</script>
<?php 
        $new_test = rand().".html";
        $myfile = fopen($new_test, "w") or die("Unable to create file!");
        fwrite($myfile, $test_html)  or die("Can't write to file");
        fclose($myfile)  or die("Can't close the file!");
        echo $new_test;
?>
<script type="text/javascript">
    console.log("$test_html: " + <?php echo $test_html; ?>);
    console.log("$new_test: " + <?php echo $new_test; ?>);
</script> 
Bhargav Chudasama
  • 6,928
  • 5
  • 21
  • 39