简体   繁体   English

在POST之前,如何使用Javascript(或Rails)重新格式化表单输入?

[英]How can I reformat form input before POST using Javascript (or Rails)?

I have a field in my form labeled "Name" that will contain both the First & Last name. 我的表单中有一个标记为“名称”的字段,其中将包含名字和姓氏。

Our existing dynamic server (to which the form is being POSTed to), expects two separate fields (first name, last name). 我们现有的动态服务器(将表单过帐到该动态服务器)需要两个单独的字段(名字,姓氏)。

Can I use Javascript to split the user input into two separate variables before the form is posted to the server? 我可以在表单发布到服务器之前使用Javascript将用户输入分为两个单独的变量吗? How would I do this? 我该怎么做?

You should not rely on client side parsing whenever possible. 您不应尽可能依赖客户端解析。 If you are sending this form to an app you can't modify, use the Javascript method mentioned above because you have no control over it (but then why not just have a first and last name field). 如果您将此表格发送到您无法修改的应用程序,请使用上述Javascript方法,因为您无法控制它(但为什么不只有名字和姓氏字段)。 But if you are controller the backend app, perform all your massaging and data validation there. 但是,如果您是后端应用程序的控制者,请在那里执行所有按摩和数据验证。

Javascript should only be used to enhance the UI experience, not perform import data manipulation, it can be both a security hole and a point of failure if use Javascript for these important tasks. 仅应使用Javascript来增强UI体验,而不应执行导入数据操作,如果将Javascript用于这些重要任务,则可能既是安全漏洞,也是失败点。

Also, when manipulating names, keep in mind all the different kinds of formats you will get, such as: 另外,在处理名称时,请记住您将获得的所有不同种类的格式,例如:

John Smith Jr
Dr John Smith
John Smith Esq.
John Smith IV
John A Smith

So be careful, massaging names is very messy business and the public will enter whatever they want, at the very least, add a small label and ask them to only enter "first and last name" and pray for the best. 因此要小心,按摩名字是一件很麻烦的事情,公众至少会输入他们想要的名字,并添加一个小标签,并要求他们仅输入“名字和姓氏”并祈求最好的名字。

For me, the reason for having a single field is for form simplicity, which will get higher conversions. 对我来说,具有单个字段的原因是为了简化表单,这将获得更高的转换率。

Getting PHP to split name into firstname and lastname, then getting PHP to resubmit the form to another server might be a bit tricky... 让PHP将名称拆分为名字和姓氏,然后让PHP将表单重新提交给另一台服务器可能有点棘手...

You can do something like this: 您可以执行以下操作:

var yourForm = document.getElementById('yourFormId');
yourform.onsubmit = new function()
{
    var nameBox = document.getElementById('nameBox');
    var fname = document.createElement('INPUT');
    var lname = document.createElement('INPUT');
    fname.type = 'HIDDEN';
    lname.type = 'HIDDEN';
    fname.name = 'fname';
    lname.name = 'lname';

    var tokens = nameBox.value.split(' ');

    // Note there are a million ways to break this parser, demonstration only
    fname.value = tokens[0];
    lname.value = tokens[1];

    yourForm.appendChild(fname);
    yourForm.appendChild(lname);
}

If it needs to be handled on client-side, Use the Javascript split method to parse the name field value. 如果需要在客户端进行处理,请使用Javascript split方法解析名称字段值。 Then append the two names to the form's url or create a couple of hidden fields. 然后将这两个名称附加到表单的url或创建几个隐藏字段。

I would process this on the server end to make sure the data that is passed is accurate from what was posted. 我将在服务器端进行处理,以确保所传递的数据与发布的数据准确无误。 It's relatively easy programmatically to split the name, but the problem is how do you define your separator. 以编程方式分割名称相对容易,但问题是如何定义分隔符。 Most would agree to split it wherever there is a white-space character, but what if they enter a three word name such as Don Juan DiMarco ? 大多数人都同意在有空格字符的地方将其拆分,但是如果他们输入三个单词的名称(例如Don Juan DiMarco)怎么办? If you decide to split the name based on whitespace, you'll have to determine how to assign the first and last names. 如果决定根据空格拆分名称,则必须确定如何分配名字和姓氏。

$arrNames = preg_split('/\s+/', $_POST['name']);

The above will give you (in PHP) an array of values split by white space. 上面的代码将给您(在PHP中)一个由空格分隔的值数组。 Running that on the string Don Juan DiMarco would give you an array like: 在字符串Don Juan DiMarco上运行该字符串,将得到一个类似以下的数组:

Array([0] => "Don", [1] => "Juan", [2] => "DiMarco")

From there you have to determine which ones are the first name, and which are a middle, and which are a last name. 从那里,您必须确定哪些是名字,哪些是中间名,哪些是姓氏。 It gets even harder if you have 4 or 5 names entered, which is entirely realistic for certain cultures. 如果输入4或5个名称,难度会更大,这对于某些文化完全是现实的。 All of this is guesswork, which is why I would recommend simply adding a Last Name input field on the front-end. 所有这些都是猜测,这就是为什么我建议在前端简单地添加一个姓氏输入字段的原因。 This would eliminate all guess work as to which name is the first name and which is the last name. 这将消除所有关于哪个名称是名字和哪个姓氏的猜测工作。

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

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