如何将在单击警报输入事件中输入的数据插入到数据库中

huangapple go评论127阅读模式
英文:

How can I insert a Data inputted in a textbox from an onclick Alert input event into DataBase

问题

UI

如何将在单击警报输入事件中输入的数据插入到数据库中

PHP提交脚本的代码

  1. if (isset($_POST['rejectreason'])){
  2. $Note = $_POST['rejection_reason'];
  3. $whyreject = 'Rejection_Reason-' . $Note;
  4. $sql = "UPDATE requestitem SET requestitem_UsefulNote='$whyreject'";
  5. $query = $dbh->prepare($sql);
  6. if ($query->execute()){
  7. $success="拒绝成功,并发送原因。";
  8. } else {
  9. $error="抱歉,拒绝失败,请稍后重试。";
  10. }
  11. }

取消按钮的代码

  1. <input type="hidden" id="rjctrsn-data" href="" name="rejection_reason">
  2. <a type="submit" name="rejectreason" onclick="document.getElementById('rjctrsn-data').value = prompt('您确定要拒绝此项吗!:', '请说明拒绝的原因');"
  3. value="" class="btn btn-xs delete-record" data-id="0">
  4. <span style="font-size:25px" class="glyphicon glyphicon-remove text-danger" style="color:red"></span>
  5. </a>
英文:

UI

如何将在单击警报输入事件中输入的数据插入到数据库中

code for the PHP Submit Script

  1. if (isset($_POST[&#39;rejectreason&#39;])){
  2. $Note = $_POST[&#39;rejection_reason&#39;];
  3. $whyreject = &#39;Rejection_Reason-&#39;.$Note;
  4. $sql = &quot;UPDATE requestitem SET requestitem_UsefulNote=&#39;$whyreject&#39; &quot;;
  5. $query = $dbh-&gt;prepare($sql);
  6. if ($query-&gt;execute()){
  7. $success=&quot;Rejection Confirmed Successfully and Reason Sent.&quot;;
  8. }else {
  9. $error=&quot;Sorry, Rejection Failed, Try again Later.&quot;;
  10. }
  11. }

code for the cancel button

  1. &lt;input type=&quot;hidden&quot; id=&quot;rjctrsn-data&quot; href=&quot;&quot; name=&quot;rejection_reason&quot;&gt;

<a type="submit" name="rejectreason" onclick="document.getElementById('rjctrsn-data').value = prompt('Are you sure you want to Reject this Item ! :', 'Please State Reason for Rection');"
value="" class="btn btn-xs delete-record" data-id="0">
<span style="font-size:25px" class="glyphicon glyphicon-remove text-danger" style="color:red"></span>
</a>

答案1

得分: 1

触发点击警告输入事件并获取用户输入

  1. <form action="submit_script.php" method="post">
  2. <button type="button" onclick="showRejectionReasonAlert()">拒绝</button>
  3. </form>
  4. <script>
  5. function showRejectionReasonAlert() {
  6. var rejectionReason = prompt("请输入拒绝原因:");
  7. if (rejectionReason != null) {
  8. document.getElementById("rjctrsn-data").value = rejectionReason;
  9. document.getElementById("yourFormId").submit();
  10. }
  11. }
  12. </script>

在执行 SQL 之前,请确保正确设置数据库连接。

  1. <?php
  2. if (isset($_POST['rejection_reason']) && !empty($_POST['rejection_reason'])) {
  3. $Note = $_POST['rejection_reason'];
  4. $whyreject = 'Rejection_Reason-' . $Note;
  5. $sql = "UPDATE requestitem SET requestitem_UsefulNote=:whyreject";
  6. $query = $dbh->prepare($sql);
  7. $query->bindParam(':whyreject', $whyreject);
  8. if ($query->execute()) {
  9. $success = "拒绝成功,原因已发送。";
  10. } else {
  11. $error = "抱歉,拒绝失败,请稍后重试。";
  12. }
  13. }
英文:

Trigger the onclick alert input event and get the user input

  1. &lt;form action=&quot;submit_script.php&quot; method=&quot;post&quot;&gt;
  2. &lt;button type=&quot;button&quot; onclick=&quot;showRejectionReasonAlert()&quot;&gt;Reject&lt;/button&gt;
  3. &lt;/form&gt;
  4. &lt;script&gt;
  5. function showRejectionReasonAlert() {
  6. var rejectionReason = prompt(&quot;Please enter the rejection reason:&quot;);
  7. if (rejectionReason != null) {
  8. document.getElementById(&quot;rjctrsn-data&quot;).value = rejectionReason;
  9. document.getElementById(&quot;yourFormId&quot;).submit();
  10. }
  11. }
  12. &lt;/script&gt;

Ensure to properly set up your database connection before executing the SQL.

  1. &lt;?php
  2. if (isset($_POST[&#39;rejection_reason&#39;]) &amp;&amp; !empty($_POST[&#39;rejection_reason&#39;])) {
  3. $Note = $_POST[&#39;rejection_reason&#39;];
  4. $whyreject = &#39;Rejection_Reason-&#39; . $Note;
  5. $sql = &quot;UPDATE requestitem SET requestitem_UsefulNote=:whyreject&quot;;
  6. $query = $dbh-&gt;prepare($sql);
  7. $query-&gt;bindParam(&#39;:whyreject&#39;, $whyreject);
  8. if ($query-&gt;execute()) {
  9. $success = &quot;Rejection Confirmed Successfully and Reason Sent.&quot;;
  10. } else {
  11. $error = &quot;Sorry, Rejection Failed, Try again Later.&quot;;
  12. }
  13. }

huangapple
  • 本文由 发表于 2023年7月20日 15:03:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76727410.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定