javascript - 如何使用链接触发 php 查询

标签 javascript php html

我有一个页面,我可以在其中收集和存储页面浏览量,我有一个 php 查询,可以在单击按钮时存储我的页面浏览量,

if(isset($_POST['submit'])){
    mysql_select_db($database_epl, $epl);
    $query_lin = sprintf("SELECT * FROM topic WHERE id = %s ORDER BY `Date` DESC", GetSQLValueString($colname_lin, "int"));
    $topicId = $_GET['id']; // you need to change 'id' to the name of your ID-parameter in the URL
    $viewsIncrementQuery = "UPDATE `topic` SET `Views` = `Views` + 1 WHERE `id` = " . $topicId;
    $incremented = mysql_query($viewsIncrementQuery, $epl) or die(mysql_error());
    // run/execute mysql query

但现在我希望能够使用链接调用该查询,(单击链接时)

我该如何解决这个问题

最佳答案

您可以向链接添加 onclick 事件并让它设置表单的操作属性,然后触发提交

html

<form method="post" id="myForm">
   <input type="text" name="id" />
   <input type="button" name="submit" value="submit">
</form>

<a href="/somePhpScript.php" onclick="submitForm(this)">Click to submit</a>

Javascript

function submitForm(element){
    var action = element.href;
    var form = document.getElementById("myForm");
    form.action = action;
    form.submit();
}

您的数据将位于 $_POST 全局数组中,如果您希望其位于 $_GET 全局数组中,则只需更改 方法 get

的属性

关于javascript - 如何使用链接触发 php 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25285000/

相关文章:

jquery - 我如何让我的 jquery 元素在几秒钟后停止消失?

javascript - 如何从 Controller 访问值到angularJs中的指令

javascript - Dart-JS 互操作 : JS object not loading when running in Dartium

javascript - codeigniter ajax 表单验证与提交

php - 小数的数据字段类型不能按要求工作

html - 如何在包含图像的两个 div 元素之间显示文本段落?

javascript - 在 (js) 中加载新元素时如何重新定位 HTML 元素?

javascript - 导航栏折叠不显示在 768px

javascript - 将鼠标悬停在元素上时如何在浏览器底部预览 URL

javascript - 在 Ajax 中,如何通过脚本代码获取 2 个输入值?