简体   繁体   English

如何将 Javascript 变量传递给 PHP 以存储在 MYSQL 数据库中

[英]How to pass a Javascript variable to PHP to store in MYSQL database

I'm trying to pass a javascript variable to PHP so when I submit my form, it will save the variable in my MYSQL database.我正在尝试将 javascript 变量传递给 PHP 所以当我提交表单时,它会将变量保存在我的 MYSQL 数据库中。

Heres the HTML:这是 HTML:

 function replyLink(element) { document.getElementById('displayForm').style.display = "block"; var replyId = element.getAttribute("data-replyid"); console.log(replyId) } function closeLink() { document.getElementById('displayForm').style.display = "none"; }
 <a href='javascript:void(0);' data-replyid='1' class='replyLink' onclick='replyLink(this)' />[Reply]</a> <div id='displayForm' style='display:none;'> <div id='replyTitle'> <label>Write a reply</label> <a href='javascript:void(0);' onclick='closeLink()' />[Close]</a> </div> <form action='' method='POST' accept-charset='utf-8' enctype='multipart/form-data'> <table id='postForm'> <tr> <td class='replyForm_title' sty>Name</td> <td><input type='text' name='commentName'></td> </tr> <tr> <td class='replyForm_title'>Comment</td> <td><textarea cols='48' rows='4' wrap='soft' name='commentText'></textarea></td> </tr> <tr> <td></td> <td><input type='submit' name='commentBtn' value='Reply' onclick='submitForm()'></td> </tr> </table> </form> </div>

If you run the code, you can see that the form pops up, and returns the data-attribute value of '1'.如果你运行代码,你可以看到表单弹出,并返回数据属性值'1'。 I'd like to insert that variable 1 into a MYSQL database.我想将该变量 1 插入到 MYSQL 数据库中。 Would anyone guide me?有人会指导我吗? Thanks.谢谢。 (ALSO, code snippet runs. So you have an understanding of how it works.) (另外,代码片段运行。因此您了解它是如何工作的。)

Add a hidden input to the form, and put replyId into its value.向表单添加隐藏输入,并将replyId放入其值中。

Then in PHP use $_POST['replyId'] to get the value.然后在 PHP 中使用$_POST['replyId']获取值。

 function replyLink(element) { document.getElementById('displayForm').style.display = "block"; var replyId = element.getAttribute("data-replyid"); document.getElementById('replyId').value = replyId; console.log(replyId) } function closeLink() { document.getElementById('displayForm').style.display = "none"; }
 <a href='javascript:void(0);' data-replyid='1' class='replyLink' onclick='replyLink(this)' />[Reply]</a> <div id='displayForm' style='display:none;'> <div id='replyTitle'> <label>Write a reply</label> <a href='javascript:void(0);' onclick='closeLink()' />[Close]</a> </div> <form action='' method='POST' accept-charset='utf-8' enctype='multipart/form-data'> <table id='postForm'> <tr> <td class='replyForm_title' sty>Name</td> <td><input type='text' name='commentName'></td> </tr> <tr> <td class='replyForm_title'>Comment</td> <td><textarea cols='48' rows='4' wrap='soft' name='commentText'></textarea></td> </tr> <tr> <td><input type="hidden" name="replyId" id="replyId"></td> <td><input type='submit' name='commentBtn' value='Reply' onclick='submitForm()'></td> </tr> </table> </form> </div>

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

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