0

I'm trying to submit my form so that when submitted, Jquery will send the submitted data to my API. Unfortunately, when I clicked the submit Button, the page just reloaded and nothing showed. I put console.log inside Jquery .submit but nothing got printed.

enter image description here

My form in Django

<form id="popup-form" method="post">    
                    {% csrf_token %}
                    <p>
                        <label class="form-label" for="address">Address</label>
                        <input type="text" name="address" id="address"/>
                    </p>
                    <p>
                        <label class="form-label" for="latitude">Latitude</label>
                        <input type="text" name="latitude" id="latitude"/>
                    </p>
                    <p>
                        <label class="form-label" for="longitude">Longitude</label>
                        <input type="text" name="longitude" id="longitude"/>
                    </p>
                    <p>
                        <label class="form-label" for="direction">Direction</label>
                        <input type="text" name="direction" id="direction"/>
                    </p>
                    <p>
                        <label class="form-label" for="paper">Paper</label>
                        <input type="text" name="paper" id="paper"/>
                    </p>
                    <!-- Submit button -->
                    <p>
                        <input type="submit" value="Submit"/>
                    </p>
                </form>

My submit function

$(document).ready(function(){
        $("#popup-form").submit(function(e){
            e.preventDefault();
            console.log("Submitted!");
            
            var address = $("#address").val();
            var latitude = $("#latitude").val();
            var longitude = $("#longitude").val();
            var direction = $("#direction").val();
            var paper = $("#paper").val();
            console.log(address);
            console.log(latitude);
            console.log(longitude);
            console.log(direction);
            console.log(paper);


            axios.get('http://127.0.0.1:8000/api/real-estate/apartment-sale-hanoi-price/predict', {
                
                "latitude": latitude,
                "longitude": longitude
                
            })
            .then(response => {
                predict_price = response.data.predicted_price;
                console.log("predicted_price: " + predict_price);
            })
            .catch(error => console.log(error));
            
        });
        });

My API returns a JSON object containing predicted_price, which worked when I tested on Thunder client. I tried putting the submit function inside document.ready and check my submit button with type "submit". I don't know what went wrong on the client side. Could you show me a way to solve this?

William Le
  • 825
  • 1
  • 9
  • 16
  • That code looks fine, it should at minimum be calling `console.log("Submitted!");`. Is this popup something that gets added to the page some time *after* the document is ready? – Kevin B Feb 14 '22 at 16:04
  • This form is actually inside a Leaflet popup, could that be a problem? – William Le Feb 14 '22 at 16:05
  • 1
    If it's added dynamically, see https://stackoverflow.com/questions/203198/event-binding-on-dynamically-created-elements – Barmar Feb 14 '22 at 16:06
  • Thank you you pointed for me a way out of this – William Le Feb 14 '22 at 16:11

0 Answers0