-1

I have a form which is sending data to another script with AJAX post. Idea is to change this to GET, so I can submit form or trough form input where user click "Submit" or just hit "Enter", and form data is passed with AJAX to another script, and data is returned to origin script.

What I want is to make user able just to type parameter in url, like whois.com/domain.com, and just hit enter, so my form can be autopopulated with data in url and submitted, so my JS can be trigerred to make AJAX call.

<script>
    $('#form1').submit(function(event) {
        event.preventDefault();
        $.ajax({
            type: 'GET',
            url: 'check.php',
            data: $(this).serialize(),

            success: function (data) {

                //console.log(data);

                $('#response').html(data);
                $("#myModal").modal();
            }
        });
    });
    </script>

Idea: Check if $_GET['variable'] is set in url yoursite.com?domain=domain.com, and then pass variable to form, and submit form by code?

Alan Kis
  • 1,790
  • 4
  • 24
  • 47

1 Answers1

1

You could use location hash instead of query string. For example, you can use format: http://yoursite.com/#domain=domainname.com

Then, you can register hashchange event listener, and in event handler you can extract domain and pass it to ajax function.

window.addEventListener('hashchange', function(ev){
    doNewAjaxRequest(location.hash.replace('#domain=', ''));
});

Or, if you are using jQuery:

$(window).on('hashchange', function(ev){
    doNewAjaxRequest(location.hash.replace('#domain=', ''));
});

Or, you can use just domain name after the hash, as: #domainname.com

$(window).on('hashchange', function(ev){
    doNewAjaxRequest(location.hash.replace('#', ''));
});

Now your users can type different domain, and when they press enter, you can handle the change.

*Keep in mind that anything after the # is not sent to server in url, but you can access and use it from javascript.

pisamce
  • 533
  • 2
  • 6