简体   繁体   中英

Converting Javascript variable to a PHP variable

我想使用videoel.getCurrentTime函数返回给我的javascript变量并将其转换为php变量,以便能够将其添加到我的SQL插入查询中,例如INSERT INTO tblData VALUES('$ phpVarFromJavascript') ;

You don't "convert" it. Send it using a get request or something similar to the php page.

Javascript runs on the client side while php runs on the server side.

There are many different ways you can do this and which one you choose depends on your particular situation. However, I think it's important to check how this all works and for you to make sure you understand the difference between client side and server side.

Check w3c's example . The Javascript part makes a request to the php file when the user chooses a person. The php file then queries the MySQL database and responds to the Javascript file which then in turns presents the result to the user.

The particular code used in this example I would not recommend using in production. The MySQL database information is stored in plain text which anyone could read. I'm using this link purely as a working example of how everything works together.

JavaScript cannot interact with PHP unless it is via an AJAX request . What you would need to do is send it via AJAX to the page, and then use PHP to access the variable .

This will convert js variable to php variable and php variable to js variable

<script>
function jstophp(){


var javavar=document.getElementById("text").value;  

document.getElementById("rslt").innerHTML="<?php 
$phpvar='"+javavar+"'; 
echo $phpvar;?>";
}

function phptojs(){

var javavar2 = "<?php 

$phpvar2="I am php variable value";
echo $phpvar2;

?>";
alert(javavar2);
}

</script> 
<body>
<div id="rslt">
</div>


<input type="text" id="text" />
<button onClick="jstophp()" >Convert js to php</button>
<button onClick="phptojs()">Convert php to js</button>

PHP variable will appear here:
<div id="rslt2">
</div>

</body>

Demo: http://ibence.com/new.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM