I am having problems with this form it has two submit options a button and an onchange instance the onchange just will not work can any one tell me why?

my doc type is

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

the form is

<table class="mailbox">
<form id="form1" action="mymessages.php?" method="post">
<tr><td colspan="4" class="header"><?=$folder?></td></tr>
<tr><td colspan="2" class="header">Subject</td><td class="header">Date</td><td class="header">From</td></tr>
<?
if ($messagecount>0)
{
echo'<tr><td class='.$viewclass.'><input type="checkbox" name="selected" value="'.$mid.'" /><td class='.$viewclass.'><a href="mymessages.php?&mid='.$mid.'">'.$subject.'</a></td><td class='.$viewclass.'>'.$daterec.'</td><td class='.$viewclass.'>'.$fromuser.'</td></tr>';
}
if ($messagecount<0)
{
echo '<tr><td colspan="4">You have no messages in your '.$folder.'</td></tr>';
}
?>
<tr><td class="header"><input type="submit" name="submit" value="Delete" /></td><td colspan="3" class="header">Move To:
<select name="test" onchange="submit()">
<option value="" selected="selected">Folder</option>
<?
while($row=mysql_fetch_array($resultfolder))
{
$folders   = ucwords($row["folder"]);
if ($folders==$folder)
{
$folders == " ";
}
echo '<option value="'.$folders.'">'.$folders.'</option>';
}
?>
</select>
</form>
</td></tr>
</table>

Dani AI

Generated

Short version: was right — a form control named submit can shadow the form's built-in submit functionality in some browsers, preventing an onchange-driven submission from working. That is a DOM name-collision issue, not a PHP bug.

A robust pattern:

  • Give the submit button a safe name (for example deleteBtn), and use a dedicated hidden field (for example actionType) to indicate what the user intended when the form is sent.
  • Use unobtrusive JS to set that hidden field when the select changes and then call the form's native submit method in a way that avoids name collisions.

Example (do not copy the names from the original thread; use your own names consistently):

<!-- hidden inputs inside the form -->
<input type="hidden" name="actionType" value="">
<input type="hidden" name="targetFolder" value="">

<script>
document.getElementById('form1').addEventListener('change', function(e) {
  var sel = this.elements['moveTo'];
  if (!sel || sel.value === '') return;
  this.elements['actionType'].value = 'move';
  this.elements['targetFolder'].value = sel.value;
  HTMLFormElement.prototype.submit.call(this);
});
</script>

Other practical tips:

  • Checkboxes for multi-select should use name="selected[]" so PHP receives an array.
  • Avoid naming inputs action, method, submit, etc., because browsers often expose form controls as properties on the form object.
  • Keep your markup valid (place <form> around the table rather than inside table rows) to reduce cross-browser quirks.
  • When things fail, open the browser console for JS errors and use the Network tab to inspect the exact POST payload; that quickly shows which fields are present.

This explains why the onchange submit failed and how to handle server-side detection reliably without relying on which button happened to be clicked.

Recommended Answers

All 4 Replies

Shadiadiph,

I've seen this before and it's a weird one! An language/browser bug in my opinion.

In your submit button's tag, name="submit" kind of hijacks submit() , or even this.form.submit() within the form, so you get a javascript error.

Solution: Change name="submit" to eg. name="formSubmit" .

If you read $_POST server-side, remember to modify your php accordingly.

Airshow

awesome thank you this has been puzzling me as my scripting seems fine i guess it must be a browser issue. thanks

the onchange submit now works but i just tested it like this i changed the name="submit" to name="formSubmit" like you said and just did a test in the heading to get a response from posting using.

if (isset($_POST["submit"]))
{
print "hello";
}
if (isset($_POST["formSubmit"]))
{
print "goodbye";
}

clciking on the delet value button produces goodbye fine but the onchange submit returns nothing?? sorry i am useless at javascript so i have no idea what it is asking me for.

don't worry i figured it out thanks for the help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.