0

I have custom page template. In this template have a html table with radio button in each table row. I tried to retrieve the value of radio button from MYSQl but I have no luck.

Scenario: If the user choose "Yes" option, the table row will be disabled. Now, the value of radio button is retrieve but not the disabled featured of "Yes" option.

How to fix this?

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();
        }
    }
}

Hidden form and 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>
1
  • If you have any more comments, you should do it here. The Moderators are strictly against extended discussion. And by the way; the Code is now updated... try if it does you any good ;-) Commented May 26, 2016 at 9:07

3 Answers 3

1

Wouldn't you want to make it a little easier & manageable by simply moving the Conditionals out of the Radio Buttons Fields? Creating 2 Variables & assigning values to them based on some conditions wouldn't cost you so much & would rather make things much clearer. Here, attempt was made to just create 2 variables: $checkYes & $checkNo before echoing out any thing. Then we checked if $result_position[$i]->process_resume=="Yes" in which case, we assigned "checked" to the $checkYes Variable. We did the same for NO. Afterwards, we then added the $checkYes or $checkNo at the appropriate slots...

    <?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>';
Sign up to request clarification or add additional context in comments.

8 Comments

i have an errors: Uninitialized string offset: 2, Notice: Trying to get property of non-object
Yes; the Error might probably be coming form the fact that you put the IF Statement outside your Loop. Both the variables & conditional statements should be within the Loop. If the conditional (IF) is outside the Loop; then $i has no meaning because it wouldn't have existed at that point.... That is why you might be having that error...
but only 1 radio is selected, the other radio button in other rows are not selected
Do you have a visual? I mean like a screen-shot to share? If you do, would you mind to share it? Add perhaps also a screen-shot of your code especially the part with the Loop...
By the way; the Problem is now obvious. The names of the Radio Buttons is FIXED in all the rows (exactly the same, regardless of if there are other rows or not). In this case, it is logical that only ONE be checked. However, you can get over this by simply suffixing the name of the Radio-Button Field with the $optionId like you did with the ID attribute. That way, you most likely would get the result you had expected.
|
1

Radio buttons in the same group should have the same name. You can then get which button was selected by using that name, and the value will be the value of the selected button (either "Yes" or "No", in this case).

In this case, you might do something like $value = $_REQUEST['processedOption']; to get it.

Note, though, this setup will only work if your table has one such set of buttons. If you have multiple rows in this table, you're going to need a different name for each group, or you'll only ever be able to see the last one. If that's the case, i'd name them like processedOption[$optionId], so that PHP will gather them up into an array for you. (The option IDs will be keys, and the "Yes"es and "No"s will be the values.)

7 Comments

no, i only have one name in all radio button in each table row. the radio button is generated depends in the numbers of rows
the radio button have only 1 name. the radio button is generated dynamically
So there is more than one row, and every row in the table has the same names for its buttons?
did you noticed this part id="processedOptionYes'.$optionId.'" the $optionId in every row is unique
I've edited to mention how you'd do that reliably. Name them 'processedOption[' . $optionId . ']', and $_REQUEST['processedOption'] becomes an array of $optionId => yes/no. It'd also work if you named them 'processedOption' . $optionId, but IMO that's a lot clunkier. Either way, the names have to be unique per group if you want the yes/no buttons to operate independently for each group.
|
0

You can use a shorthand if-else inside your echo ( ( $result_location[$i]["process_resume"] == "Yes" ) ? " checked" : '' )

I thereby presume that you get the value from your database using $result_location[$i]["process_resume"]. Seeing the rest of your code, I guess that it should be $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>';

1 Comment

i changed the $result_location[$i]["process_resume"] into this $result_position[$i]->process_resume but i got these errors: Notice: Trying to get property of non-object, Notice: Uninitialized string offset: 1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.