views:

104

answers:

4

I am generating the following html form with this php:

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>";

Which is handled by submitUserInfo, which is here:

function submitUserInfo() {
    url = "edit_user.php?cmd=submitinfo&username="+document.userForm.username.value+"&firstname="+document.userForm.firstname.value+"&lastname="+document.userForm.lastname.value;
    var xmlHttp=GetXmlHttpObject();
    if(xmlHttp.responseText == 'true') {
     alert(url);
     xmlHttp.open("GET",url,true);
     xmlHttp.send(null);
     //updateByUser(username);
    }
}

I clearly have url defined as beginning with

edit_user.php?cmd=submitinfo&username

however when pressing the submit button, it tries to send the url as

edit_user.php?username=

and I cannot figure out why. I have used the above technique succusfully with other forms on my site, and can not find any reason that cmd=submitinfo& is being excluded.

A: 

Use a tool like Firebug to determine if the browser is sending it without cmd=submitinfo& or if it's being lost on the server side.

Matt
Wouldn't displaying the url be just as useful to see if I am sending it clientside or not?
Joshxtothe4
Displaying it where? You need to start at the root of the problem and seeing if it's making it through the first place it's sent -- from the browser.
Matt
A: 

Change:

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

to:

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

You must declare your url variable before you try to use it.

Also, avoid echoing straight HTML to the page. The way you've done it is going to be an absolute pain to debug, update, and stylize.

John Rasch
what is the preferred method to comment html?
Joshxtothe4
The preferred method is not to comment it at all, but put the HTML on the page itself
John Rasch
A: 

Frameworks like jQuery is not really needed for such simple tasks, although I would suggest you taking a look if you plan to implement more JavaScript on your site.

Your code, using jQuery, would look like this:

$("#<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;
});

Looks a bit nicer, I think :)


Also; using frameworks - not jQuery in particular - takes care of browser work-arounds.. so you won't have to.. take care of 'em, that is :)

roosteronacid
A: 

I think you need to change the type of the button from "submit" to "button":

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