在输入上使用jquery val()后,PHP $ _POST为空 [英] PHP $_POST is empty after using jquery val() on input

查看:163
本文介绍了在输入上使用jquery val()后,PHP $ _POST为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查出生年份的输入值.我已经弄清楚了,如果我使用jquery函数val()来获取此输入的值,那么此输入的php帖子将为空.不使用val()就可以正常工作.在这里,您可以看到代码的重要部分.这不是那么重要的形式能力,但是检查表单是否写得不合适或有误会很有用.

I want to check value of input for year of birth. I have figured out that if I use jquery function val() to get value of this input, so the php post of this input will be empty. It works fine without using val(). Here you can see the important part part of code. It is not so essential ability of form, but it would be useful to check if the isn't written anything inappropriate or any mistype.

感谢您的帮助.

<?php
if (!empty($_POST['rn']) && isset($_POST['rn'])){
    // proceed
} else {
    echo "You haven't filled all inputs";
}
?>
<script>
$("#kontrolaButton").click(function(){
    var rok = parseInt($("#rok_nar").val(),10);
    if (rok > 1900 && rok < 2016) {
        $("#odeslatButton").trigger('click');
    };
});
</script>
<form accept-charset="utf-8" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" class="send_form">

<input class="bold" id="rok_nar" type="text" name="rn" placeholder="Rok narození"/>
<input type="button" value="Odeslat" name="kontrola" id="kontrolaButton" /> 
<input type="submit" value="Odeslat" name="odeslat" id="odeslatButton" style="visibility: hidden;" />

</form>

更新: 我可能已经发现了问题.我没有在这里写它,因为我不认为这是一个问题.我的脚本如下:

Update: I have probably found the problem. I didn't write it here, because I didn't consider it as an issue. My script looks like:

$("#kontrolaButton").click(function(){
    var rok = parseInt($("#rok_nar").val(),10);
    alert(rok);
    if (rok > 1900 && rok < 2016) {
        $("#odeslatButton").trigger('click');
    };
});

删除警报后,代码可以正常工作:/有趣的是...

After erasing the alert, the code works :/ Interesting...

推荐答案

您应该将脚本右移至HTML主体的末尾,以便在 kontrolaButton 元素位于DOM中时执行该脚本.

You should move the script right to the end of the HTML body so that it executes when the kontrolaButton element is in the DOM.

请注意,您为关闭alert(rok);而发出的向下/向上鼠标或按键/释放键可能会干扰表单的提交.例如,我可以重现这样的问题:我按了ESC键以关闭警报,尽管表单提交了数据,但下一页没有呈现.那只是一个空白页.这可能与浏览器密切相关,但是在我的设置中,ESC键显然会中断页面​​的呈现.

Note that the mouse down/up or key press/release you issue to close the alert(rok); may interfere with the submission of the form. For instance, I could reproduce a problem like this: I pressed the ESC key to close the alert, and although the form submitted its data, the next page did not render; it was just a blank page. This may be very browser dependent, but on my set-up the ESC key apparently interrupts the rendering of a page.

可能还会有其他副作用,当然,如果页面上有其他Java脚本代码可以响应这些键或鼠标事件,那么当然也可以.

There may be other such side effects, certainly also if you have other Javascript code on the page that responds to these key or mouse events.

这篇关于在输入上使用jquery val()后,PHP $ _POST为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