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

时间:2016-05-26 07:31:53

标签: php mysql wordpress radio-button custom-wordpress-pages

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

场景:如果用户选择“是”选项,则表行将被禁用。现在,单选按钮的值是检索但不是禁用的“是”选项。

如何解决这个问题?

PHP UPDATED

        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>

3 个答案:

答案 0 :(得分:1)

同一组中的单选按钮应具有相同的名称。然后,您可以使用该名称获取选择的按钮,该值将是所选按钮的值(&#34;是&#34;或&#34;否&#34;,在这种情况下)。 / p>

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

请注意,此设置仅在您的表具有一组这样的按钮时才有效。如果此表中有多行,则您需要为每个组指定不同的名称,或者您只能看到最后一个。如果是这种情况,我将它们命名为processedOption[$optionId],以便PHP将它们收集到一个数组中。 (选项ID将是键,&#34;是&#34; es和&#34;否&#34; s将是值。)

答案 1 :(得分:1)

你不想让它变得更容易吗?只需将条件数移出无线电按钮字段即可管理?创建2个变量&amp;根据某些条件为它们分配值不会花费你这么多钱。宁愿让事情更清楚。在这里,尝试只创建2个变量:$ checkYes&amp;在回显任何事情之前$ checkNo。然后我们检查$ result_position [$ i] - &gt; process_resume ==“是”在这种情况下,我们将“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>';

答案 2 :(得分:0)

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

中使用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>';