Javascript 如何访问if语句中的变量
我有一个注册表单,在那里我用AJAX response onkeyup检查我的数据库中是否存在用户名或电子邮件allready 你可以在这里看到我是如何检查的:Javascript 如何访问if语句中的变量,javascript,php,ajax,variables,global-variables,Javascript,Php,Ajax,Variables,Global Variables,我有一个注册表单,在那里我用AJAX response onkeyup检查我的数据库中是否存在用户名或电子邮件allready 你可以在这里看到我是如何检查的: $query = $_GET ['query']; $field = $_GET ['field']; if ($field == "username") { $check = $db->prepare("SELECT * FROM users WHERE username = :query");
$query = $_GET ['query'];
$field = $_GET ['field'];
if ($field == "username")
{
$check = $db->prepare("SELECT * FROM users WHERE username = :query");
$check->bindParam(':query', $query);
$check->execute();
$count = $check->rowCount();
if ($count > 0)
{
echo '<font color=red>Username already exists</font>';
}
else
{
echo '<font color=green>Username avalable</font>';
}
}
if ($field == "email")
{
$check = $db->prepare("SELECT * FROM users WHERE email = :query");
$check->bindParam(':query', $query);
$check->execute();
$count2 = $check->rowCount();
if ($count2 > 0)
{
echo '<font color=red>Email already exists</font>';
}
else
{
echo '<font color=green>Email avalable</font>';
}
if (!filter_var($query, FILTER_VALIDATE_EMAIL)) {
echo '<font color=red><br />Please enter a valid Email</font>';
}
else {
echo '<font color=green><br />Valid email</font>';
}
}
$query=$\u GET['query'];
$field=$_GET['field'];
如果($field==“username”)
{
$check=$db->prepare(“从用户名=:查询的用户中选择*);
$check->bindParam(':query',$query);
$check->execute();
$count=$check->rowCount();
如果($count>0)
{
echo“用户名已存在”;
}
其他的
{
回显“用户名可用”;
}
}
如果($field==“email”)
{
$check=$db->prepare(“从电子邮件=:查询的用户中选择*);
$check->bindParam(':query',$query);
$check->execute();
$count2=$check->rowCount();
如果($count2>0)
{
回显“电子邮件已存在”;
}
其他的
{
回音“电子邮件可用”;
}
如果(!filter\u var($query,filter\u VALIDATE\u EMAIL)){
echo“
请输入有效的电子邮件”;
}
否则{
回显“
有效电子邮件”;
}
}
我想创建另一个if语句,如果有任何错误,那么它应该禁用submit按钮。我不想将禁用添加到我已经拥有的每个语句中,因为这样我可能会以如下情况结束:
if ($count > 0 || $count2 > 0)
{
echo '<script id="cb" type="text/javascript">document.getElementById("regr_btn").disabled=true</script>'
}
else
{
echo '<script id="cb" type="text/javascript">document.getElementById("regr_btn").disabled=false</script>'
}
我输入一封存在的电子邮件,它会禁用“提交”按钮。然后输入正确的用户名,它会再次启用提交按钮,即使电子邮件仍然错误
所以我不想创造这样的东西:
if ($count > 0 || $count2 > 0)
{
echo '<script id="cb" type="text/javascript">document.getElementById("regr_btn").disabled=true</script>'
}
else
{
echo '<script id="cb" type="text/javascript">document.getElementById("regr_btn").disabled=false</script>'
}
if($count>0 | |$count2>0)
{
echo'document.getElementById(“regr_btn”).disabled=true'
}
其他的
{
echo'document.getElementById(“regr_btn”).disabled=false'
}
但我无法通过我的计数,因为它们不是全球性的。
你们中有谁能告诉我如何才能做到这一点
别担心。我还在检查表单提交时的错误。我知道用户可以自己轻松地激活提交按钮
另外,如果我需要提供更多的代码,请告诉我
编辑:
验证功能:
<script type='text/javascript'>
function validate(field, query)
{
xmlhttp = new XMLHttpRequest(); // Creating Object
xmlhttp.onreadystatechange = function() // Checking if readyState changes
{
if (xmlhttp.readyState!=4 && xmlhttp.status==200) // Validation Under Process
{
document.getElementById(field).innerHTML = "Validating..";
}
else if (xmlhttp.readyState==4 && xmlhttp.status==200) // Validation Completed
{
document.getElementById(field).innerHTML = xmlhttp.responseText;
}
else // If an error is encountered
{
document.getElementById(field).innerHTML = "Unknown Error Occurred. <a href='index.php'>Reload</a> the page.";
}
}
xmlhttp.open("GET","check.php?field="+field+"&query="+query, false);
xmlhttp.send();
}
</script>
函数验证(字段、查询)
{
xmlhttp=newXMLHttpRequest();//正在创建对象
xmlhttp.onreadystatechange=function()//检查readyState是否更改
{
if(xmlhttp.readyState!=4&&xmlhttp.status==200)//正在进行验证
{
document.getElementById(field).innerHTML=“正在验证..”;
}
else if(xmlhttp.readyState==4&&xmlhttp.status==200)//验证已完成
{
document.getElementById(field).innerHTML=xmlhttp.responseText;
}
else//如果遇到错误
{
document.getElementById(field).innerHTML=“发生未知错误。页面。”;
}
}
open(“GET”、“check.php?field=“+field+”&query=“+query,false”);
xmlhttp.send();
}
第一个代码块中的代码在check.php文件中
我的表单中的电子邮件和用户名字段:
<div id='username'></div>
<input type="email" name="email" placeholder="Email" required="required" onkeyup="validate('email', this.value)"><br>
<div id='email'></div>
<input type="password" name="password" placeholder="Password" required="required" onkeyup="validate('password', this.value)"><br>
如果它们在同一个函数中,$count
和$count2
不需要是全局的
您不会告诉任何关于如何调用第二个代码块的信息,但最常见的是
- 作为参数传入
和$count
$count2
- 创建一个验证结果对象并传递它,其中包括该信息
$count = 0;
并在每个if条件的右括号前将$count的值重置为0。执行以下操作:
$query = $_GET ['query'];
$field = $_GET ['field'];
// Set counter to 0
$count = 0;
// Store all messages here
$msg = "";
if ($field == "username")
{
$check = $db->prepare("SELECT * FROM users WHERE username = :query");
$check->bindParam(':query', $query);
$check->execute();
if ($check->rowCount() > 0)
{
$count += 1;
$msg = '<font color=red>Username already exists</font>';
}
else
{
$msg = '<font color=green>Username avalable</font>';
}
}
if ($field == "email")
{
$check = $db->prepare("SELECT * FROM users WHERE email = :query");
$check->bindParam(':query', $query);
$check->execute();
if ($check->rowCount() > 0)
{
$count += 1;
$msg .= '<br /><font color=red>Email already exists</font>';
}
else
{
$msg .= '<br /><font color=green>Email avalable</font>';
}
if (!filter_var($query, FILTER_VALIDATE_EMAIL)) {
$msg .= '<font color=red><br />Please enter a valid Email</font>';
}
else {
$msg .= '<font color=green><br />Valid email</font>';
}
}
if ($count > 0)
{
// Disable submit button here
// Alternatively you can add the javascript that disables the submit button to
// The $msg like so: $msg .= "<script></script>"; and echo it all together.
}
echo $msg;
$query=$\u GET['query'];
$field=$_GET['field'];
//将计数器设置为0
$count=0;
//将所有消息存储在此处
$msg=”“;
如果($field==“username”)
{
$check=$db->prepare(“从用户名=:查询的用户中选择*);
$check->bindParam(':query',$query);
$check->execute();
如果($check->rowCount()>0)
{
$count+=1;
$msg='用户名已存在';
}
其他的
{
$msg='Username available';
}
}
如果($field==“email”)
{
$check=$db->prepare(“从电子邮件=:查询的用户中选择*);
$check->bindParam(':query',$query);
$check->execute();
如果($check->rowCount()>0)
{
$count+=1;
$msg.=“
电子邮件已存在”;
}
其他的
{
$msg.=“
电子邮件可用”;
}
如果(!filter\u var($query,filter\u VALIDATE\u EMAIL)){
$msg.='
请输入有效的电子邮件';
}
否则{
$msg.='
有效电子邮件';
}
}
如果($count>0)
{
//禁用此处的提交按钮
//或者,您可以将禁用submit按钮的javascript添加到
//$msg like so:$msg.='';并一起回显。
}
echo$msg;
如果有人修改了你的代码(在浏览器中运行?),他们是否有权访问你的数据库?我不确定我是否遵守了-你能详细说明一下吗?这段代码所做的只是检查我的注册表中的输入是否已经存在于数据库中。所以是的,他们可以输入大量随机电子邮件来查看它是否在数据库中,但其他的就不多了。我误解了你的代码是如何工作的。算了吧,好吧。我刚刚添加了一些代码,以便更好地理解。:-)可以但这与禁用提交按钮无关?我对答案做了一些更改。太好了!非常感谢你!