如何使用 PHP 从 MYSQL 检索值到单选按钮?

How to retrieve value from MYSQL to radio button using PHP?

我有自定义页面模板。在此模板中,每个 table 行中有一个 html table 和单选按钮。我试图从 MYSQl 检索单选按钮的值,但我没有运气。

场景:如果用户选择"Yes"选项,table行将被禁用。现在,单选按钮的值是检索,而不是 "Yes" 选项的禁用功能。

如何解决这个问题?

PHP 已更新

        if (isset($_POST['list_location']) && $_POST['list_location'] != 'Select by Location'){
    $list_location = $_POST['list_location'];
    $result_location = $wpdb->get_results($wpdb->prepare("SELECT DISTINCT id, submit_time, last_name, first_name, middle_name, mobile_number, email, location, position, message, attachment_resume_id, process_resume FROM resume_databank WHERE location = '" . $list_location . "' ORDER BY location ASC", OBJECT));

        echo '<table>';
        echo '<tr>';
        $optionId = 0;
        echo '<th>Submit Time</th>';
        echo '<th>Last Name</th>';
        echo '<th>First Name</th>';
        echo '<th>Middle Name</th>';
        echo '<th>Mobile Number</th>';
        echo '<th>Email</th>';
        echo '<th>Location</th>';
        echo '<th>Position</th>';
        echo '<th>Message</th>';
        echo '<th>Resume</th>';
        echo '<th>Processed?</th>';
    for ($i=0;$i < count($result_location); $i++){
        $row = $result_location[$i];
        $optionId = $result_location[$i]->id;
        $checkYes   = "";
        $checkNo    = "";

        if($result_location[$i]->process_resume == "Yes"){
            $checkYes = "checked";
        }
        if($result_location[$i]->process_resume == "No"){
            $checkNo = "checked"; 
        }

    echo '<tr>';
        echo '<td id="submit_time">' . $result_location[$i]->submit_time . '</td>';
        echo '<td id="last_name">' . $result_location[$i]->last_name . '</td>';
        echo '<td id="first_name">' . $result_location[$i]->first_name . '</td>';
        echo '<td id="middle_name">' . $result_location[$i]->middle_name . '</td>';
        echo '<td id="mobile_number">' . $result_location[$i]->mobile_number . '</td>';
        echo '<td id="email">' . $result_location[$i]->email . '</td>';
        echo '<td id="location">' . $result_location[$i]->location . '</td>';
        echo '<td id="position">' . $result_location[$i]->position . '</td>';
        echo '<td id="message">' . $result_location[$i]->message . '</td>';
        echo '<td id="resumeFile'.$optionId.'"><a href=' . wp_get_attachment_url($result_location[$i]->attachment_resume_id) . '>Download Resume</a></td>';
        echo '<td id="radioOption><label for="Yes">Yes</label>
            <input type="radio" id="processedOptionYes'.$optionId.'" name="processedOption'.$optionId.'" ' . $checkYes . ' value="Yes" onclick="proccessedCheck('.$optionId.',\'Yes\')"/>
            <label for="No">No</label>
            <input type="radio" id="processedOptionNo'.$optionId.'" name="processedOption'.$optionId.'" ' . $checkNo . ' value="No" onclick="proccessedCheck('.$optionId.',\'No\')"/></td>';
    echo '</tr>';
    }
    echo '</table>';
}

JS:

function proccessedCheck(optionId,optionAnswer){
    if(optionAnswer == 'Yes'){
        if (confirm('You have chosen ' + optionAnswer + ', is this correct?')){
            jQuery("#processedOptionYes" + optionId).attr('disabled',true);
            jQuery("#processedOptionNo" + optionId).attr('disabled',true);
            var withlink = jQuery("#resumeFile"+ optionId).html();
            var withoutlink = jQuery(withlink).html();
            jQuery("#resumeFile"+optionId).html("").append(withoutlink);
            jQuery("#inputHidden1").val(optionId);
            jQuery("#inputHidden2").val(optionAnswer);
            jQuery("#hiddenForm").submit();
        }
    }
}

隐藏形式和PHP:

<?php
if (isset($_POST['optionAnswer']) && $_POST['optionAnswer']){
    $optionId = $_POST['optionId'];
    $optionAnswer = $_POST['optionAnswer'];
    $queryOptionId = $wpdb->query($wpdb->prepare("UPDATE resume_databank SET process_resume = '$optionAnswer' WHERE id = %d", $optionId));
}
?>

<form id='hiddenForm' method='POST' action=''>
    <input type="hidden" id="inputHidden1" name="optionId" />
    <input type="hidden" id="inputHidden2" name="optionAnswer" />
</form>

同一组中的单选按钮应具有相同的名称。然后,您可以使用该名称获取选择了哪个按钮,该值将是所选按钮的值(在本例中为 "Yes" 或 "No")。

