-2

I have to read the values of multiple elements, put them into variables and then write them with default text to a file. I'm now using javascript and php. I hope you can help me. I am possibly open to other solutions.

This is what I have right now:

    <script>

  function createfile(){
    var maximum = x;
    var element = 0;

    var Element_exists = document.getElementById(element);
    if(Element_exists){

      function toggle(Element_exists){
          if(Element_exists.tagName === 'Input'){
              var p_name = document.getElementById(element).value;
              var p_font = document.getElementById(element + "_font").value;
              var p_fontsize = document.getElementById(element + "_fontsize").value;
              var total_p = "</p>"+ p_name + " new text "+ p_font +" even more new text " + p_fontsize + " last bit of text</p>"
          }else{

          }
      }

      <?php
      $myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
      $txt = total_p;
      fwrite($myfile, $txt);
      fclose($myfile);
      ?>

    }
  }
  </script>
  • 1
    You seem to confuse client and server side code. You can't access client side variables directly from code executed on the server. They run on different machines. – mata Jan 26 '18 at 10:31
  • javascript is client-side code, you should open an external PHP file then convert arrays in json_encode and send data to javascript. And also, try learning jQuery its easier to manipulate DOM compared to raw javascript – Ali Çarıkçıoğlu Jan 26 '18 at 11:07

2 Answers2

1

So now what can I do if I need to call PHP? It depends how you need to do it: either by reloading the page or by using an AJAX call.

You can do by reloading page and send HTTP request you can make AJAX call with JavaScript and this does not require reloading page

Reference : What is the difference between client-side and server-side programming?

Farhan Qasim
  • 990
  • 5
  • 18
1

You have to find a way to send the javascript values to php.

PHP is executed on the server, Javascript is executed on the client. That means that you can't access a JS variable directly from PHP and viceversa.

Try sending your Javascript value via POST and getting it in PHP with $_POST

If you want to get deep in differences between PHP and Javascript, take a read at this

Brugui
  • 574
  • 6
  • 20