简体   繁体   English

通过javascript提交表单

[英]Submitting form via javascript

I have a page where the user gets directed to if they want to edit some text which was on the previous screen. 我有一个页面,如果用户想要编辑上一个屏幕上的某些文本,用户可以定向该页面。 Once they have finished editing the form data it gets submitted for validation to javascript, this is the form: 一旦他们完成了对表单数据的编辑,就会将其提交给javascript进行验证,这是表单:

<form id='editpost' action='' method='post' >
Your Post:<br/>
<input type='hidden' name='' value='""'>
Title: <input type='text' name='' value='""'/><br/>
<textarea name='' cols='100' rows='100'>""</textarea>
<br/><input type=submit value='Submit' name='' onclick='return validate(document.forms[0]);'><input type=submit value='Cancel' name=''> 
</form>
}

I check for values being entered in the appropriate boxes by using javascript: 我使用javascript检查在相应的框中输入的值:

<script type="text/javascript">
function validate(F) {      
    var post = F;
    var title = F;
    post = post.trim();
    title = title.trim();
    if(post.length == 0) {
        alert("This is blank");
        return false;
    }
    else {
        if(title.length == 0) {
            var agree=confirm("Really?");
            if (agree) {
                return true;
            }
            else {
                return false;
            }
        }
        return true;
    }
}

When the form is eventually submitted some data gets updated in a mysql database with the data from the form and the user gets redirected to the page to view it. 当表单最终提交时,一些数据在mysql数据库中使用表单中的数据进行更新,用户将被重定向到页面以查看它。 What I want to be able to do is submit the form via a post method in javascript, this is to stop the user turning their javascript off and potentially enter blank bodys into the db. 我希望能够做的是通过javascript中的post方法提交表单,这是为了阻止用户关闭他们的javascript并可能将空白的bodys输入数据库。 I'm really not sure how to do this though. 我真的不知道该怎么做。

your submit button on the page should be a dummy button that only calls your validation function. 页面上的提交按钮应该是一个只调用验证功能的虚拟按钮。 Then to submit just say document.getElementById('editpost').submit(); 然后提交just say document.getElementById('editpost')。submit(); from your code. 从你的代码。 (if my memory fails me you might try capitalizing that S on submit) (如果我的记忆失败了,你可以试着在提交时将S大写)

Hope this helps. 希望这可以帮助。

edit: 编辑:

<form id='editpost' action='editPost.php' method='post' >
...
<br/><input type=button value='Submit' name='alterPost' onclick='validateForm(document.forms[0]);'><input type=submit value='Cancel' name='no'> 
</form>

then in your javascript, instead of returning a value 然后在你的javascript中,而不是返回一个值

...
// if it's valid
F.submit();
...
// if it's not valid
// show the user a message explaining why
...

What I want to be able to do is submit the form via a post method in javascript, this is to stop the user turning their javascript off and potentially enter blank 我想要做的是通过javascript中的post方法提交表单,这是为了阻止用户关闭他们的javascript并可能输入空白

What you actually need is server side (not client side/Javascript) validation in order to prevent "invalid data" (however you define it) from being sent/saved to your database. 您实际需要的是服务器端 (而不是客户端/ Javascript)验证,以防止“无效数据”(无论您定义它)​​被发送/保存到您的数据库。

If Javascript is turned off on the client (browser) none of your Javascript will run, the form will POST (or GET) to whatever action you define (editPost.php). 如果关闭了JavaScript客户端(浏览器) 不关你的Javascript运行,表单将POST(或GET),以任何行动定义(editPost.php)。 You need to add validation code/handling to editPost.php 您需要向editPost.php添加验证代码/处理

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

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