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

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

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

问题

UI

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

PHP提交脚本的代码

if (isset($_POST['rejectreason'])){
    $Note = $_POST['rejection_reason'];
    $whyreject = 'Rejection_Reason-' . $Note;

    $sql = "UPDATE requestitem SET requestitem_UsefulNote='$whyreject'";
    $query = $dbh->prepare($sql);
    if ($query->execute()){
        $success="拒绝成功,并发送原因。";          
    } else {
        $error="抱歉,拒绝失败,请稍后重试。";
    }
}

取消按钮的代码

<input type="hidden" id="rjctrsn-data" href="" name="rejection_reason">	
<a type="submit" name="rejectreason" onclick="document.getElementById('rjctrsn-data').value = prompt('您确定要拒绝此项吗!:', '请说明拒绝的原因');"
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>
英文:

UI

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

code for the PHP Submit Script

if (isset($_POST[&#39;rejectreason&#39;])){
$Note = $_POST[&#39;rejection_reason&#39;];
$whyreject = &#39;Rejection_Reason-&#39;.$Note;

 $sql = &quot;UPDATE requestitem SET requestitem_UsefulNote=&#39;$whyreject&#39; &quot;;
 $query = $dbh-&gt;prepare($sql);
 if ($query-&gt;execute()){
      $success=&quot;Rejection Confirmed Successfully and Reason Sent.&quot;;          
}else {
      $error=&quot;Sorry, Rejection Failed, Try again Later.&quot;;
      }
}

code for the cancel button

 &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

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

<form action="submit_script.php" method="post">
  <button type="button" onclick="showRejectionReasonAlert()">拒绝</button>
</form>

<script>
function showRejectionReasonAlert() {
  var rejectionReason = prompt("请输入拒绝原因:");
  if (rejectionReason != null) {
    document.getElementById("rjctrsn-data").value = rejectionReason;
    document.getElementById("yourFormId").submit();
  }
}
</script>

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

<?php

if (isset($_POST['rejection_reason']) && !empty($_POST['rejection_reason'])) {
    $Note = $_POST['rejection_reason'];
    $whyreject = 'Rejection_Reason-' . $Note;

    $sql = "UPDATE requestitem SET requestitem_UsefulNote=:whyreject";
    $query = $dbh->prepare($sql);
    $query->bindParam(':whyreject', $whyreject);

    if ($query->execute()) {
        $success = "拒绝成功,原因已发送。";
    } else {
        $error = "抱歉,拒绝失败,请稍后重试。";
    }
}
英文:

Trigger the onclick alert input event and get the user input

&lt;form action=&quot;submit_script.php&quot; method=&quot;post&quot;&gt;
  &lt;button type=&quot;button&quot; onclick=&quot;showRejectionReasonAlert()&quot;&gt;Reject&lt;/button&gt;
&lt;/form&gt;



&lt;script&gt;
function showRejectionReasonAlert() {
  var rejectionReason = prompt(&quot;Please enter the rejection reason:&quot;);
  if (rejectionReason != null) {
    document.getElementById(&quot;rjctrsn-data&quot;).value = rejectionReason;
    document.getElementById(&quot;yourFormId&quot;).submit();
  }
}
&lt;/script&gt;

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

&lt;?php

if (isset($_POST[&#39;rejection_reason&#39;]) &amp;&amp; !empty($_POST[&#39;rejection_reason&#39;])) {
    $Note = $_POST[&#39;rejection_reason&#39;];
    $whyreject = &#39;Rejection_Reason-&#39; . $Note;

    $sql = &quot;UPDATE requestitem SET requestitem_UsefulNote=:whyreject&quot;;
    $query = $dbh-&gt;prepare($sql);
    $query-&gt;bindParam(&#39;:whyreject&#39;, $whyreject);

    if ($query-&gt;execute()) {
        $success = &quot;Rejection Confirmed Successfully and Reason Sent.&quot;;
    } else {
        $error = &quot;Sorry, Rejection Failed, Try again Later.&quot;;
    }
}

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:

确定