【问题标题】:javascript should return false, form still validates and php inserts data in tablejavascript应该返回false,表单仍然有效并且php在表中插入数据
【发布时间】:2013-02-16 22:00:21
【问题描述】:

您好,非常感谢所有阅读和回答这篇文章的人。 首先,如果这个问题已经得到解答,我感到非常抱歉,但我正在整个网络上寻找这个问题的答案,比如 12 小时,但我没有发现任何问题。 所以我的问题如下:我有一个表单、一个脚本和一个 php 部分。由于该网页将有许多表单页面,因此我尝试编写脚本,以便直接从字段中获取需要验证的数据。因此该函数有一个参数。现在,提交按钮被隐藏,我在最后一个字段后使用回车来自动提交表单。即使 javascript 有效(它会在空字段周围创建一个红色边框),当我按下输入时,表单仍然通过 php 提交。在我的新手中,我认为问题在于脚本没有返回 true 或 false,或者 onSubmit 有错误,因此它没有通过 true 或 false。我在下面发布代码。 非常感谢,再次!

对于表单部分:

        <form action="addUser.php" method="post" name="login" onsubmit="return checkField()">

          <table border="0" cellpadding="0" cellspacing="0" class="addUser">
            <tr>
              <td><p class="capRegister">Date de utilizator</p></td>
              <td><p class="capRegister">Date de identificare</p></td>
            </tr>
            <tr>
              <td><input class="input1" name="user" type="text" placeholder="alege-ti un nume de utilizator" tabindex="1" onblur="checkField(name)" value="<?php echo $user; ?>"/></td>
              <td><input class="input1" name="nume" type="text" placeholder="numele tau" tabindex="4" onblur="checkField(name)" value="<?php echo $nume ?>" /></td>
            </tr>
            <tr>
              <td><input class="input1" name="password" type="password" placeholder="alege-ti o parola" tabindex="2" onblur="checkField(name)" /></td>
              <td><input class="input1" name="prenume" type="text" placeholder="prenumele tau" tabindex="5" onblur="checkField(name)" value="<?php echo $prenume; ?>"/></td>
            </tr>
            <tr>
              <td><input class="input1" name="confirma" type="password" placeholder="confirma parola" tabindex="3" onblur="checkField(name)" /></td>
              <td><input class="input1" name="interior" type="tel" placeholder="interior" tabindex="6" onblur="checkField(name)" value="<?php echo $interior ?>"/></td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td><input class="input1" name="mobil" type="tel" placeholder="mobil" tabindex="7" onblur="checkField(name)" value="<?php echo $mobil; ?>"/></td>
            </tr>
          </table>
          <p><input class="submitButton" name="login2" type="submit" value="" /></p>

  </form>

现在是脚本部分:

function checkField(x) {

//initializeaza o variabila care pe baza argumentului functiei verifica campurile dupa nume (argumentul contine numele campului)
var continutCamp=document.getElementsByName(x)[0].value;
//variabila pentru footer
var msgJos;
//variabila pentru parola
var tempParola=document.getElementsByName('password')[0].value; 

msgJos="";
document.getElementById('bottomBar').innerHTML=msgJos;
document.getElementsByName(x)[0].style.borderStyle='none';

//verifica daca campul este gol     
if (continutCamp==null || continutCamp=="")
    {   
    // inconjoara cu o margine rosie eroarea
    document.getElementsByName(x)[0].style.borderColor='#FF0000';
    document.getElementsByName(x)[0].style.borderStyle='solid';
    document.getElementsByName(x)[0].style.borderRadius='4px';

    // scrie un mesaj in campul de jos

    msgJos='<p id="footerInfo">nu ai completat campul '+ x +'</p>';
    document.getElementById('bottomBar').innerHTML=msgJos;

    //return false pentru a impiedica procesarea formularului
    return false;

    }

        //daca nu se potriveste parola cu confirmarea
if((name=="confirma")&&(continutCamp!=tempParola))
{
    //coloreaza ambele campuri de parola si confirmare cu rosu
    document.getElementsByName('confirma')[0].style.borderColor='#FF0000';
    document.getElementsByName('confirma')[0].style.borderStyle='solid';
    document.getElementsByName('confirma')[0].style.borderRadius='4px';
    document.getElementsByName('password')[0].style.borderColor='#FF0000';
    document.getElementsByName('password')[0].style.borderStyle='solid';
    document.getElementsByName('password')[0].style.borderRadius='4px';

    //cod ca sa schimbe continutul footerului cu mesajul potrivit
    msgJos='<p id="footerInfo">parolele nu se potrivesc</p>';
    document.getElementById('bottomBar').innerHTML=msgJos;

    //din nou return false ca sa nu se proceseze formularul daca parola nu e aceeasi
return false;
}


}

