My JSP page has many Submit buttons for example
<input type="submit" name="RemoveCustomer" value="Submit"/>
<input type="submit" name="AddCustomer" value="Submit"/>
and within my scriptlet tag I have something like this
if(request.getParameter("addByName") != null && request.getParameter("addByName").length() > 0) {
...
} else {
if(request.getParameter("removeByName") != null && request.getParameter("removeByName").length() > 0) {
...
} else {
...
}
}
Where addByName and removeByName are HTML TextFields
Clearly I could click the button RemoveCustomer to access removeByName and do something, but if the addByName TextField was not empty it would add a customer.
What I want to know is in my scriptlet how do I know what submit button was pressed?
You could put sections in <div> with class or id that makes clear what has happened.
It sounds to me like your JSP is in need of a good refactoring. If you agree, I have two suggestions:
Eliminate all scriptlet code and use only JSTL. JSPs are for view only; move all logic out of the page.
The logic really belongs in controllers on the server side. Have a Front View Controller servlet that accepts all incoming traffic and delegates requests to classes that are written specifically to handle different use cases.
You might have grown to the point where your app could benefit from an MVC framework like Spring, Wicket, etc. I'd recommend looking into one.
You can do request.getParameter("name of submitbutton"). Here name of submit button is RemoveCustomer or AddCustomer.
Related
I am displaying a list of blog posts in a for loop, each has a comment form with send button underneath it. I need to let Django know which specific post has been clicked on so have added {{post.id}} to the button element's id. How do I then pass this information to jQuery? Is it possible to do something like this?
<button id="addComBtn{{post.id}}...>Send</button>
$('#addComBtn //post.id here').click(function() {
...
});
Or is there a better way?
Why haven't you tried out?
Yes it is possible. This should be the solution
<button id="addComBtn{{post.id}}...>Send</button>
$('#addComBtn{{ post.id }}').click(function() {
...
});
Because first Django renders the page, with its own engine(meaning, it doesn't check anything else outside of {%%}s and {{}}s), then it sends to the client, and when it renders, it will replace that.
I need to validate a Promo Code for one of my html Booking form field. If the entered promo code is correct, users can submit the Booking details. Only one unique promo code. Something like "15OFFNOW" How should I do it? Please help.
Thanks.
First, don't put the promo code in your page. Anyone can see it.
I would do this, but it depends on actually functionality.
Do client side check (this can be bypassed by a malicious user)
Do server side check
Do client side check
Use a good non-reversible hashing algorithm and verify what you have in the prom text box to the hash you have stored in a JavaScript variable or in a data-hash attribute.
So if hash(text box value) == valueOf(data-hash), then proceed to sever validation.
Do server side check
In server no need of hash. Just check the post string with the promo code you have.
i try your code
<form method="post">
<input class="form-control form-control-light bdr-2 rd-0" required
data-label="Promo Code"
data-msg="Please enter a valid promo code."
type="text" name="promo-code"
placeholder="Promo Code"
pattern="15OFFNOW" required>
<input type="submit" value="submit"/>
</form>
validation is work . show this message .
You can use Javascript for that , I fyou want to match promocode or you can validate it at backend using any backend language like PHP or java
for JQuery
//previous Ajax code here
if($("#input_id").val() !== "15OFFNOW"){
return false ;
}
// here you can proceed for Ajax request
You are looking for an input pattern, also called regexp (though I would instead suggest doing it js way (but not global) or on server side as advanced users can simply inspect html code). Most probably something like this
<input type="text" name="promo" pattern="15OFFNOW" required >
Also, please try googling it, there're similar questions like this answered also on StackOwerflow, e.g.
html: Can we match an exact string using html pattern attribute only?
js & php: check if input value not equal to integer then don't submit form
My client side scripting is rough and I just started with Razor. I haven't been able to find a way to do the following, where I want to inject a model field value within the onclick confirmation message:
#using (Html.BeginForm("Delete", "ManageLocations", new { id = #Model.Location.Id }))
{ <input type="submit" onclick="return confirm('Are you sure you wish to delete #Model.Location.DisplayLocation ?');" value="Delete">}
What am I missing?
Ooops, stupid mistake...I was embedding this BeginForm within another....The reason this does not work is you cannot have one form nested within another.
In my next application, I want to dynamically load CActiveForms into other CActiveForms with AJAX.
Therefore, I create different form views like this:
$form = $this->beginWidget('CActiveForm', [...]
But it always renders the tag. This results in nested forms:
<form id="form1">
<form id="form2">
</form>
</form>
How do i prevent the CActiveForm from rendering the form tag for form2?
I don't think it's possible natively with CActiveForms. You could however create your own widget which inherits from it. Then you can override the init() and run() methods, and take out the echo CHtml::endForm(); on line 340 and echo CHtml::beginForm on line 328.
Your results may be mixed though, a lot of Javascript and styling etc. relying on having a parent form with some settings etc.
I would like to create a form that changes dynamically.
I have a form for creating a project (with fields such as: project_name, project_description...) and the project can have any amount (bigger or equal to 0) of categories.
What i want is to display a button which would give the user the option to add another category field. In addition I would also like the option for category fields to be "deleteable" by the user (if he changes his mind or made a mistake). What would be the best way to do so. I would like an Ajax type solution.
My solution so far is to leave an empty div beneath the last category and onclick of the button to load another field into that div with yet another div which will be used for the next div. Not to happy with this solution since i now have to count how many fields I have and give each div it's own id which complicates the matter even more.
Is there a more simple solution to this?
If you are trying to add fields dynamically with a button, you can easily do so by doing something like the following:
HTML:
<form>
<p>
<label>Name:</label> <input type="text">
<label>Age:</label> <input type="text">
<span class="remove">Remove</span>
</p>
<p>
<span class="add">Add fields</span>
</p>
</form>
JS:
$(".add").click(function() {
$("form > p:first-child").clone(true).insertBefore("form > p:last-child");
return false;
});
$(".remove").click(function() {
$(this).parent().remove();
});
Demo: http://jsfiddle.net/UeSsu/1/
I started to write a form generator is based on a definition in JSON a while back. It works but could use some enhancements. It's written using Prototype.js but it wouldn't be a huge effort to port it over to jQuery.
You're welcome to steal the code. (just view source)
I've done something similar. To delete fields I didn't really removed fields. I just hidden them with a display:none and had a hidden input "delete" that I trigger to true. Then, the page receiving the result knows which field is to be deleted in the database.
They are not deleted before the form is submitted. It's like a "two pass" conception. But if you don't really need a true ajax, it works fine. Otherwise you need your JS remove function to call the server and tell to delete the field with its id. A little bit more complex to code.