简体   繁体   English

在 PHP 中使用 HTML 变量

[英]Using HTML variable in PHP

I'm trying to use the value stored in token in my PHP script to update a database value with SQLite.我正在尝试使用存储在我的 PHP 脚本中的令牌中的值来使用 SQLite 更新数据库值。 It wont let me use document.getElementByID....any ideas?它不会让我使用 document.getElementByID .... 任何想法?

<div id="TokenCount">
<label for="token"><abbr title="Tokens">Tokens</abbr></label>
<input id="token" value="0" />
</div>

<div id="Buttons">
<button id="pay" onClick="pay(10); loadMessage()"><?php $db=sqlite_open("../../logins.db"); 
$token = document.getElementById('token').value;
sqlite_query($db,"UPDATE Users SET tokens='$token' WHERE user_id='{$_SESSION['user_id']}'"); 
sqlite_close($db); ?>Pay</button> 
</div>
</div>   

PHP code is executed server-side, so the website is sent to the client with the result of the PHP code without any PHP found in the source. PHP 代码在服务器端执行,因此网站将与 PHP 代码的结果一起发送到客户端,而在源代码中没有找到任何 PHP。 Because of this HTML tags cannot be used in PHP on a website.因为这个 HTML 标签不能在网站上的 PHP 中使用。

As i said in comments, you can't execute PHP-code(server side code) on client side (in browser).正如我在评论中所说,您不能在客户端(在浏览器中)执行 PHP 代码(服务器端代码)。

You should send AJAX query to server, and save this value to mysql on server side.您应该将AJAX 查询发送到服务器,并将此值保存到服务器端的 mysql。

Javascript(jquery) code: Javascript(jquery) 代码:

function loadMessage(){
    $.post("setValue.php", {val:document.getElementById('token').value} function( data ) {
       alert('success');
    });
}

Create new php script for example setValue.php (r use contoller action, or your own staff how you routing pages).创建新的 php 脚本,例如 setValue.php(使用控制器操作,或您自己的员工如何路由页面)。 Script code:脚本代码:

$token = $_POST['val'];
sqlite_query($db,"UPDATE Users SET tokens='$token' WHERE user_id='{$_SESSION['user_id']}'"); 
sqlite_close($db);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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