在这种情况下,您可以执行类似 $value = $_REQUEST['processedOption']; 的操作来获取它。

但请注意,此设置仅在您的 table 具有一组这样的按钮时才有效。如果您在此 table 中有多个行,您将需要为每个组使用不同的名称,否则您将只能看到最后一个。如果是这样的话,我会将它们命名为 processedOption[$optionId],这样 PHP 就会为您将它们收集到一个数组中。 (选项 ID 将是键,"Yes"es 和 "No"s 将是值。)

您可以在 echo ( ( $result_location[$i]["process_resume"] == "Yes" ) ? " checked" : '' )

中使用 shorthand if-else

因此我假设您使用 $result_location[$i]["process_resume"] 从数据库中获取值。看到你剩下的代码,我猜应该是 $result_position[$i]->process_resume.

echo '<td id="radioOption><label for="Yes">Yes</label>';
echo '<input type="radio" id="processedOptionYes'.$optionId.'" name="processedOption"'.( ( $result_location[$i]["process_resume"] == "Yes" ) ? " checked" : '' ).'value="Yes" onclick="proccessedCheck('.$optionId.',\'Yes\')"/>';
echo '<label for="No">No</label>';
echo '<input type="radio" id="processedOptionNo'.$optionId.'" name="processedOption"'.( ( $result_location[$i]["process_resume"] == "No" ) ? " checked" : '' ).'value="No" onclick="proccessedCheck('.$optionId.',\'No\')"/></td>';

您不想通过简单地将条件从单选按钮字段中移出来让它变得更容易和更易于管理吗?创建 2 个变量并根据某些条件为它们分配值不会花费您太多,而且会让事情变得更清楚。在这里,尝试仅创建 2 个变量:$checkYes 和 $checkNo,然后再回显任何内容。然后我们检查是否 $result_position[$i]->process_resume=="Yes" 在这种情况下,我们将 "checked" 分配给 $checkYes 变量。我们为 NO 做了同样的事情。之后,我们在适当的位置添加了 $checkYes 或 $checkNo...

    <?php
        // BE SURE THAT BOTH THE VARIABLES & CONDITIONAL STATEMENTS EXIST
        // WITHIN YOUR LOOP... PREFERABLLY AT THE BEGINNING OF THE LOOP.
        // OTHERWISE; $i HAS NO MEANING TO THE CONDITIONAL STATEMENTS BELOW.
        // CREATE THE $checkYes & $checkNo VARIABLES + INITIALIZE THEM TO EMPTY STRINGS
        $checkYes    = "";
        $checkNo     = "";
        // NEXT ASSIGN "checked" TO THE $checkYes OR $checkNo VARAIBLE
        // IF ANY OF THE CONDITIONS BELOW IS SATISFIED...
        // WE'LL USE THESE VARIABLES BELOW: WITHIN THE RADIO BUTTONS SECTION
        if($result_position[$i]->process_resume=="No"){
            $checkNo  = "checked"; 
        }
        if($result_position[$i]->process_resume=="Yes"){
            // TO DISABLE ALL RADIO BUTTONS WITH A "YES" VALUE;
            // SIMPLE ADD "disabled" TO THE $checkYes STRING LIKE SO:::
            $checkYes = "checked  disabled";
            $checkNo  = "disabled"; 
        }

        echo '<td id="submit_time">'    . $result_position[$i]->submit_time     . '</td>';
        echo '<td id="last_name">'      . $result_position[$i]->last_name       . '</td>';
        echo '<td id="first_name">'     . $result_position[$i]->first_name      . '</td>';
        echo '<td id="middle_name">'    . $result_position[$i]->middle_name     . '</td>';
        echo '<td id="mobile_number">'  . $result_position[$i]->mobile_number   . '</td>';
        echo '<td id="email">'          . $result_position[$i]->email           . '</td>';
        echo '<td id="location">'       . $result_position[$i]->location        . '</td>';
        echo '<td id="position">'       . $result_position[$i]->position        . '</td>';
        echo '<td id="message">'        . $result_position[$i]->message         . '</td>';
        echo '<td id="resumeFile'       . $optionId . '"><a href='              . wp_get_attachment_url($result_position[$i]->attachment_resume_id) . '>Download Resume</a></td>';
        echo '<td id="radioOption><label for="Yes">Yes</label>

    <input type="radio" id="processedOptionYes'.$optionId.'" name="processedOption_' . $optionId . '" ' . $checkYes . ' value="Yes" onclick="proccessedCheck('.$optionId.',\'Yes\')"/>
    <label for="No">No</label>
    <input type="radio" id="processedOptionNo'. $optionId.'" name="processedOption_' . $optionId . '" ' . $checkNo . '  value="No" onclick="proccessedCheck('.$optionId.',\'No\')"/></td>';