简体   繁体   English

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

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

I have a PHP snippet that display the table row dynamically. 我有一个PHP代码段,可动态显示表行。 Every row I there's a radio button with "Yes" and "No" option. 我每一行都有一个带有“是”和“否”选项的单选按钮。

I created a JS function, when the user choose an option, there's a pop-box will be displayed. 我创建了一个JS函数,当用户选择一个选项时,将显示一个弹出框。

If the user choose "Yes" option in the radio button and click "Ok" in the pop-box, the table row will be disabled even the radio button will be disable too. 如果用户在单选按钮中选择“是”选项,然后在弹出框中单击“确定”,则即使单选按钮也将被禁用,表行也将被禁用。 And the chosen option will be save in MYSQL. 所选的选项将保存在MYSQL中。

How to save the chosen option in MySQL? 如何在MySQL中保存所选选项?

My JS snippet of disabling a row is not working. 我的禁用行的JS代码段不起作用。 How to fix this? 如何解决这个问题?

PHP: 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: 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?');
    }
}

Ok so if you are echo'ing the whole table from PHP just preset the parameters into the table 好的,如果您要从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>

Contents of savedb.php, this doesn't have to be a seperate file 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


?>

For starters, try replacing: 对于初学者,请尝试更换:

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

with: 与:

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

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

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