繁体   English   中英

如何使用JS禁用表行并将所选选项插入MYSQL?

[英]How to disabled table row using JS and insert the chosen option in MYSQL?

我有一个PHP代码段,可动态显示表行。 我每一行都有一个带有“是”和“否”选项的单选按钮。

我创建了一个JS函数,当用户选择一个选项时,将显示一个弹出框。

如果用户在单选按钮中选择“是”选项,然后在弹出框中单击“确定”,则即使单选按钮也将被禁用,表行也将被禁用。 所选的选项将保存在MYSQL中。

如何在MySQL中保存所选选项?

我的禁用行的JS代码段不起作用。 如何解决这个问题?

PHP:

echo '<td id="resumeFile"><a href="' . $dir  . $file . '">Download Resume</a></td>';
        echo '<td id="radioOption">
                  <label for="Yes">Yes</label>
                    <input type="radio" id="processedOptionYes" name="processedOption" value="Yes" onclick="proccessedCheck()"/>
                  <label for="No">No</label>
                    <input type="radio" id="processedOptionNo" name="processedOption" value="No" onclick="proccessedCheck()"/></td>';

JS:

function proccessedCheck(){
    var checked = null;
    var inputs = document.getElementsByName('processedOption');
        for (var i = 0; i < inputs.length; i++){
            if (inputs[i].checked) {
            checked = inputs[i];
            break;
            }
        }

    if(checked == null){
        return false;
    } else if (checked == true){
            document.getElementById("resumeFile").disabled = true;
            document.getElementById("radioOption").disabled = true;
            document.getElementById("resumeFile").title = "This option has been disabled.";
    } else {
        return confirm('You have chosen '+ checked.value + ', is this correct?');
    }
}

好的,如果您要从PHP中回显整个表,只需将参数预置到表中

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
<script   src="https://code.jquery.com/jquery-2.2.3.min.js"   integrity="sha256-a23g1Nt4dtEYOj7bR+vTu7+T8VP13humZFBJNIYoEJo="   crossorigin="anonymous"></script> 
<script>
function proccessedCheck(id,answer) {
    if (confirm('You have chosen '+ id +': '+ answer + ', is this correct?')) {
        $("#processedOptionYes"+id).attr('disabled',true);
        $("#processedOptionNo"+id).attr('disabled',true);
        var withlink = $("#resumeFile"+id).html();
        var withoutlink = $(withlink).html();
        $("#resumeFile"+id).html("").append(withoutlink);
        $("#input1".val(id);
        $("#input2".val(answer);
        $("#myform").submit();

    }     
}
</script>

<!-- EDIT: hidden form to submit -->

<form id="myform" method="POST" action="savedb.php">
  <input type="hidden" id="input1" name="id" />
<input type="hidden" id="input2" name="answer" />
</form>


<table>
  <tr>
    <?php
$dir="";
$file="";
$id = 0;
//foreach($array as $row) {
    $id++;
    echo '<td id="resumeFile'.$id.'"><a href="' . $dir  . $file . '">Download Resume</a></td>';
        echo '<td id="radioOption>
                  <label for="Yes">Yes</label>
                    <input type="radio" id="processedOptionYes'.$id.'" name="processedOption" value="Yes" onclick="proccessedCheck('.$id.',\'Yes\')"/>
                  <label for="No">No</label>
                    <input type="radio" id="processedOptionNo'.$id.'" name="processedOption" value="No" onclick="proccessedCheck('.$id.',\'No\')"/></td>';
//}
                    ?>
  </tr>
</table>
</body>
</html>

saveb.php的内容,不必是单独的文件

<?php

// Check if my post array arrived, comment this line when u done
echo "<pre>";print_r($_REQUEST);echo "</pre>"; die();

// Connect to DB
// Build SQL insert string with $_REQUEST['id'] as the primary key


?>

对于初学者,请尝试更换:

        document.getElementById("resumeFile").disabled = true;
        document.getElementById("radioOption").disabled = true;

与:

        document.getElementById("processedOptionYes").disabled = true;
        document.getElementById("processedOptionNo").disabled = true;

暂无
暂无

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

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