我有一个表单,提交时执行客户端的javascript代码,如下
<form class="test" onsubmit="return submitChange();">
<input class="q-input" id="txt1" type="text"
.....但我同时想把id="txt1"的文本框内容通过php发送到服务端,该怎么弄?我试过了在onsubmit前面加个action="send.php"类似的东西,但是没成功:(
<form class="test" onsubmit="return submitChange();">
<input class="q-input" id="txt1" type="text"
.....但我同时想把id="txt1"的文本框内容通过php发送到服务端,该怎么弄?我试过了在onsubmit前面加个action="send.php"类似的东西,但是没成功:(
如果在执行客户端javascript代码的同时,进行服务端操作,可以考虑用AJAX来实现一下PS: 为何要如此呢?
当然有用:可以在客户端进行一些验证,例如是否是合格的email格式,用户名中是否包含非法字符等。@LZ你提到的问题,最简单的方法是在你的JS脚本中再次调用诸如document.form.submit()这样的方法,同时指定form的action,应该是可以解决的。
action="send.php"
然后在send.php里接收$a = $_POST["name"];
<form class="test" action='send.php' method='POST' onsubmit="return submitChange();">
<input class="q-input" id="txt1" type="text" name='txt1'/>
<input type='submit' name='submit' value='submit'/>
</form>form运行机制如下:
1 当点击type='submit'的button时,会触发表单提交.
2 表单提交前,会先执行onsubmit
3 如上所示,如果submitChange()函数返回true,则表单正常提交到send.php里.
send.php可以根据form的method来决定获取表单内元素的方式.
如上所示, 可以通过$_POST['txt1']来获取,注意,这个txt1必须为name才生效.
4 如果submitChange()函数返回false,本次表单提交失败.