Okay i am going nuts here i have a jsp page with multiple task to complete add,delete update . in order to do so i knopw i must set vlaues of a hidden input to say add,delete or update based upon what buttom was pressed so i have made the following code.


the forms name is test

the hidden inputs:

   <input type="hidden" name="m1" value="" />
//set to blank
the button:
<input name="Add" type="submit" value="Add" alt="Add" onclick='javascript:return add();'

<input name="Update" type="submit" value="Update" alt="Update" onclick='javascript:return Update();'

<input name="Dekete" type="submit" value="Delete" alt="Delete" onclick='javascript:return Delete();'
The javascript:

function add(){
      
     

alert("this shit worked")
document.test.getElementById("m1").value = "Add"
alert(document.test.getElementById("m1").value);







  }
I am using the same code as above for the update and delte just changing the function name and the values entered

I am then validating the Jsp code to only work if the value is for example add - does the add code i know the validation works as i have manually entered the values and it worked a treat.

I am very new to JS so any help would be appreciated

I have the JS code in the <script> </script> because i have other validation elements working so this is not the issue.

The problem: The above code does not seem to change the hidden inputs value

Thanks in advance

Osirion,

You don't need to use a hidden field for this.

You can test to see which submit button was clicked in JSP. The clicked button will appear in the POST parameters - the other two won't.

I can't remember the syntax in JSP but I'm sure you know that.

Airshow

Osirion,

You don't need to use a hidden field for this.

You can test to see which submit button was clicked in JSP. The clicked button will appear in the POST parameters - the other two won't.

I can't remember the syntax in JSP but I'm sure you know that.

Airshow

Thanks for setting me in the right direction !

I found the below code:

You should name each button:

<input type="submit" name="SAVE" value="Save">
<input type="submit" name="DELETE" value="Delete">

In your JSP:

String button = request.getParameter("SAVE");( SET ON CLICK)

if(button != null) ;//process save request
else {
button = request.getParameter("DELETE");
if(button != null) ;//process delete request
}

Have not tested it yet but hopefully will work

Osirion,

Remembering that you have three buttons, "Add", "Delete" and "Update", you might like to do it as follows.

It's a good idea to test the query string towards the top of page and set variable(s) accordingly:

String action;
if( request.getParameter("add") != null )
{
	action = "add" : 
} 
else if ( request.getParameter("delete") != null )
{
	action = "delete";
}
else if ( request.getParameter("update") != null )
{
	action = "update";
}
else
{
	action = "" ;
}

You may be able to do it like this (I think the order of precidence for the ternary operator in java/JSP is C-like and not php-like; if not you will need more parentheses):

String action = ( request.getParameter("add") != null ) ? "add" : ( request.getParameter("delete") != null ) ? "delete" : ( request.getParameter("add") != null ) ? "update" : "" ;

Then, further down the page, where you need to branch:

switch(action){
	case "add":
		//"add" statements
	break;
	case "delete":
		//"delete" statements
	break;
	case "update":
		//"update" statements
	break;
	default:
		//default statement (if required)
	}
}

This structure can be repeated if you need to branch on action again.

Hope this helps.

Airshow

commented: thanks +1

Airshow thank you for your help and time! You helped me solve the issue!

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.