php - 通过 MySQL 动态启用和禁用 DIV

标签 php mysql forms dynamic

昨晚,我试图弄清楚如何在提交按钮上方、最底部的联系表单上动态启用和禁用 span#txtCaptchaDiv

因此,我向 MySQL 添加了一个名为 captcha 的新字段,我希望其中 1 显示,0 隐藏

因此,如果我将 1 添加到字段 captcha,以下代码将显示在我的 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" />

如果我将 0 添加到字段 captcha,则我的 form.php 上的验证码区域将为空白。

你能帮我一下吗?

这是我当前拥有的index.php代码:

<?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.\n";
    }
    if(theform.txtInput.value != ""){
        if(ValidCaptcha(theform.txtInput.value) == false){
            why += "- Security code did not match.\n";
        }
    }
    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>

最佳答案

这对你有用......享受吧!

    <?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";
        }
    }    
?>

关于php - 通过 MySQL 动态启用和禁用 DIV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21066095/

相关文章:

Javascript数组没有完全分配给php数组

php - 一个文件中可以同时理解多种语言概念的Web开发工具

javascript - Bootstrap 模态弹出窗口在 PHP 页面上不起作用 |控制台没有显示错误

javascript - 如何将 PHP 的输出显示为 HTML

javascript - 如何在antd中获取FormItem更改时的字段值

javascript - 在 AngularJs 中验证后隐藏表单

php - Laravel 4.1 _token 表单提交错误

php - DVWA 1.9 设置错误

mysql - 从三个表中选择的数据会重复出现

php - 计算一个月中每个工作日的数量