【问题标题】:Is there a way to use javascript variable as part of PHP SQL page?有没有办法使用 javascript 变量作为 PHP SQL 页面的一部分?
【发布时间】:2019-05-26 13:32:10
【问题描述】:

我在 laravel PHP 页面中有一个变量,保存在 Javascript 中。我目前将其打印/输出为:

${data.id}

我现在想连接到 PHPMyAdmin 以检查 ID 是否在表中。但是,我意识到我不能在 PHP 中使用 ID 号 - 因为这是在服务器端完成的,而 JS 在客户端完成。有点卡住了。

不幸的是,我使用的是共享主机包,所以 node.js 不是我可以使用的解决方案。

任何帮助都会很棒!我想我可以使用 AJAX,但不确定它的外观。

【问题讨论】:

  • 在 jQuery 中使用 Ajax 向 PHP 发送异步调用
  • 谢谢。那会是什么样子?

标签: javascript php sql


【解决方案1】:

这里是 AJAX 示例。

$.ajax({
  method: "POST",
  url: "some.php",
  data: { id: data.id}
})
  .done(function( response) {
    console.log(response);
    //TODO:process your response here if receive response from php file.

  });

【讨论】:

  • 谢谢。好的。在“some.php”中,变量是如何传递的? $select = '选择 *'; $from = '来自表'; $where = ' WHERE id = $id ORDER BY $id DESC LIMIT 5';
  • 在 PHP 中,您可以从 $_POST 获取此值,然后随意使用它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-09-06
  • 2011-12-15
  • 1970-01-01
  • 1970-01-01
  • 2022-12-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多