Jump to content
xisto Community
Sign in to follow this  
sonesay

Javascript Object Node Referencing Help

Recommended Posts

I've got two buttons with an onclick event to set the hidden input field to a certain value then continue submitting the form. I have more than one form on display on the page and I don't want to use ids. Is there a way I can reference it from the button/button onclick event?the forms is laid out like this

echo"<form method='post' action='manage.php?manage=news&id={$rs['id']}'>";				echo"<input type='hidden' name='do' value='' />";				echo"<button type='submit' onclick=\"news_modify('edit');\">Edit</button> ";				echo"<button type='submit' onclick=\"news_modify('delete');\">Delete</button>";				echo"</form>";

So basically the idea is to fire off new_modify function and start referencing the hidden input type with name of do and set it to the supplied argument then continue submitting the form.any help is appreciated thank you.

Share this post


Link to post
Share on other sites

No I'm trying to control the <input type='hidden' name='do' value='' />to be set as either 'edit' or 'delete' depending on what ever button was pushed then submit.

Share this post


Link to post
Share on other sites

If you don't want to use id, you need to know the sequence number of the form in the display.First, you need to access the form object.With id, you can access the form object by using: document.getElementById('$rs['id'])otherwise, you can access it using: document.forms[<form number>]


Because I don't know the sequence number of your form, I will use form id instead. I will use id 'form1' for simplicity.
<script type="text/javascript">function news_modify(type){	if(type=='edit'){		document.getElementById('form1').do.value='edit'	}	else if(type=='delete'){		document.getElementById('form1').do.value='delete2'	}		document.getElementById('form1').submit();}<form method='post' action='manage.php?manage=news&id='form1'><input type='hidden' name='do' value='' /><button type='submit' onclick="news_modify('edit');">Edit</button> <button type='submit' onclick="news_modify('delete');">Delete</button></form></script>

Hope it helps

Edited by apacheNewbie (see edit history)

Share this post


Link to post
Share on other sites

I found the solution to this problem

function news_modify(e, action){	var targ;	if (!e)  	{  	var e=window.event;  	}	if (e.target)  	{  	targ=e.target;  	}	else if (e.srcElement)  	{  	targ=e.srcElement;  	}	if (targ.nodeType==3) // defeat Safari bug  	{  	targ = targ.parentNode;  	}	var tname;	tname=targ.tagName;	var targ_do = targ;	while(targ_do.name != 'do')	{			targ_do = targ_do.previousSibling;		}		targ_do.value = action;		//var form = targ.parentNode;	//form.action = form.action + '&do='+action;		}

and when you call it on <button onclick="news_modify(event, action);" . You must supply the object (I think thats what it is called) event or else it wont work.That function will get the object button and travel up looking for other siblings belonging to the same parent "Form" and get the hidden input with name of 'do'.Although this works a better solution to what I wanted to do didn't required this. I was just not using the form correctly. I could have the same effect and control with

echo"<form method='get' action='manage.php'>";				echo"<input type='hidden' name='manage' value='news' />";				echo"<input type='hidden' name='id' value='{$rs['id']}' />";				//echo"<input type='hidden' name='do' value='' />";				echo"<button name='do' value='edit' type='submit' >Edit</button> ";				echo"<button name='do' value='delete' type='submit' >Delete</button>";				echo"</form>";

Its simple yeah I cant believe I didn't know how you could use buttons like that. So in this scenario what ever button is pressed you are able to to submit 'do' with the correct value and then process your page with control.I hope this helps anyone with a similar problem. sone.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.