Home  >  Article  >  Backend Development  >  How to assign value from js to php

How to assign value from js to php

尚
Original
2019-10-31 11:07:385748browse

How to assign value from js to php

js variables are assigned to php:

js variables are assigned to php. Since PHP is a server language and needs to be run in the background, JS is a client language and can be run directly in the browser. Running on php, so js variables cannot be directly assigned to php, so how do you know what is the server language and what is the client language? In fact, when we look at the source code of the web page, we will find that we can directly view the source code of JS, while PHP cannot view the source code and can only output the results to the page. In order to achieve this effect, ajax is used here.

Process:

1, define a JS variable.

2, assign the variable to data

3, and assign the parameters of data to the php variable

. This will achieve the effect of assigning the js variable to php.

Example:

JS part:

$('button').click(function(){
 var js2_101=30;
 $.ajax({
 type:'GET',
 url:"3.php",
 data:{text:js2_101},
 success: function(data){
 alert(data)
 }
 }); 
 return false;
 });

PHP part:

$m2_10=$_GET['text'];
$php2=10;
echo $m2_10+$php2."<-----JS赋值给php,可以在PHP中进行编程";

Recommendation:php server

The above is the detailed content of How to assign value from js to php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn