views:

448

answers:

1

I've added ability for users to add new point on my google map. My code:

GEvent.addListener(map, "click", function(overlay,point) {
    if (point) {
        var myHtml = '<div id="addpoint"><form id="formadd" name="formadd_point" enctype="multipart/form-data" method="post" action="pointadd.php"><table><tr><td>Place:</td><td>' + point + '</td></tr><tr><td>Name:</td><td><input name="name" type="text" size="32" maxlength="200" /></td></tr><tr><td>Photo (jpg,png:2Mb):</td><td><input type="file" name="image" size="20" accept="image/png,image/jpeg" /></td></tr><tr><td></td><td><input name="pcoord" type="hidden" value="'+point+'" /><input type="hidden" name="MAX_FILE_SIZE" value="2000000" /><input name="subpoint" type="submit" value="Add" /></td></tr></table></form></div>';
        map.openInfoWindowHtml(point, myHtml);
        $('#formadd').ajaxForm({ beforeSubmit: validate, target:'#addpoint' });
    }
});

It should be AJAX post without reloading. But It is not working! I don't know why. Maybe there is some conflict jquery.js and google api? How to submit form without reloading page?

+1  A: 

Maybe you try to add eventListener to form before form html successfully added dom. Try this:

GEvent.addListener(map, "click", function(overlay,point) {
    if (point) {
        $('#formadd').live("load", function(){
            $(this).ajaxForm({ beforeSubmit: validate, target:'#addpoint' });
        });

        var myHtml = '<div id="addpoint"><form id="formadd" name="formadd_point" enctype="multipart/form-data" method="post" action="pointadd.php"><table><tr><td>Place:</td><td>' + point + '</td></tr><tr><td>Name:</td><td><input name="name" type="text" size="32" maxlength="200" /></td></tr><tr><td>Photo (jpg,png:2Mb):</td><td><input type="file" name="image" size="20" accept="image/png,image/jpeg" /></td></tr><tr><td></td><td><input name="pcoord" type="hidden" value="'+point+'" /><input type="hidden" name="MAX_FILE_SIZE" value="2000000" /><input name="subpoint" type="submit" value="Add" /></td></tr></table></form></div>';
        map.openInfoWindowHtml(point, myHtml);

    }
});
Murat Corlu
Thank you very much for the answer. I spent a lot of time trying to understand what is the problem. It was my headache. Your code is working even if I write it after map.openInfoWindowHtml! The only problem is that I have to click submit button twice. Nothing happens after first clicking. And the form is sent by Ajax after the second clicking.Thank you very much for helping to solve this problem!
SPnova
I'm not sure that how ajaxForm plugin is running but I imagine what is problem. First eventHandler that I wrote, adds a handler to submit event and returns false. And I think ajaxForm plugin adds eventHandler to handle submit of form there. So first click is doing nothing but running ajaxForm event handler. And second click trigger of ajaxForm's handler.If you can find a solution to adding ajaxForm eventHandler by a live event, your problem will gone.
Murat Corlu
I changed my code above. Maybe this will work.
Murat Corlu