-1

I have a html table, each table row have a radio button dynamically generated. Each option in the radio button have a unique id that generated dynamically also. But this id is not yet save in the database.

How to insert the option id? And how to update the option answer in that option id? Please help me. I tried to insert the values but I have no luck

Scenario:

There's a default value for the radio button, which is "No". When the user change the default value, there's a confirmation box that will ask the user if he/she want to processed. If the user click "Ok" the default value will change into "Yes".

PHP for html table:

    echo '<td id="resumeFile'.$optionId.'">' . $record_s->attachment_resume_id . '</td>';
    echo '<td id="processedYes><label for="Yes">Yes</label>
              <input type="radio" id="processedOptionYes'.$optionId.'" name="processedOption" value="Yes" onclick="proccessedCheck('.$optionId.',\'Yes\')"/>
              <label for="No">No</label>
              <input type="radio" id="processedOptionNo'.$optionId.'" name="processedOption" value="No" checked="checked" onclick="proccessedCheck('.$optionId.',\'No\')" echo $record_s->process_resume === "No" checked="checked"/>/>No</td>';
    echo '</tr>';
        }
    echo '</table>';
}

if (isset($_POST['optionId']) && $_POST['optionId']){
    $optionId = $_POST['optionId'];
    $queryOptionId = $wpdb->query("INSERT INTO resume_databank(process_resume_id) VALUES ('$optionId')");
}

Hidden Form:

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

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();
        }
    }
}
User014019
  • 1,215
  • 8
  • 34
  • 66

2 Answers2

0

I dont use Jquery, but Javascript is pretty simple to read the value. It is the same as a checkbox value in that it is .checked when true.

Loop through your form fields looking for checked items

var formObj = document.getElementById('hiddenform');
for(var i = 0;i < formObj.elements.length;i++){
   radiovalues[] = escape(formObj.elements[id].checked);
}

Most fields have a value, ie text, hidden, password etc

escape(formObj.elements[id].value)

The checkbox and radio doesnt have a value, you are looking for "checked" which will return true or false.

PHPDev
  • 81
  • 5
  • so i will put that after the jquery function? so i will create a new function and call it in the hidden form? – User014019 May 25 '16 at 04:20
  • This is where you have ` if(optionAnswer == 'Yes')`, your looking for the value, i would be looking for .checked. http://stackoverflow.com/questions/1423777/how-can-i-check-whether-a-radio-button-is-selected-with-javascript – PHPDev May 25 '16 at 04:28
  • `proccessedCheck(optionId)` and `if(optionId.checked)` – PHPDev May 25 '16 at 04:32
0

Hi u can change the jquery by using like below with using a class instead of function in the input type, add a class radiods to input type= radio.

$(".radiods").click(function(){
    var clickid = this.id;
    if($('input:radio[name=processedOption]:checked').val() == "Yes")
    {
        if (confirm('You have chosen YES, is this correct?'))
        {
            $("#inputHidden1").val(clickid);
            $("#inputHidden2").val("Yes");
        }
    }
});

and then use ajax to update in database,so no need of form

krishna
  • 7
  • 5
  • do u require for ajax or above example – krishna May 25 '16 at 10:02
  • it's ok now :) i have a new question relevant with this question http://stackoverflow.com/questions/37431587/cascade-dropdown-not-working-using-js/37431909?noredirect=1#comment62369831_37431909 – User014019 May 25 '16 at 10:09