user1149620
user1149620

Reputation: 873

PHP/AJAX/Javascript/MySQL Form Submission

I'm following a tutorial, this version of the form works, where I use onchange. However, the second version below where I try to use onsubmit doesn't work. Can anyone help please?

<form>
<select name="users" onchange="showUser(users.value)">
    <option value="">Select a person:</option>
    <option value="1">Peter Griffin</option>
    <option value="2">Lois Griffin</option>
    <option value="3">Glenn Quagmire</option>
    <option value="4">Joseph Swanson</option>
</select>

This is the one that doesn't work and I'm not sure why?

<form onsubmit="showUser(users.value)">
<select name="users">
    <option value="">Select a person:</option>
    <option value="1">Peter Griffin</option>
    <option value="2">Lois Griffin</option>
    <option value="3">Glenn Quagmire</option>
    <option value="4">Joseph Swanson</option>
</select>
<input type="submit" value="Submit"  >

Upvotes: 0

Views: 262

Answers (1)

Rick Kuipers
Rick Kuipers

Reputation: 6617

Try this:

<select name="users" onchange="showUser(this[this.selectedIndex].value);">
    <option value="">Select a person:</option>
    <option value="1">Peter Griffin</option>
    <option value="2">Lois Griffin</option>
    <option value="3">Glenn Quagmire</option>
    <option value="4">Joseph Swanson</option>
</select>

For the other one:

<form onsubmit="showUser(this.users[this.users.selectedIndex].value); return false;">
<select name="users">
    <option value="">Select a person:</option>
    <option value="1">Peter Griffin</option>
    <option value="2">Lois Griffin</option>
    <option value="3">Glenn Quagmire</option>
    <option value="4">Joseph Swanson</option>
</select>
<input type="submit" value="Submit" />

Using return false; prevents the browser from posting the form to the url and thus reloading the page.

Upvotes: 2

Related Questions