php - 将 mysql_query 添加到 onclick 函数中

标签 php javascript html mysql

我想让它只有在前一个框中的信息正确时才会出现新的输入框,通过用 mysql_query 检查它我可以修改这段代码吗

function show(id){
  if(document.getElementById(id+"-link").style.display=="none") {
     document.getElementById(id+"-link").style.display="block";
     document.getElementById(id+"-input").style.display="block";
  }
}

<tr> 
  <td><input type="text" id="a1" name="em_1"></td>
  <td><a href="#null" onclick="show('b1')">and</a></td>
</tr><tr>
  <td><input type="text" id="b1-input" name="em_2" style="display:none"></td>
  <td><a href="#null" style="display:none" id="b1-link"</a></td>
</tr><tr>

所以我需要在这里的某个地方添加:

$userCheck=mysql_query(SELECT email FROM users WHERE name = "em_1")
$row=mysql_num_rows($usercheck)
if($row!==0){"show('b1')";}

或有影响的东西,无需刷新页面。这可能吗?

最佳答案

Ajax :

function XMLDoc()
{
if (window.XMLHttpRequest)
  {
    xmlhttp=new XMLHttpRequest();
  }
else
  {
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
    {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            // xmlhttp.responseText -> return value from php file , so from here you can do your action
        }
    };
xmlhttp.open("POST","yourfile.php?email={value}",true); // from here send value to check in server side
xmlhttp.send(); 
}

PHP:

if (isset($_POST['email'])){ // which is variable from `yourfile.php?email`
    $userCheck=mysql_query("SELECT email FROM users WHERE name = 'em_1'");
    $row=mysql_num_rows($usercheck);
    if ($row==0){
        echo "no";
    } else {
        echo "yes";
    }
}

传入检查电子邮件是否存在的服务器端脚本的 yourfile URL,并返回例如是或否 => 然后在 Ajax 的帮助下,它将在当前文档中返回

注意:不要使用mysql扩展,使用mysqliPDO

关于php - 将 mysql_query 添加到 onclick 函数中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14224540/

相关文章:

javascript - 知道 Angular 是否为北、南、西或东的最快代码

javascript - 让 React 开发人员工具与 Webpack 一起工作

php - 使用表格和 while 循环在大图像下显示小图像

MYSQL 40 兼容模式下的 phpMyAdmin 错误 #1064

php - 在后端订单版中 Hook woocommerce 价格

php - 下拉列表中的选定值不应重复

php - 无法在 paypal 快速结帐时传递负值

javascript - 为什么在使用 redux 和 react.js 时我的 Prop 是 `undefined`?

html - 使用 CSS 选择器选择最后一个元素

java - 使用 apache commons 从 html 表单中解析列表框值