-1

I am very new to php.

After all comments below i have changed the question and i have tried with ajax as per the suggestion

<script type="text/javascript">
    function getval(sel) {
        var get= sel.value;
        alert(get);
    }
</script>

I have tried :

<script type="text/javascript">
var a=get;  
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
 alert( xhttp.responseText)
 }
};

xhttp.open("GET", "?a="+a, false);
xhttp.send();

</script>

But i did not get output.

Facing issue in xhttp.open all the code are in single page so i have not given any path.

R9102
  • 687
  • 1
  • 9
  • 32
  • you need submit something to sever first – Ma Yubo May 04 '16 at 08:52
  • You can't do that without AJAX. The reason is pretty simple, when this page is requested, the PHP is rendered first which will also render the Javascript scripts. You can pass variables from PHP to JS but not the other way around. It's a 1-directional flow. Alternatively , you can use AJAX to send data from browser to PHP without page reload. http://www.w3schools.com/ajax/ – dlock May 04 '16 at 08:54
  • @Ma yubo don't we have any option to get without submit – R9102 May 04 '16 at 08:54

2 Answers2

1

You can't do that without reloading the page or using AJAX. The reason is pretty simple, when this page is requested, the PHP is rendered first which will then render the Javascript scripts along with the HTML DOM. You can pass variables from PHP to JS but not the other way around. It's a 1-directional flow. Alternatively , you can use AJAX to send data from browser to PHP without page reload. http://www.w3schools.com/ajax/

dlock
  • 9,447
  • 9
  • 47
  • 67
0

for javascript

var a=1  //variable you want pass

var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
     alert( xhttp.responseText)
     }
 };
xhttp.open("GET", "php.php?a="+a, true);
xhttp.send();

on php

echo $_GET['a']
Milap
  • 6,915
  • 8
  • 26
  • 46
Ma Yubo
  • 215
  • 1
  • 10
  • – R9102 May 04 '16 at 10:14
  • This is bad idea to write such big code in comment. You can write new answer with some explanation. – Milap May 04 '16 at 10:15
  • issue facing in xhttp.open("GET", "a="+a, false); all the code or same page iam sorry if i have done mistake here – R9102 May 04 '16 at 10:15
  • you will need a php file, which is called php,php contain above php code – Ma Yubo May 04 '16 at 10:23