Passing variables from AJAX to ASP

Asked

Viewed 478 times

0

I have a contact form that uses an AJAX function to validate and return a success PARAMETER that allows me to send the message WITHOUT the visitor leaving the contact page.

Normally, I use these Forms in HTML pages and the function PROCESSES and SENDS the message in the backend, without any intervention.

Works! but only if the server runs PHP.

On a WINDOWS server it does not work. This is because inside AJAX I will need to load the page that handles and sends the variables in email format. On a WINDOWS server I will need a classic ASP page.

The function in HTML works, in ASP works, but I can’t get ajax to send variables from FORM to the ASP page, so that ASP captures and treats the message.

I am going to paste here the section in question:

var dataString = '?name=' + name + '&email=' + email + '&message=' + message + '&fone=' + fone;
// alert (dataString);return false;

        $.ajax({
            type:"GET",
            url:"contact.asp",
            data:dataString,
            success:function () {
                $('#af-form').prepend("<strong>Sua mensagem foi enviada!</strong> Em breve entraremos em contato.<br>&nbsp;");
                $('#af-form')[0].reset();
            }
        });
        return false;

If I create a PHP page with the same instructions as the ASP page, AJAX sends the data correctly (but obviously does not process).

So what I need to do is CHANGE the way the datastring content is being passed on. So, friends, just asking the college for help.

* ATTENTION * 1) Read the question carefully; 2) Refrain from "Okay, but why ASP?" 3) Suggest alternative paths, but try to maintain the integrity of the function. 4) Be kind and polite to those who know less than you.

[]'s

  • Helium, see if this link helps you: https://goo.gl/1ao3BR .

  • It’s an interesting approach (thank you!), but it assumes that my source document is an ASPX. Actually what I have in hand is an HTML.

1 answer

1


You can do your ajax like this.

let name;
let email:
name = document.getElementById('name');
email= document.getElementById('email');

//voê pode setar o email ao inves do getElementById com o $ com jquery
//exemplo:
name = $('#name').val();
email = $('#email').val();

// alert (dataString);return false;

        $.ajax({
            type:"GET",
            url:"contact.asp",
            data : { name : name, email : email },
            // se não funcionar utilizar com o encodeURIComponent
            // data : { name : encodeURIComponent(name), email : encodeURIComponent(email)},
            success:function () {
                $('#af-form').prepend("<strong>Sua mensagem foi enviada!</strong> Em breve entraremos em contato.<br>&nbsp;");
                $('#af-form')[0].reset();
            }
        });
        return false;

or


name = document.getElementById('name');
email= document.getElementById('email');

$.ajax({
            type:"GET",
            url:"contact.asp?name="+encodeURIComponent(name)+"&email="+encodeURIComponent(email),
            success:function () {
                $('#af-form').prepend("<strong>Sua mensagem foi enviada!</strong> Em breve entraremos em contato.<br>&nbsp;");
                $('#af-form')[0].reset();
            }
        });
        return false;

the input must contain the id

<input type="text" id="name" />

To rescue the data in Asp you must do so to GET method

dim name
name = Request.QueryString("name")

To rescue the data in Asp you must do so to method POST

dim name
name = Request.Form("name")

If I remember well this below should work in the 2

dim name
name = Request("name")

I advise you to always remove single quotes before inserting this into the database if this is the case so there is no sql Injection

dim name
name = Request.QueryString("name").Replace("'","")

I haven’t had a chance to test the code but it should work.

  • Perfect, bro. There’s only one little detail to adjust regarding coding. but the function fulfills what is expected! Thanks, thanks!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.