document.getElementById('save_f" /> document.getElementById('save_f" /> document.getElementById('save_f"/>

Why I can't get javascript values into my php5 code?

105 views Asked by At

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?

2

There are 2 answers

0
rubenwardy On

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
4
Bhargav Chudasama On

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>