Jump to content

onsubmit function problem


Go to solution Solved by ginerjm,

Recommended Posts

A bit rusty on my JS but this seems correct to me. Just doesn't work.

 

	function CheckAddEntries()
	{
		var returnval = false;
		alert("in checkaddentries");
		var mname = document.getElementById('addform').getElementById('machine_name');
		alert("hit first value "+mname);
		var target = f.getElementById('target').value;
			return returnval;
	}
Right now the function seems useless, but that's because it's not working so far. Here's the call:

(This form is contains a single row of an html table)

 

$input_elems .= "<form method='POST' name='addform' id='addform' onsubmit='return CheckAddEntries()'>";
$input_elems .= "<tr>";
$input_elems .= "<td>";
$input_elems .="<input type='text' name='machine_name' id='machine_name' value=''>";
$input_elems .= "</td>";
$input_elems .= "<td>";
$input_elems .= "<input type='text' name='target' id='target' value=''>";
$input_elems .= "</td>";
$input_elems .= "<td>";
$input_elems .= "<input type='submit' name='btn' value='Add Machine'>";
$input_elems .= "</td>";
$input_elems .= "</tr>";
$input_elems .= "</form>";
When I click the Add Machine button the JS function shows my first alert message but it never shows the second one. Anyone see something I'm doing wrong here?
Link to comment
https://forums.phpfreaks.com/topic/297540-onsubmit-function-problem/
Share on other sites

I've never used getElementByID() recursively like this

var mname = document.getElementById('addform').getElementById('machine_name');

Is there a reason why you are referencing the form using getElementById() then trying to reference the 'machine_name' elements using getElementById() from that? Why not just directly reference the 'machine_name' element using

var mname = document.getElementById('machine_name');

Also, the function doesn't make sense. You are getting the value for Target, but then simply returning returnVal - which is false. I'm guessing this is not complete. And, there will also be a failure on this line since 'f' is not defined

 

var target = f.getElementById('target').value;
Edited by Psycho

Correct. As I said, it doesn't do anything yet because what I have doesn't work. And I have since altered my code a bit.

 

Now passing the form object with the call:

onsubmit='return CheckAddEntries(this.form)'
and retrieving the form object in the function like this:

function CheckAddEntries(frm)
{
  var f = frm;
  var returnval = false;
  alert("in checkaddentries ");
  var mname = f.getElementById('machine_name').value;
  alert("hit first value "+mname);
  var target = f.getElementById('target').value;
  return returnval;
}
I still get the first alert but not the second.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.