-1
 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

Dinoo
  • 27
  • 10
  • function myfun(){ var texteld= document.getElementById('texteld').value; texteld"; ?> } i tried this way but i cant pass js value to php varible – Dinoo Apr 03 '16 at 06:52

2 Answers2

5

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;
}
?>
Khaled Rahman
  • 141
  • 1
  • 6
0

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.

Greg Borbonus
  • 1,384
  • 8
  • 16