remove all the items in jstl foreach on button click spring - sql

I have created a search query to filter records from the database. Whenever I enter the value I want to filter for eg the email as a criteria and click on search, it retrieves the record from the database, if I click on search again instead of adding the new search result it repopulate the list with the new records and keeping the old record in view.
editted
this is adminList initialization
List<Admin> adminList = new ArrayList<Admin>();
this is the query I am using to search
#SuppressWarnings("unchecked")
public List<Admin> getAllAdmins(String singleAdmin) {
try{
String query = "SELECT DISTINCT e.* FROM admin e WHERE e.email like '%"+ singleAdmin +"%'";
List<Object[]> adminObjects = fetchAll(query);
for(Object[] adminObject: adminObjects) {
Admin adminIndividual = new Admin();
Integer id = ((Integer) adminObject[0]);
String email = (String) adminObject[1];
String name = (String) adminObject[2];
adminIndividual.setId(id);
adminIndividual.setEmail(email);
adminIndividual.setName(name);
adminList.add(adminIndividual);
}
System.out.println("database values for admin>>>>> "+adminList);
return adminList;
}catch(Exception e){
e.printStackTrace();
return null;
}
}
this is the snippet of code in my .jsp file that send the data to the controller to make a search for the query criteria
<form method="post" action="searchAdmin">
<p><label for="name">Admin Email</label>
<input class="form-control" type='text' name='searchName' id='searchName'/>
<input class="btn btn-success" type='submit' value='RETRIEVE'/>
</p>
this is how I am populating the list that gets the values whenever a new search is made
<c:forEach items="${adminList}" var="admin">
<p>
<label for="name">FULLNAME</label>
<input type="text" name="name" placeholder="" value="${admin.name}" readonly/>
</p>
<p>
<label for="email">Email</label>
<input type="text" name="email" placeholder="" value="${admin.email}" readonly/>
</p>
</c:forEach>
you can see the fullname repeating twice and if I enter an email again to click on search, it doubles to three retaining the two previous records. Please how do I tackle this issue whenever I try to search, as the page refreshes let is populates the search query with out repeating with the previous search query. Kindly assist!

Related

URL address availability - auto verification