最后是 php 部分:

<?php
require_once("localVars.php");

$footerError;

if(isset($_POST['user']))
{

// $dbc=mysqli_connect(USER,PASS,BAZA) or die ('<p id="footerInfo"> Eroare in timp ce ma conectam </p>');

$user=$_POST['user'];
$parola=$_POST['password'];
$nume=$_POST['nume'];
$prenume=$_POST['prenume'];
$interior=$_POST['interior'];
$mobil=$_POST['mobil'];

$query="INSERT INTO users (user, parola, nume, prenume, interior, mobil) VALUES ('$user','$parola','$nume','$prenume','$interior','$mobil')"; 

// $result=mysqli_query($dbc, $query) or die ('<p id="footerInfo"> Eroare in timp ce bagam datele </p>');

mysqli_close($dbc); 
}

else{

    $footerError='nu sunt date de introdus';
}

?>

为了不在数据库中发送十亿次尝试,php 上有 cmets。 $_POST 检查用户,但之前确实检查过提交或任何其他字段名称。

【问题讨论】:

  • 您的checkField() 方法需要一个参数x,但您没有在onsubmit 中传递任何内容。我会从那里开始。

标签: php javascript forms validation


【解决方案1】:

您的函数checkField(x) 需要一个变量“x”,但您的 onSubmit 侦听器只有“return checkField()”,它缺少预期的变量。这是一个 javascript 错误,由于它无法评估并向表单返回 true/false,因此提交了表单。

如果您还没有它,请下载 firebug (getfirebug.com) 并将其安装在您的浏览器中,它将使调试这些类型的 javascript 错误变得非常容易。

【讨论】:

  • 感谢大家的快速回复。但是,我曾经在那里争论过。当它完成时,它的写法类似于 checkField(name)。即使正确填写了所有字段,它也没有验证表格。这就是为什么我从那里删除了论点。可能是参数不正确,但我不知道在那里调用什么参数以使其返回 false 或 true
  • 我们在这里谈论两个不同的事情。一个是逻辑问题,另一个是javascript语法问题。如果checkField 函数需要一个变量(不管它叫什么),则必须将一个变量传递给它,否则它会出错。
  • 我在那里尝试的任何参数,除了实际名称之外,都以验证表单结尾。如果我写名字,它在任何情况下都不会验证表格。所以有些事情是非常错误的
  • @RazvanSoneriu 这就是硬币的另一面。您尝试验证此表单的方式存在缺陷。一旦你修复了 javascript 语法,你仍然需要编写代码以便它在逻辑上工作。
  • 好吧,我认为我的逻辑很好。我想要完成的只是从字段本身获取字段的名称,而不是为每个字段编写案例,因为 web 应用程序将有很多字段要检查,我需要适用于所有情况的东西。
【解决方案2】:

您的主要问题似乎是您在没有这样的变量时使用了全局变量“名称”。因此,您的 onblur 事件不会正确调用该函数。 name 不是 this 的同义词。如果您想获取触发事件的元素的名称,则必须将 name 替换为 this.name

<input class="input1" name="password" type="password" placeholder="alege-ti o parola" tabindex="2" onblur="checkField(this.name)" />

更新:

另一个问题是您再次检查变量name,但该变量不存在。在此处将name 替换为x

if((name=="confirma")&&(continutCamp!=tempParola))

看起来像

if((x=="confirma")&&(continutCamp!=tempParola))

应该是这样,我在编辑这个答案之前测试了它。

【讨论】:

  • 我一定会试试这个!非常感谢,很快就会回复
  • 这仍然不起作用。所以问题是如果函数自动获取字段名称(因此普遍适用于任何字段)当名称登录时它应该做什么,以便它返回true或false。
  • 非常感谢!但我实际上创建了一个在提交表单时调用的新函数,它检查是否所有字段都已填写。
猜你喜欢
  • 1970-01-01
  • 2021-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-12
  • 2015-04-17
相关资源
最近更新 更多