如何将javascript值传递给php变量[重复]

This question already has an answer here:

 function myfun(){

    var texteld= document.getElementById('texteld').value;

    <?php

     $id =  "<script>texteld</script>";

 }

This PHP and JavaScript code does not working. i want to pass java script texteld value to PHP $id

</div>

To Answer your question, you need to understand how a page loads:

  1. Request to server.
  2. Server Processes request, load pages, run php, whatever needs to be done.
  3. Server outputs html,css,javascript, whatever the process from 2 tells it to.
  4. Finally, your browser interprets all the data and gives you a layout.

So, as you can see, php is run before javascript is even sent to the browser to be run. What you want to do is go backwards in the process, this is not possible.

After the page loads and the browser has some data, you can send data back to php. This is not the same as your attempt though. You may need to rethink your strategy.

To pass a Javascript value to PHP you'd need to use AJAX. With jQuery, it would look something like this (most basic example possible)

var variableToSend = 'foo';
$.post('file.php', {variable: variableToSend});

On your server, you would need to receive the variable sent in the post:

$variable = $_POST['variable'];

Or use This

 function myJavascript() { 
      var texteld = document.getElementById('texteld').value;
      window.location.href = "test2.php?name=" + texteld; 
    }

In php file get variable like these code

<?php 
if(isset($_GET['name'])){
$name= $_GET['name']; 
echo $name;
}
?>