0

I have a div "BrowseDiv" which is in else part of php code as shown below. if $url is not exists, it should show the div. Also When check the check box , I'm calling a java script which should display the div.But it is not displaying when onchange checkbox is calling

<tr height="20px" >     
    <td style="text-align:center">
    <?php if(file_exists($url)){ ?>
        <a href=<?php echo "documents/".$num."/".$id;?> target="_blank" id="href_doc1">My Doc</a>
         </br>
         <input type="checkbox" id="check_doc1" name="check_doc1"  onchange="CheckedDelete('check_doc1')">Delete My Doc</input>
        <?php } else { ?>
        </br></br>
        <div id="BrowseDiv" ><b></br>Upload Supporting Document</b> </br> 
        <input type="file" name="doc1_upload_onload" id="doc1_upload_onload"> 
        </div> 
     } ?>
    </td>       
</tr>


function CheckedDelete(chk_bx){

    if (document.getElementById(chk_bx).checked == true) {    
      alert(" will be deleted");      
      var href_doc1 = document.getElementById('href_doc1');
      href_doc1.style.display = 'none'; 
      document.getElementById('BrowseDiv').style.display= 'block';      
    } else {
      var Thephoto = document.getElementById('href_doc');
      Thephoto.style.display = 'block';
      document.getElementById('BrowseDiv').style.display= 'none';   
    }
}
2
  • Change if (document.getElementById(chk_bx).checked == true) { to if (document.getElementById(chk_bx).checked) { Commented Mar 22, 2017 at 5:53
  • That is because your BrowseDiv is not present in DOM so your getElementById is throwing an error. Remember that it will only show if file doesn't exist, then your getElementById will work as expected. Commented Mar 22, 2017 at 5:53

2 Answers 2

1

The "BrowseDiv" is in else part and hence document.getElementById('BrowseDiv') must be returning NULL.

For this you need to change your logic a bit. Something like this:

<td style="text-align:center"><?php if(file_exists($url)){ ?>
<a href=<?php echo "documents/".$num."/".$id;?> target="_blank" id="href_doc1">My Doc</a>
 </br>
 <input type="checkbox" id="check_doc1" name="check_doc1"  onchange="CheckedDelete('check_doc1')">Delete My Doc</input>
<?php   } ?>
</br></br>
<div id="BrowseDiv" style="display: <?php echo (file_exists($url)) ? 'none' : 'block' ?>" ><b></br>Upload Supporting Document</b> </br> 
<input type="file" name="doc1_upload_onload" id="doc1_upload_onload"> 
</div>

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

1 Comment

shows Parse error: syntax error, unexpected 'echo' (T_ECHO) i
1

You should do this instead..

<tr height="20px" >     
    <td style="text-align:center"><?php if(file_exists($url)){ ?>
    <a href=<?php echo "documents/".$num."/".$id;?> target="_blank" id="href_doc1">My Doc</a>
     </br>
     <input type="checkbox" id="check_doc1" name="check_doc1"  onchange="CheckedDelete('check_doc1')">Delete My Doc</input>
    <?php  } ?>
    </br></br>
    <div id="BrowseDiv" style="display: <?php if(file_exists($url) { echo 'none'; } else { echo 'block'; } ?>"><b></br>Upload Supporting Document</b> </br> 
    <input type="file" name="doc1_upload_onload" id="doc1_upload_onload"> 
    </div>

    </td>       
</tr>

Comments

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.