Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

return false; form help - ready to give up :'(

Status
Not open for further replies.

gia999

Programmer
Mar 26, 2008
14
GB
Hello everyone,

I have spent the last 2 hours trying to get this to work and am so lost and just cant get it to work no matter what I try :(.

Basically I have a form which controls a google map which lets you put a Postcode in, it then finds the nearest pointer I have added to this based on your chosen postcode.

The problem is, this is a return false; based form, BUT I then need to also do some ASP coding based on the Postcode value submitted via the form, but i cant access it on a return false and maybe this is really easy, but I just dont know how to change it around to achieve both and with the original form working.

Sadly im no javascript guru and am really lost :(

My code is below :




<script src="type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
var map;
var icon0;
var newpoints = new Array();
var markers = new Array();

function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function'){
window.onload = func
} else {
window.onload = function() {
oldonload();
func();
}
}
}

addLoadEvent(loadMap);
addLoadEvent(addPoints);

function loadMap() {
map = new GMap2(document.getElementById("map"));
map.addControl(new GLargeMapControl());
map.addControl(new GMapTypeControl());
map.setCenter(new GLatLng( 54.41892996865827, -7.119140625), 5);
// map.setMapType(G_MAP_TYPE);

icon0 = new GIcon();
icon0.image = " icon0.shadow = " icon0.iconSize = new GSize(20, 34);
icon0.shadowSize = new GSize(37, 34);
icon0.iconAnchor = new GPoint(9, 34);
icon0.infoWindowAnchor = new GPoint(9, 2);
icon0.infoShadowAnchor = new GPoint(18, 25);
}

function addPoints() {

<% MapLoop = 0
sqlMap = "SELECT * FROM Clients WHERE cID1 <> '' AND isActive=1"
set mapRs = connMain.execute(sqlMap)
If NOT mapRs.EOF Then
While NOT mapRs.EOF
MapCoord = mapRs("cID1") %>
newpoints[<%= MapLoop %>] = new Array(<%= MapCoord %>, icon0, 'Office1', '<b>Test Solutions<%= MapLoop %><\/b><br \/>Address here');
<% MapLoop=MapLoop+1
mapRs.MoveNext
Wend
End If
mapRs.Close
set mapRs = Nothing %>

for(var i = 0; i < newpoints.length; i++) {
var point = new GPoint(newpoints[1],newpoints[0]);
var popuphtml = newpoints[4];
var marker = createMarker(point,newpoints[2],popuphtml);
markers.push(marker);
map.addOverlay(marker);
}
}

function createMarker(point, icon, popuphtml) {
var popuphtml = "<div id=\"popup\">" + popuphtml + "<\/div>";
var marker = new GMarker(point, icon);
GEvent.addListener(marker, "click", function() {
marker.openInfoWindowHtml(popuphtml);
});
return marker;
}

function closestMarkerSearch(map, markers, zoom, address) {
if (markers.length == 0) {
throw new Exception('No markers given');
}
new GClientGeocoder().getLatLng(address, function(latLng) {
if (latLng === null) {
alert('Please enter a postcode');
} else {
var distance;
var closestDistance = null;
var closestMarker = null;
for (var i = 0; i < markers.length; i++) {
distance = latLng.distanceFrom(markers.getLatLng());
if (closestDistance === null || distance < closestDistance) {
closestDistance = distance;
closestMarker = markers;
}
}
map.setZoom(zoom);
map.setCenter(closestMarker.latLng);
GEvent.trigger(closestMarker, 'click');
}
});
}
//]]>
</script>
<div id="googleMap">
<form name="SearchForm" onsubmit="closestMarkerSearch(map, markers, 10, document.getElementById('PostCode').value); return false">
<div class="formLineMap">
<label>Enter your nearest Town or Postcode:</label>
<input size="10" type="text" name="PostCode" id="PostCode" class="text" />
<input type="submit" name="Search" value="Search" class="button" />
</div>
<div class="clear"></div>
</form>
<div id="map"></div>
</div>




Any help would be so so much appreciated - you have no idea :(

Many thanks,
Gia xx
 
Does the javascript work? Or is the problem getting the Postcode from the javascript?

Assuming the javascript works:

I'd try an asp hiddenfield on the form with name, id and value properties.

Then write the Postcode to the hiddenfield value somewhere in the javascript using the hiddenfield's name property.

Then get the value of the hiddenfield with asp by using the hiddenfield's id property to access the value.
 
Hi there,

Thank you so much for getting back, so much appreciated.

Basically, yeah the Javascript all works perfect, my only problem is, is that I need to then also access the PostCode form field value via ASP when you hit submit, but I obviously cant as it is a Return false; form, so it doesnt action and refresh the page so I can then do request.form("") in ASP. Hope that makes sense.

I have looked at your suggestion, and although it sounds great, I am afraid I am not sure how to integrate that, as I didnt think there was any way of me getting that PostCode value into ASP without actioning a form?

As I say I am not great at Javascript so maybe I am being stupid, could you show me a little example of how I would do what you suggest? Then I can try it?

Thank you so much again for your help. Was at tearing my hair out point, and its driving me mad :(
 
Sorry another trail of thought I had, which maybe completely wrong, is if I could just change the current form set-up to actually action, but still allow the javascript to work, but then I could request.form("") the value for use in ASP too?

When I tried this though, whatever I was doing to try and get the javascript to still work when actioning the form, just wasnt working at all - i tried lots of things :(
 
Maybe you could make it simple and seperate the two?

Have the javascript on an button or something, they do their bit, click on the button, the script runs and does whatever it does (I have no idea) and in addition writes some values like vbdewd suggests and makes the form submit button (complete with proper action) "live" (un-grays it or makes it "visible" or something). It makes the user have to make two clicks, but maybe you could use that as a check ie ""if this looks good to you mr. user, then hit submit".

 
Thanks guys, I will give that a go see if I can do it like that. Your help is much appreciated.
 
gia999
Sorry, my idea is no good. I've been playing with a simple form and can't get anywhere with "return false" in onsubmit.

In my first post I said: "I'd try an asp hiddenfield on the form with name, id and value properties."

That's a mistake. An asp:hiddenfield has no name attribute.

If you figure out a way to make it work, please post back the result!

 

That's a mistake. An asp:hiddenfield has no name attribute.

That must be ASP.NET.

Something like

Code:
<input type="hidden" name="hosted_button_id" value="2037650">

is fine as far as I know and I think it would show up just fine in classic ASP page as the action on a form.

Code:
<%
variable=Request.Form("hosted_button_id")
%>






 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top