0

I'm having trouble displaying the results of a PHP/JSON array in a php field.

The value of interest is number 9... 'ptm:yes/no'. This is based on whether a field in a db is null or not null. This is obviously working because the correct data is returned.

I have tested that I am getting the correct results like this: enter image description here

enter image description here

However I'm having trouble getting this to display in a php based form. Item number 3

<tr>
  <td>
    <span class="qText" name="was_PMV"> 3. Did file start as a PMV?</span>
  </td>
  <td>
    <select class="qAns" name="was_PMV_a">
      <option value="No">No</option>
      <option value="Yes">Yes</option>
    </select>
  </td>
</tr>

And the javascript:

function setOther(data){
    var jdata=JSON.parse(data);
    document.getElementsByName("date_of_incident_a")[0].value=jdata.doi;
    document.getElementsByName("date_of_incident_s")[0].innerHTML=jdata.doi;
    document.getElementsByName("transfer_to_lit_a")[0].value=jdata.ttl;
    document.getElementsByName("transfer_to_lit_s")[0].innerHTML=jdata.ttl;
    document.getElementsByName("filed_suit_a")[0].value=jdata.fsd;
    document.getElementsByName("filed_suit_s")[0].innerHTML=jdata.fsd;
    document.getElementsByName("phase_assigned_a")[0].value=jdata.pa;
    document.getElementsByName("phase_assigned_s")[0].innerHTML=jdata.pa;
    alert ("value to set is : " + jdata.ptm);
    $("[name='was_PMV_a')").val(jdata.ptm);
    document.getElementsByName("was_PMV_s")[0].innerHTML=jdata.ptm;
    document.getElementsByName("trial_date_a")[0].value=jdata.td;
    document.getElementsByName("trial_date_s")[0].innerHTML=jdata.td;
    document.getElementsByName("ALocation_a")[0].value=jdata.al;
    document.getElementsByName("settlement_t4")[0].value=Math.round(jdata.sto);
    formatValue('4');

I have a feeling that the disconnect is on the recoveryForm.php... does this need to be a dropdown menu? What would be the simplest way to display the returned value of jdata.ptm (yes/no) on the form?

This is what it looks like enter image description here

No matter what I do the value on the form shows as "No"... I'm thinking this has to be a simple fix on the form?

EDIT - I believe I have isolated my problem to my getOther.php file, does anybody see any discrepencies?

    <?php
    $success=FALSE;
    $postCasenum = getPostCasenum();
    #header('Content-Type: application/json');

    $sql = "select cases.date_of_incident, cases.case_date_9, cases.case_date_3, user_tab6_data.Trial_Phase, cases.case_date_5, user_case_data.ALocation, cases.staff_1, cases.staff_8, user_case_data.PMV_to_MVA, ";
    $sql=$sql."(select sum(insurance.actual) from insurance where case_num=cases.casenum) as sumTotal ";
    $sql=$sql."from cases LEFT JOIN user_tab6_data ON cases.casenum=user_tab6_data.case_id ";
    $sql=$sql."LEFT JOIN user_case_data ON cases.casenum=user_case_data.casenum ";
    $sql=$sql."WHERE cases.casenum=?";
    $conn = odbc_connect( "needles","dba","sql" );
    if( $conn ) {
        #echo $sql;
        $stmt=odbc_prepare($conn, $sql);
        $queryResult=odbc_execute($stmt, array($postCasenum));

        if (odbc_fetch_row($stmt)) {
            $data = [
                'doi' => odbc_result($stmt,1),
                'ttl' => odbc_result($stmt,2),
                'fsd' => odbc_result($stmt,3),
                'pa' => odbc_result($stmt,4),
                'td' => odbc_result($stmt,5),
                'al' => prepString(odbc_result($stmt,6)),
                'sl' => odbc_result($stmt,7),
                's8' => odbc_result($stmt,8),
                'ptm' => empty(odbc_result($stmt,9)) ? "No" : "Yes",
                'sto' => odbc_result($stmt,10)
            ];
            echo json_encode($data);
        }
 //       while(odbc_fetch_row($stmt)){
 //         $json=$json.'"doi":"'.odbc_result($stmt,1).'",';
 //           $json=$json.'"ttl":"'.odbc_result($stmt,2).'",';
 //           $json=$json.'"fsd":"'.odbc_result($stmt,3).'",';
 //           $json=$json.'"pa":"'.odbc_result($stmt,4).'",';
  //          $json=$json.'"td":"'.odbc_result($stmt,5).'",';
 //           $json=$json.'"al":"'.prepString(odbc_result($stmt,6)).'",';
 //           $json=$json.'"s1":"'.odbc_result($stmt,7).'",';
 //           $json=$json.'"s8":"'.odbc_result($stmt,8).'",';
//          $json=$json.'"ptm":"'.odbc_result($stmt,9).'",';
//            $json=$json.'"sto":"'.odbc_result($stmt,10).'"';
//        }
//        $json=$json.'}';
//        echo $json;
        #echo "you found me!";
        odbc_close( $conn );
    } else {
        echo "{}";
    }
?>
<?php
    function getPostCasenum(){
        return $_POST["casenum"];
    }
    function prepString($myStr){
        $ret=str_replace('"',"",$myStr);
        return $ret;
    }
?>
3
  • Try document.getElementsByName("was_PMV_s")[0].value = jdata.ptm; Commented Jun 8, 2020 at 19:05
  • no luck, thanks though. previously, the document.getElementsByName for the ptm item followed suite with all of the others, that was changed by somebody elses suggestion... either way neither of them work. Commented Jun 8, 2020 at 19:28
  • There's a syntax error here, Try by fixing this first: $("[name='was_PMV_a')").val... should be $("[name='was_PMV_a']").val Commented Jun 8, 2020 at 19:38

2 Answers 2

1

Just tested and yes it should work with:

$("[name='was_PMV_a']").val(jdata.ptm);

Your code:

$("[name='was_PMV_a')").val(jdata.ptm);
//error here -------^

It's a simple syntax error in your code. You really should check the console (F12 on Chrome)

Edit: See fiddle in reply to your comment https://jsfiddle.net/ofh4yzmr/

Sign up to request clarification or add additional context in comments.

2 Comments

Indeed, I do see that syntax error and I fixed it. But still, the field always shows as "No" when it should show as "Yes". Does the form field need to be a drop-down menu? Isn't there a simpler way to input the raw data contained in jdata.ptm right into an empty field? Thank you for pointing that out,however
That's up to you. You can use select or radio options. See the fiddle in updated post. To demonstrate, I set the value to your variable before select changes. The issue is most likely where your function is being called.
0

The problem you have is that you try to assign the selected option using the value property of select tag and this is not possible in html (I do not know if this is possible using jQuery). The way to do this is by creating the option elements and checking individually if their state should be selected. Please take a look at the following example, run and modify the ptm value to Yes or No and you will see that the select selects the appropriate option

Please read the new Option constructor

// sample data
const data = {
  doi: "2014-05-01",
  ttl: null,
  fsd: "2017-01-21",
  pa: null,
  td: null,
  al: "",
  sl: "ZTRM",
  s8: "",
  ptm: "No",
  st: 50000,
};

const ptm = data.ptm;
const select = document.querySelector('select[name="was_PMV_a"]');
select.innerHTML = "";

for (const entry of ["Yes", "No"]) {
  const option = new Option(entry, entry, entry === ptm, entry === ptm);

  select.appendChild(option);
}
<select class="qAns" name="was_PMV_a">
  <option value="No">No</option>
  <option value="Yes">Yes</option>
</select>

Update 0

Try adding this function

function populatePTM(ptm) {
  const select = document.querySelector('select[name="was_PMV_a"]');

  select.innerHTML = "";

  for (const entry of ["Yes", "No"]) {
    const option = new Option(entry, entry, entry === ptm, entry === ptm);

    select.appendChild(option);
  }
}

And modify your setOther function replacing this section

alert ("value to set is : " + jdata.ptm);
$("[name='was_PMV_a')").val(jdata.ptm);

For a call to populatePTM like this populatePTM(jdata.ptm);

3 Comments

I tried to implement your solution, but alas, same result. I am basically useless when it comes to javascript. I placed your code in my recoveryForm.js and removed the 2 lines (alert line, and line below it) and replaced with your code. Is this the correct way to implement your solution? Of course, I excluded the sample data, and just entered the js lines below your example json.
When you say "try adding this function" do you mean in addition to your original answer?
Yes, replace the alert line an the next one in the setOther method by populatePTM(jdata.ptm); this is one way to go

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.