【问题标题】:Dynamically enable and disable DIV via MySQL通过 MySQL 动态启用和禁用 DIV
【发布时间】:2014-01-30 17:04:52
【问题描述】:

昨晚我试图弄清楚如何在提交按钮上方最底部的联系表单上动态启用和禁用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>

【问题讨论】:

  • 你有什么代码可以给我们看吗?还是希望我们为您编写?
  • 我添加了整个 index.php,感谢您的编辑

标签: php mysql forms dynamic


【解决方案1】:

这对你有用……享受吧!

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

【讨论】:

    【解决方案2】:

    这样试试

    <?PHP
        if($mysqlResult['captcha'] === 1)
        {
            echo $myHtml;
        }
    ?>
    

    其中$mysqlResult 是一个数组,其中包含查询结果,$mysqlResult['captcha'] 是查询中captcha 行的值,$myHtml 是您刚刚显示的 HTML 代码关于你的答案。

    祝你好运! ;)

    参考

    http://php.net/manual/en/

    编辑:

    http://www.php.net/manual/en/language.types.array.phpArray在手册上输入)

    http://www.php.net/manual/en/control-structures.if.phpIf控制结构在手册上)

    http://www.php.net/manual/en/ref.mysql.php (MySQL native 函数。已弃用。首选 MySQLi)

    http://www.php.net/manual/en/book.mysqli.phpMySQLi扩展名

    http://www.php.net/manual/en/book.pdo.php (PDO native php class)

    【讨论】:

    • 关于让 $myHtml 显示验证码的任何其他建议?
    • 基本上你要做的是一个if来检查它是否满足一些条件(在这种情况下,检查captcha是否等于1) .因此,执行以下操作的任何操作都有效:
    • 对不起,我真的很陌生,我的字段验证码在表格表单相关下,如何使用 echo 提取数据?
    • formrelated里面存储了什么?
    • 在表单相关表上,字段验证码的值为 1
    【解决方案3】:

    另一个解释 IF 逻辑基本构造的答案。

    假设我有一些条件我想做某事;在这种情况下,以下逻辑

    SHOW my form with the basic inputs
    IF condition 'captcha = 1' is met, SHOW input2 (captcha)
    SHOW rest of the HTML
    

    在 PHP 中应该是这样的

    <?PHP
      echo $myFormWithBasicInputs;
      if($captcha === 1)
      {
        echo $input2;
      }
      echo $restOfHTML;
    ?>
    

    在您的情况下,$myFormWithBasicInput$restOfHTML 已作为 HTML 输出。您要做的就是在其中注入一个 PHP 代码来检查某些条件是否匹配。会是这样的

    <html>
      <!-- MY FORM WITH BASIC INPUTS -->
      <?PHP
        $captcha = $mySQLresult['captchaRow'];
        if($captcha === 1)
        {
      ?>
        <!-- CAPTCHA INPUT HERE -->
      <?PHP
        }
      ?>
      <!-- REST OF HTML -->
    </html>
    

    请注意,这是示例代码的解决方法。

    【讨论】:

    • 我在 mySQl 中添加了带有验证码的代码,并向验证码添加了 1 个值。它没有返回任何东西?
    • 我代码中的 gray HTML 是 cmets。放置您的 HTML 代码并进行测试;)。顺便说一句,请注意,这只是示例代码,毕竟你不应该使用它
    【解决方案4】:
        <?PHP
        $mysql_query = "SELECT captcha FROM formrelated";
        $captcha = $mySQLresult['captchaRow'];
        if($captcha === 1)
        {
      ?>
    
    <!--- CODE---->
    <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>
    
    
      <?PHP
        }
      ?>
      <!-- REST OF HTML -->
    

    【讨论】:

    • 几乎就是这样。但是您忘记执行 $mysql_query 并将输出 保存到$mySQLresult 变量中。顺便说一句,这根本不是答案:P
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多