Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/429.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 奇怪的javascript表单行为_Php_Javascript_Html_Ajax - Fatal编程技术网

Php 奇怪的javascript表单行为

Php 奇怪的javascript表单行为,php,javascript,html,ajax,Php,Javascript,Html,Ajax,我使用此php生成以下html表单: echo "<form name=\"userForm\"> Username: <input type=\"text\" name=\"username\" /> <br /> First name: <input type=\"text\" name=\"firstname\" /> <br /> Last name: <input type=\"text\" name=\"lastnam

我使用此php生成以下html表单:

echo "<form name=\"userForm\">
Username:
<input type=\"text\" name=\"username\" />
<br />
First name:
<input type=\"text\" name=\"firstname\" />
<br />
Last name:
<input type=\"text\" name=\"lastname\" />
<br />
<input name=\"Submit\" type=\"submit\" value=\"Update\" onclick=\"submitUserInfo();return false\"/>
</form>";
我很清楚,url定义为以开头

edit_user.php?cmd=submitinfo&username 
但是,当按下submit按钮时,它会尝试按以下方式发送url:

edit_user.php?username=

我不知道为什么。我已经成功地将上述技术用于我网站上的其他表单,并且找不到任何理由将cmd=submitinfo&排除在外。

使用类似Firebug的工具来确定浏览器是否在没有cmd=submitinfo的情况下发送它,或者它是否在服务器端丢失。

更改:

url = "edit_user.php?cmd=submitinfo&username="+document.userForm.username.value+"&firstname="+document.userForm.firstname.value+"&lastname="+document.userForm.lastname.value;
致:

您必须先声明
url
变量,然后才能尝试使用它

另外,避免将HTML直接回送到页面。调试、更新和样式化将是一件非常痛苦的事情。

对于这样简单的任务,像这样的框架并不是真正需要的,尽管我建议您考虑一下是否计划在您的站点上实现更多JavaScript

使用jQuery的代码如下所示:

$("#<id of form goes here>").submit(function ()
{
    $.ajax({
        data: {
            "username": $("#<id of username-input goes here>").val(),
            "firstname": $("#<id of firstname-input goes here>").val(),
            "lastname": $("#<id of lastname-input goes here>").val()
        },

        url: "edit_user.php"
    });

    return false;
});
$(“#”)提交(函数()
{
$.ajax({
数据:{
“用户名”:$(“#”)val(),
“firstname”:$(“#”)val(),
“lastname”:$(“#”)val()
},
url:“edit_user.php”
});
返回false;
});
看起来好多了,我想:)



也;使用框架(特别是jQuery)可以解决浏览器的问题。。所以你不必。。照顾好他们,就是:)

我想你需要把按钮的类型从“提交”改为“按钮”:



显示url是否有助于查看我是否将其发送到客户端?将其显示在何处?您需要从问题的根源开始,看看它是否通过了从浏览器发送的第一个位置。GetXmlHttpObject实现是什么?对responseText=='true'的检查是什么?如果您尝试将url设置为本地(var url=“…”;)会发生什么情况?如果在构建url之后立即显示它,会发生什么情况?如果在调用GetXmlHttpObject之后显示它会发生什么情况?注释html的首选方法是什么?首选方法是根本不注释它,而是将html放在页面本身上
var url = "edit_user.php?cmd=submitinfo&username="+document.userForm.username.value+"&firstname="+document.userForm.firstname.value+"&lastname="+document.userForm.lastname.value;
$("#<id of form goes here>").submit(function ()
{
    $.ajax({
        data: {
            "username": $("#<id of username-input goes here>").val(),
            "firstname": $("#<id of firstname-input goes here>").val(),
            "lastname": $("#<id of lastname-input goes here>").val()
        },

        url: "edit_user.php"
    });

    return false;
});
<input name=\"Submit\" type=\"button\" value=\"Update\" onclick=\"submitUserInfo();return false\"/>