I have an input that's in form. I enter the URL adress into this input, click sumbit, and below it shows if this address is available or not. This is done.
Now I want to make this information show up automatically (without clicking submit), after making a change in the input? I would also like the value in the input to not disappear after checking. How can I do that?
I would like it to look more or less like this, but instead of email correctness, URL availability - https://youtu.be/HzJngc-Se9Q
<form action="" method="GET" name="form1" id="form1">
<div id="custom-search-input">
<div class="adress-div">
<input type="text" id="ok" name="domain" maxlenght="30" class="adress" pattern="(.{1,})?([.]{1})?.+[.]{1}.+" placeholder="np. www.page.com" title="Enter URL adress." autocomplete="off" required/>
<br>
<input type="submit" class="button2" value="Check!">
</div>
</div>
</form>
<?php
error_reporting(0);
if(isset($_GET['domain'])){
$domain = $_GET['domain'];
$godaddycheck = 'https://in.godaddy.com/domains/searchresults.aspx?checkAvail=1&tmskey=&domainToCheck='.$domain.'';
$namecomcheck = 'https://www.name.com/domain/search/'.$domain.'';
$registercomcheck = 'http://www.register.co, m/domain/search/wizard.rcmx?searchDomainName='.$domain.'&searchPath=Default&searchTlds=';
if ( gethostbyname($domain) != $domain ) {
echo "<br><br><h1 style='color: #e30000;'><b>$domain</b> not available.</h1>";
}
else {
echo "<br><br><h1 style='color: #00e339;'><b>$domain</b> available.</h1><h2>
</h2>";
}
}
?>
This is not possible doing only with PHP.
You will need to do that with JavaScript. Therefore you need to
listen on an input change for your <input>field
Send the value of the input field with AJAX (https://developer.mozilla.org/en-US/docs/Web/Guide/AJAX/Getting_Started) to a PHP script, which then validates the domain
Fetch the response and display it to the user
But there are a lot of tutorials out there, how to do that. For example: https://www.w3schools.com/php/php_ajax_php.asp

only first word from radio button selection is being returned

To get the selected option from a radio button form, I'm looking at request.vars to get the value. Following is the controller code:
def get_results():
test = request.vars['QuestionOne']
for topic in session.selectedtopics:
test = test + request.vars[topic]
return locals()
And now the code for the view:
{{extend 'layout.html'}}
<P>Please select the options that most closely approximates the actual scenario.
<br>
<form action="{{=URL('get_results')}}" method="post">
{{nameTopic =""}}
{{session.selectedtopics = list()}}
{{for topic in topics:}}
{{if nameTopic <> topic.topic.replace(" ", "_"):}}
<br><h2>{{=topic.topic}}</h2>
{{nameTopic = topic.topic.replace(" ", "_")}}
{{session.selectedtopics.append(nameTopic)}}
{{pass}}
<p><input type="radio" name={{=nameTopic}} value={{=topic.param}}>{{=topic.param}}</p>
{{pass}}
<br>
<input type="submit">
</form>
Here is the problem: I don't know the reason, but it is getting only the first word of the selected option in the radio form. For example, the option selected is "It is normal", but the var is returning only "It". Any idea why it is happening?
Thank in advance.
You need to quote the HTML attribute values:
<input type="radio" name="{{=nameTopic}}" value="{{=topic.param}}">
Without the quotes, you'll end up with final HTML like:
<input type="radio" name=some_topic value=It is normal>
The browser will interpret the value to be "It", with "is" and "normal" being invalid HTML attributes.

Getting Place ID based on Place Name, City Name and Address from Google Places API service

I am using the Google Places API service(https://developers.google.com/places/web-service/) to get the place id based on location name. The user types some location name and related suggestions appear from which the user can select and get the place Id. I have a requirement where I need to have three textboxes and a button. The user will enter Place Name, City Name and Address and click the button to get placeId. Right now, I don't see an option to get place Id based on more than one parameter. How can I accomplish that?
Text Search Requests accept a query string. One potential solution would be to allow your users to enter the place name, city, and address into different text fields, but then concatenate all of them into a single query string before sending your ajax request.
Your form looks something like this:
<form id="form">
<input type="text" name="place" value="">
<input type="text" name="city" value="">
<input type="text" name="address" value="">
<input type="submit" value="Locate">
</form>
Your javascript will look something like this:
$( "#form" ).submit(function( event ) {
// concatenate places into a single query string
var query = $('input[name="place"]').val() + $('input[name="city"]').val() + $('input[name="address"]').val();
// convert spaces to '+' symbol for query
query = encodeURIComponent(query);
// send ajax request
$.ajax({url: "https://maps.googleapis.com/maps/api/place/textsearch/xml?query=" + query + "&key=YOUR_API_KEY", success: function(result){
alert('success, now retrieve your id from result variable');
}});
//prevent the submit button from reloading the page
event.preventDefault();
});

MonoRail CheckboxList?

I'm trying to use a Checkboxlist in MonoRail to represent a many to many table relationship. There is a Special table, SpecialTag table, and then a SpecialTagging table which is the many to many mapping table between Special and SpecialTag.
Here is an excerpt from the Special model class:
[HasAndBelongsToMany(typeof(SpecialTag),
Table = "SpecialTagging", ColumnKey = "SpecialId", ColumnRef = "SpecialTagId")]
public IList<SpecialTag> Tags { get; set; }
And then in my add/edit special view:
$Form.LabelFor("special.Tags", "Tags")<br/>
#set($items = $FormHelper.CreateCheckboxList("special.Tags", $specialTags))
#foreach($specialTag in $items)
$items.Item("$specialTag.Id") $Form.LabelFor("$specialTag.Id", $specialTag.Name)
#end
The checkboxlist renders correctly, but if I select some and then click Save, it doesn't save the special/tag associations to the SpecialTagging table (the entity passed to the Save controller action has an empty Tags list.) One thing I noticed was that the name and value attributes on the checkboxes are funky:
<label for="special_Tags">Tags</label><br>
<input id="3" name="special.Tags[0]" value="UCampus.Core.Models.SpecialTag" type="checkbox"> <label for="3">Buy 1 Get 1 Free</label>
<input id="1" name="special.Tags[1]" value="UCampus.Core.Models.SpecialTag" type="checkbox"> <label for="1">Free</label>
<input id="2" name="special.Tags[2]" value="UCampus.Core.Models.SpecialTag" type="checkbox"> <label for="2">Half Price</label>
<input id="5" name="special.Tags[3]" value="UCampus.Core.Models.SpecialTag" type="checkbox"> <label for="5">Live Music</label>
<input id="4" name="special.Tags[4]" value="UCampus.Core.Models.SpecialTag" type="checkbox"> <label for="4">Outdoor Seating</label>
Anyone have any ideas?
Thanks!
Justin
The checkboxlist renders correctly
it seems to me that you could also render something like
<input type="checkbox" name="special.Tags" value="1"/>
<input type="checkbox" name="special.Tags" value="2"/>
which make it simpler (no index to output for the name, it will be properly resolved as an array via controller action parameter binding
also, in your sample, the fact that all checkboxes having the same value UCampus.Core.Models.SpecialTag is probably not right, you may want to output actual primary key identifier from the tags (not sure, could you display the class you are binding back on the form handling action?)
I was able to get it working by specifying the id and text attributes...
$Form.LabelFor("special.Tags", "Tags")<br/>
#set($items = $FormHelper.CreateCheckboxList("special.Tags", $specialTags, "%{value='Id', text='Name'}"))
#foreach($specialTag in $items)
$items.Item("$specialTag.Id") $Form.LabelFor("$specialTag.Id", $specialTag.Name)
#end

MySQL query using multiple criteria from checkboxes

I would like to do a multiple search query usig multiple checkboxes which represent particular textboxes.
How do i create a mysql query which will be filtering the checked and unchecked checkboxes (probably using if statements)?
The query should be able to filter the checked and ucnchecked boxes and query them using the AND condition.
Thanks
Mysql won't be able to handle your html form post. You will need to use a server-side language to check for those checkboxes being set and then modify your query with additional conditions.
You would likely get a better response if you were more specific, but I'll throw you an example with php. Assuming your form looks something like:
<form method="post">
<input type="checkbox" name="field1_cb" value="1" />
<input type="text" name="field1_txt" />
<br /><br />
<input type="checkbox" name="field2_cb" value="1" />
<input type="text" name="field2_txt" />
</form>
Your server code might look something like:
$query = "SELECT * FROM tbl WHERE field0 = 'asdf' ";
$fields = array('field1','field2');
foreach($fields as $f){
if(isset($_POST[$f.'_cb'])){
$query .= " AND $f = ".$_POST[$f.'_txt'];
}
}
//Run your query...
Please don't copy and paste this code directly, it is disgustingly insecure. Just trying to get you started.