views:

32

answers:

3

hey guys, this might be really stupid, but hopefully someone can help. I'm trying to post to an external script using ajax so i can mail the data, but for some reason my data is not making it to the script.

$(document).ready(function() {
$("#submitContactForm").click(function () {
    $('#loading').append('<img src="http://www.xxxxxxxx.com/demo/copyshop/images/loading.gif" alt="Currently Loading" id="loadingComment" />');
        var name = $('#name').val();
        var email = $('#email').val();
        var comment = $('#comment').val();
        var dataString = 'name='+ name + '&email=' + email + '&comment=' + comment;

        $.ajax({
            url: 'http://www.xxxxx.com/demo/copyshop/php/sendmail.php',
            type: 'POST',
            data: '?name=Dave&[email protected]&comment=hiiii',
            success: function(result) {
                $('#loading').append('success');
                }
            });    

    return false;
});
});

the php script is simple (for now - just wanted to make sure it worked)

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$comment = $_POST['comment'];

$to = '[email protected]';
$subject = 'New Contact Inquiry';
$message = $comment;

mail($to, $subject, $message);
?>

the jquery is embedded in an .aspx page (a language i'm not familiar with) but is posting to a php script. i'm receiving emails properly but there is no data inside. am i missing something? i tried to bypass the variables in this example, but its still not working

thanks

A: 

You cannot use AJAX to send a request to a different domain.

SLaks
i feared this was the case...... is there any other way to do this?
Dave Kiss
You can make a proxy script in your domain that forwards the request to the other domain.
SLaks
can you tell me more about your option slaks?
Dave Kiss
Make a PHP (or ASP.Net) page on your domain that sends the request to the other domain.
SLaks
problem is, i can't create pages on domain A - i can only insert html/javascript into an existing page using a custom page editor
Dave Kiss
Then you can use [JSONP](http://en.wikipedia.org/wiki/JSON#JSONP).
SLaks
+1  A: 

You can't using Ajax to talk to a site that isn't in the same origin as the document your script is running in, unless both the browser and the destination support CORS. You can use JSONP to work around it a bit, but really CORS is the future in this regard.

T.J. Crowder
do you have a suggestion to the best way around this?
Dave Kiss
who said anything about different domains?
Sean Kinsey
the processing script is on another domain.
Dave Kiss
@Dave Kiss: CORS and JSONP *are* the suggestions. :-) Before those came on the scene I'd've said do it server-side: You send your request to a page on your server, which sends the request to the other domain, and then gives you the response (e.g., proxies it). Aside from those, you start getting into signed Java applets/Flash apps/ActiveX controls...
T.J. Crowder
A: 

Remove the '?' character from your data. I'm guessing this is messing up the data parsing in PHP.

When posting the data should be encoded using encodeURIComponent and this will encode ? as %3F.

Sean Kinsey