Doesn't work the code javascript inside echo in PHP

i’ve tried many times to solve this problem, when the option “OTROS” is selected it must show the div#contenedoropots but it doesn’t work the script inside of echo PHP.

PHP

<option id="copiado"
  <?php 
	if($columna['tipo_defecto'] == "OTROS"){ 							
		echo 'selected ';
		echo "<script type='text/javascript'>";
		echo "document.getElementById('contenedoropots').style.display = 'block';";
		echo "</script>";									
	} 
?> 					
		>OTROS</option>

HTML

<div id="contenedoropots" style="display:none">
	 <label id="labelcopiarA" for="cpOtros">Especificar:</label>
	 <input id="copiar" type="text" name="cpOtros"  onkeyup="copiarValue();" value="<?=$columna['especificar']?>"/>
</div>

The result with this code is in dropdown list:

document.getElementById('contenedoropots').style.display = 'block';>OTROS

How i can get the script work in echo PHP

Post the way the source code looks in the browser (after the PHP has run).

You are outputting the JavaScript code inside the tag. That obviously won’t work and is invalid.

You will need to duplicate the logic you have to write the JavaScript outside of the tag, while writing the selected output inside the tag.

1 Like

I found my error, i have to do twice the script but the first i have to confirm the condition and then after put the code HTML and after put again other code in PHP with javascript for show me the div#contenedoropots

That’s full code:

<select name="defecto" onchange="mostrarOtros(this);">
<option id="copiado"
	<?php 
		if($columna['tipo_defecto'] == "OTROS"){ 							
			echo 'selected ="selected" ';													
		} 
	 ?>				 					
	  >OTROS</option>
							
		</select>							
		<br />		
<div id="contenedoropots" style="display:none">
	   <label id="labelcopiarA" for="cpOtros">Especificar:</label>
	   <input id="copiar" type="text" name="cpOtros"  onkeyup="copiarValue();" value="<?=$columna['especificar']?>"/>
</div>
		    <?php
			    if($columna['tipo_defecto'] == "OTROS"){                            
			        echo "<script type='text/javascript'>";
			        echo "document.getElementById('contenedoropots').style.display = 'block';";
			        echo "</script>";                                   
			    } 
			?> 

Thank you for everyone guys

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.