dqfaom241360 2014-01-11 18:20
浏览 57
已采纳

通过MySQL动态启用和禁用DIV

Last night I was trying to figure out how I can how I can dynamically enable and disable span#txtCaptchaDiv on my contact form at the very bottom, above the submit button.

So I added a new field to MySQL, called captcha where I wanted to 1 to show and 0 to hide

So if I add 1 to field captcha the following code will show on my form.php

 <label for="code">Write code below > <span id="txtCaptchaDiv" style="color:#F00"></span><!-- this is where the script will place the generated code --> 
  <input type="hidden" id="txtCaptcha" /></label><!-- this is where the script will place a copy of the code for validation: this is a hidden field -->
  <input type="text" name="txtInput" id="txtInput" size="30" />

If I add 0 to field captcha the captcha area will be blank on my form.php.

Can you guy help me out please?

here is my index.php code I currently have:

<?php

require_once("/config/database.php");
$con = mysql_connect($config["db_server"],$config["db_user"],$config["db_pass"]);

?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Email FORM</title>
</head>

<body>


<div style="width: 550px; text-align: center;">
<span style="filter:alpha(opacity=60); opacity:.6; padding-left: 10px;"><br />


<?php 
 $data = mysql_query("SELECT * FROM formrelated") 
 or die(mysql_error()); 
 while($info = mysql_fetch_array( $data )) 
 Print " ".$info['welcomemsg'] . ""; 
 ?>

 </span></div>


<form id="form1" name="form1" method="post" action="submit.php" onsubmit="return checkform(this);">
<table width="454" border="1" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td width="123">Name</td>
    <td width="325">
      <input name="name" type="text" />
     </td>
  </tr>
  <tr>
    <td height="21">Address</td>
    <td><input name="adress" type="text" /></td>
  </tr>
  <tr>
    <td height="21">&nbsp;</td>
    <td><input name="address2" type="text" /></td>
  </tr>
  <tr>
    <td height="21">Email</td>
    <td><input name="email" type="text" /></td>
  </tr>
  <tr>
    <td height="21">Tel</td>
    <td><input name="email" type="text" /></td>
  </tr>
</table>

<!--- captcha code here--->

<center>
<table width="454" height="122" border="0" cellspacing="0" cellpadding="0" background="reCAPbg.png">
  <tr>
    <td height="73" colspan="2" align="center" valign="middle"><label for="code"><span id="txtCaptchaDiv" style="color:#333; font-size:18px;"></span><!-- this is where the script will place the generated code --> 
      <input type="hidden" id="txtCaptcha" /></label></td>
    <td width="136" rowspan="2">&nbsp;</td>
  </tr>
  <tr>
    <td width="145"> type the code here:</td>
    <td width="173" height="47" align="center"><input type="text" name="txtInput" id="txtInput" size="20" /></td>
  </tr>
</table>
</center>
<!--- captcha code ends here--->

<input name="Submit" type="button" value="submit" />
</form>


<script type="text/javascript">
//Generates the captcha function    
    var a = Math.ceil(Math.random() * 9)+ '';
    var b = Math.ceil(Math.random() * 9)+ '';       
    var c = Math.ceil(Math.random() * 9)+ '';  
    var d = Math.ceil(Math.random() * 9)+ '';  
    var e = Math.ceil(Math.random() * 9)+ '';  

    var code = a + b + c + d + e;
    document.getElementById("txtCaptcha").value = code;
    document.getElementById("txtCaptchaDiv").innerHTML = code;  
</script>




<script type="text/javascript">
function checkform(theform){
    var why = "";

    if(theform.txtInput.value == ""){
        why += "- Security code should not be empty.
";
    }
    if(theform.txtInput.value != ""){
        if(ValidCaptcha(theform.txtInput.value) == false){
            why += "- Security code did not match.
";
        }
    }
    if(why != ""){
        alert(why);
        return false;
    }
}

// Validate the Entered input aganist the generated security code function   
function ValidCaptcha(){
    var str1 = removeSpaces(document.getElementById('txtCaptcha').value);
    var str2 = removeSpaces(document.getElementById('txtInput').value);
    if (str1 == str2){
        return true;    
    }else{
        return false;
    }
}

// Remove the spaces from the entered and generated code
function removeSpaces(string){
    return string.split(' ').join('');
}

</script>

</body>
</html>
  • 写回答

4条回答 默认 最新

  • dongmou5628 2014-01-11 22:47
    关注

    This will work for you... enjoy!

        <?PHP
    
        $query = mysql_query("SELECT captcha FROM formrelated WHERE id = '1'");
    
        while ($row = mysql_fetch_assoc($query)) {
            $captchathis = $row['captcha'];
    
             if ($captchathis == "1") {
               echo "YOUR HTML CODE HERE";
            }
            else {
               echo "BLANK";
            }
        }    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料