简体   繁体   English

表单验证执行不正确

[英]Form validation executing incorrectly

My script disables the submit button for a form if the text-input is empty.如果文本输入为空,我的脚本会禁用表单的提交按钮。 I wrote a separate script that prompts a confirmation window before the user is allowed to submit updated content into the database.我编写了一个单独的脚本,在允许用户将更新的内容提交到数据库之前提示确认窗口。 Both scripts work fine individually.这两个脚本单独工作正常。

My problem arises when I try to combine both scripts using an if/else statement.当我尝试使用 if/else 语句组合两个脚本时,我的问题就出现了。 The code below disables the button as expected and inserts the user-inputted data into the database once the submit button is enabled, however the confirmation window is entirely bypassed.下面的代码按预期禁用按钮,并在启用提交按钮后将用户输入的数据插入数据库,但确认窗口被完全绕过。

How can I get the confirmation window to appear again before anything is submitted?在提交任何内容之前,如何让确认窗口再次出现?

JavaScript: JavaScript:

<script>   

function validationCheck() {
    if(document.getElementById('updateEventTitle').value==='') { 
        document.getElementById('updateBtn').disabled = true; 
    } else { 
        document.getElementById('updateBtn').disabled = false;

        //Confirmation window
       function updateEvent() {

            var r =confirm('Do you want to update this item?');
            if (r==true)    {
                window.location.href = 'server.php';
            } else {
                event.preventDefault();
            }
        }  
    }
}

</script>

HTML: HTML:

<form action='editevent.php?updaterow=$iddata' method='POST' id='updateEventTitle'>

  <input type='text' id='updateEventTitle' name='myUpdateEventTitle' size='30' maxlength='40' placeholder='$row[eventName]' onkeyup='validationCheck()' required/>

  <input class='btn btn-primary btn-sm' onclick='updateEvent()' type='submit' name='updateEventTitle' value='Update' id='updateBtn' disabled/> 

</form>

You make reference to event in your two functions, but neither of them declared the event function argument.您在两个函数中引用了event ,但它们都没有声明event函数参数。 You need to set up the event argument in your functions:您需要在函数中设置event参数:

 function validationCheck(event)
 function updateEvent(event)....

And, just nesting a function inside of another doesn't make the second one run.并且,只是在另一个函数内部嵌套一个函数并不能使第二个函数运行。 Instead, reorganize things a bit and don't set up your events with inline HTML event attributes , like onclick in the first place.相反,稍微重新组织一下, 不要使用内联 HTML 事件属性来设置您的事件,例如首先是onclick Do your JavaScript work separate from your HTML.将 JavaScript 与 HTML 分开工作。

Also, with a form, it's not the click event of the submit button that you should be handling, it's the submit event of the form .此外,对于表单,您应该处理的不是submit按钮的click事件,而是formsubmit事件。

Also (FYI), don't bother with self-terminating HTML tags .另外(仅供参考), 不要理会自我终止的 HTML 标签

Lastly, you've got your form action set to: editevent.php?updaterow=$iddata but later, if the user confirms that they want to submit, you are redirecting to: server.php .最后,您将form action设置为: editevent.php?updaterow=$iddata但稍后,如果用户确认他们要提交,您将重定向到: server.php While that redirect will now work, the data that was inputted into the form will not be sent to it.虽然该重定向现在可以工作,但输入到表单中的数据不会发送给它。 Instead, with form validation, you simply test for situations where the submit should be halted and if that scenario doesn't exist, you simply do nothing and let the form submit as it was going to.相反,通过表单验证,您只需测试应该停止提交的情况,如果这种情况不存在,您只需什么都不做,让表单按原样提交。

 // Set up your event handlers in JavaScript, not with inline HTML document.querySelector("form").addEventListener("submit", validationCheck); document.getElementById("updateEventTitle").addEventListener("keyup", validationCheck); // Get your DOM references just once, not every time the function runs let eventTitle = document.getElementById('updateEventTitle'); let btnUpdate = document.getElementById('updateBtn'); function validationCheck(event) { if(eventTitle.value==='') { btnUpdate.disabled = true; } else { btnUpdate.disabled = false; // If it was the submit button that was pressed... if(event.type === "submit"){ // Confirmation window // You only need to test for and handle situations where the form // submit event should be halted. Otherwise, let the form submit to its action URL if (!confirm('Do you want to update this item?') { event.preventDefault(); } } } }
 <form action='editevent.php?updaterow=$iddata' method='POST' id='updateEventTitle'> <input type='text' id='updateEventTitle' name='myUpdateEventTitle' size='30' maxlength='40' placeholder='$row[eventName]' required> <input class='btn btn-primary btn-sm' type='submit' name='updateEventTitle' value='Update' id='updateBtn' disabled> </form>

Is there a way to use @Scott Marcus method for multiple seperate forms on the same page, for example if I had a second additional form such as below?有没有办法在同一页面上对多个单独的表单使用@Scott Marcus 方法,例如,如果我有第二个附加表单,如下所示? Javascript only seems to allow you to pass one param at a time using .getElementById Javascript 似乎只允许您使用 .getElementById 一次传递一个参数

<form action='editevent.php?updaterow=$iddata' method='POST'     id='updateEventDate'>
<input type='text' id='updateEventDate' name='myUpdateEventDate' size='15' maxlength='10' placeholder=$eventDate required/> 
<input class='btn btn-primary btn-sm' type='submit' name='updateEventDate' value='Update' id='updateBtn' disabled> 
</form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM