传递通过 Javascript 设置的隐藏表单元素

发布于 2024-09-09 06:46:53 字数 588 浏览 7 评论 0原文

我有一个简单的 HTML 表单,其中有一些用于发送 SMS/TXT 消息的输入字段。我还有一个 jQuery 脚本,可以计算输入的字符数,并计算出发送消息将使用多少 SMS Credits。

它们出现在 div 中,如下所示:

<div id='message'> 
<span id='char'>0</span> characters, 
<span id='msgs'>0</span> SMS Credit(s)
</div>​​​​

您可以在 处看到减去大部分 php 的整个页面(只是为了简单起见) http://jsfiddle.net/gHAcM/

我想做的是包括字符数(从 0 开始)和学分数(从 0 开始)

我不确定这是否可以完成,否则如果不能,我将需要通过 php 处理页面再次计算这些值。只是试图避免做同样的事情两次,一次在 Javascript 中,然后再一次在 PHP 中。

非常感谢, 史蒂夫

I have a simple HTML Form that has some input fields for sending an SMS/TXT message. I also have a jQuery script that counts the number of characters entered and also works out how many SMS Credits will be used to send the message.

These appear in a div as follows:

<div id='message'> 
<span id='char'>0</span> characters, 
<span id='msgs'>0</span> SMS Credit(s)
</div>​​​​

You can see the whole page minus most of the php (just to keep things simple) at http://jsfiddle.net/gHAcM/

What I would like to do is include the number of characters (from 0) and the number of credits (from 0

I'm not sure if this can be done, otherwise I will need to calculate these values again via the php processing page if they can't. Just trying to avoid doing the same thing twice, once in Javascript and then again in PHP.

Many thanks,
Steve

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

┊风居住的梦幻卍 2024-09-16 06:46:53

这是可以做到的。您需要做的就是创建几个隐藏的输入元素(例如 并在计算完值后将其放入其中。然而你不应该这样做(因此没有代码 - 我不鼓励你)

无论如何,你应该总是在服务器上计算任何像这样的关键内容,因为理论上用户可以发布他们想要的任何内容,这样他们就可以改变他们的请求。说“credits = 0”,而您不想相信这一点,或者如果他们篡改了字符数,那么稍后可能会崩溃,因为它收到的输入太长......或者也许他们告诉您它有一个。负数字符...只需在服务器上重新验证即可。

It can be done. All you need to do is create a couple of hidden input elments (eg <input type="hidden" name="characterCount" /> and put the values in there when you have calculated them. However you SHOULDN'T do this (hence no code - I don't wnat to encourage you).

You should always calculate anything critical like this on the server anyway since in theory a user can post whatever they want so they could alter their request to say "credits=0" and you don't want to trust that. Or if they fiddle the character count then presumably somethign will crash later on because it receives input that is too long... Or maybe they told you it had a negative number of characters... Just revalidate on server. Its the way to go. :)

汐鸠 2024-09-16 06:46:53

您可以创建几个隐藏字段并将计数放入其中以及跨度中,然后将其与表单一起提交。然而,这个系统很容易被利用,通过伪造具有虚假字符和信用计数的http请求,因此您可能仍然希望在服务器上进行计数以避免欺诈。此外,如果用户关闭了 javascript,计数将为零。

要执行此操作,您现在已经

$chars.text(len);

添加

$("#my_hidden_char_count").val(len);

了假设您

<input id="my_hidden_char_count" value="0"></input>

的表单中已添加的内容。对学分执行同样的操作。

You can make a couple hidden fields and put the counts there as well as in the spans and then submit them with the form. However, this system can be easily gamed by forging the http request with fake character and credit count, so you may want to still do the counting on the server to avoid fraud. Also if the user has javascript turned off the count will be zero.

To do this where you now have

$chars.text(len);

add

$("#my_hidden_char_count").val(len);

which assumes you have

<input id="my_hidden_char_count" value="0"></input>

in your form. Do the same for credits.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文