gpt4 book ai didi

javascript - 在 PHP 页面链接中附加 HTML 元素的值

转载 作者:行者123 更新时间:2023-11-28 17:54:32 25 4
gpt4 key购买 nike

我有一个 HTML 文件,如下所示:

<html>

<body>
<div id="container">
<section>
<article id="news-card">
<div id='identity'>011</div>
<h1>HEADLINE</h1>
<p>AUTHOR</p>
<a href='#' class='readmore'>Read more</a>
<form action='postnews.php' method='post'>
<input type='checkbox' name='bookmark'>Bookmark?</input>
</form>
</article>
<section>
</div>
</body>

</html>

这是 PHP 文件:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "News-Articles";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

$_bookmark = isset($_POST["bookmark"]) ? 'Y' : 'N';
$sql = "INSERT INTO stats (iid, bookmark, readmore, counter)
VALUES ('$_iid', '$_bookmark', NULL, NULL)
ON DUPLICATE KEY UPDATE
bookmark = '$_bookmark';"
mysqli_query($conn, $update);
mysqli_close($conn);
?>

我想将 id = 'identity' 的元素的值包含到表单的操作属性中。像这样的事情:

<form action = "postnews.php?iid=getElementById(identity).innerHTML">

因此,在 PHP 文件中,我可以 $_GET["iid"] 插入该特定 iid 的书签值。我怎样才能找到解决方案?

最佳答案

您只需将 iid=value 添加到表单的提交操作中即可。我存了一张支票?因此,如果操作已经有一个查询字符串,它只会将 iid 附加到它上面。

var theForm = document.getElementById('bookmark-form');
theForm.addEventListener('submit', changeAction, false);

function changeAction(event)
{
event = event || window.event;

var form = event.target;
var idDiv = document.getElementById('identity');
var idToAdd = idDiv.innerHTML;
var action = form.action;

if (action.indexOf('?') === -1) {
form.action = action + '?iid=' + idToAdd;
} else {
form.action = action + '&iid=' + idToAdd;
}

event.preventDefault();

console.log(form.action);
}
<html>

<body>
<div id="container">
<section>
<article id="news-card">
<div id='identity'>011</div>
<h1>HEADLINE</h1>
<p>AUTHOR</p>
<a href='#' class='readmore'>Read more</a>
<form action='postnews.php' method='post' id="bookmark-form">
<input type='checkbox' name='bookmark'>Bookmark?</input>
<input type='submit' value='Submit'>
</form>
</article>
<section>
</div>
</body>

</html>

关于javascript - 在 PHP 页面链接中附加 HTML 元素的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44770342/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com