duan1226 2014-05-24 11:17 采纳率: 100%
浏览 35
已采纳

在php中使用mysql枚举和javascript显示和隐藏元素?

I am trying to simply show and hide an element depending on the value of an ENUM in mysql database.

I am using the javascript and PHP but it seems like the javascript within PHP does not or cannot select element given as the element is always on display!

here is my php code:

if ($sizeSelect != '1') {

echo '<script type="text/javascript">';
echo 'document.getElementById("sizeSelect").style.display = "block"';
echo '</script>';
}else{
echo '<script type="text/javascript">';
echo 'document.getElementById("sizeSelect").style.display = "none"';
echo '</script>';
}

and this is my HTML element:

<select id="sizeSelect" name="sizeSelect">
            <option>Small</option>
            <option>Large</option>
            </select>

is there anything that I'm missing?

any help would be appreciated.

Thanks

P.S. I have made sure that I am connected to mysql database and get the ENUM value properly so the mysql connection is not an issue here.

okay, I just did a test within my html file and placed the following code at the top of the page and still didn't work but when I put the same code at the bottom of the page, it did work and changed the element's display to none:

 <script language='javascript' type='text/javascript'>
document.getElementById('sizeSelect').style.display = 'none';
</script>

SO, i did try this code in my PHP file with document ready function but still doesn't work from php file!

if ($sizeSelect = 1) {

echo "<script language='javascript' type='text/javascript'>";
echo
"$(document).ready(function(){ document.getElementById('sizeSelect').style.display = 'block';});";
echo "</script>";
}else{
echo "<script language='javascript' type='text/javascript'>";
echo "document.getElementById('sizeSelect').style.display = 'none';";
echo "</script>";
} 

any help would be great.

  • 写回答

3条回答 默认 最新

  • dtp19819 2014-05-24 11:24
    关注

    Try to put your php code that generating the javascript after your SELECT tag or at the end of file, because your javascript doesn't find the DOM element "#sizeSelect", i made test and it worked for me. like this:

    <select id="sizeSelect" name="sizeSelect">
        <option>Small</option>
        <option>Large</option>
    </select>
    
    <?php 
    
        $sizeSelect = // retrieve sizeSelect value from your database;
        if ($sizeSelect != '1') {
    
            echo '<script type="text/javascript">';
            echo 'document.getElementById("sizeSelect").style.display = "block"';
            echo '</script>';
        }else{
            echo '<script type="text/javascript">';
            echo 'document.getElementById("sizeSelect").style.display = "none"';
            echo '</script>';
        }
    ?>
    

    After you specified that you're using Smarty, i come back with a solution for that:

    1. Concatenate your js content produced in php file on a string variable instead of using echo, and assign its value with Smarty assign() function

       $jsContent= '';
       if ($sizeSelect != '1') {
           $jsContent.= '<script type="text/javascript">';
           $jsContent.=  'document.getElementById("sizeSelect").style.display = "block"';
           $jsContent.=  '</script>';
       }
       else{
           $jsContent.=  '<script type="text/javascript">';
           $jsContent.=  'document.getElementById("sizeSelect").style.display = "none"';
           $jsContent.=  '</script>';
       }
       $smarty->assign('js', $jsContent);
      
    2. Display the variable content as i said before, just after the select tag being targeted

       <select id="sizeSelect" name="sizeSelect">
           <option>Small</option>
           <option>Large</option>
       </select>
       {$js}
      
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#线性回归#的问题:【统计】回归系数要转化为相关系数才能进行Fisher' Z转化吗(相关搜索:回归模型)
  • ¥100 使用matlab解决含分段变量的优化问题
  • ¥15 matlab基于鲸鱼算法优化vmd
  • ¥20 PCB设计与制作问答
  • ¥20 课堂跑如何下载ppt
  • ¥15 有没有哥看着画下电路
  • ¥15 STM32阿里云平台显示不了传感器采集的数据
  • ¥20 三菱FX系列PLC上位机串口下指令置位M64和M65,这两条指令分别是什么呢?
  • ¥15 有关结冰模拟程序咨询
  • ¥15 ubuntu服务器配置dns域名后无法访问其他域名