Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

problem with AJAX call and email script

Options
  • 26-06-2008 10:18am
    #1
    Registered Users Posts: 26,579 ✭✭✭✭


    hi,

    bit of background. i have this web form that has two drop down boxes, an area and a forum and also has a free text field. the idea is a user comes in picks their area, their forum and inputs their userid and it basically goes off to the database, selects the correct information from what they supplied on the form and then sends an email to that person.

    i want to do this by ajax and i had this working on a solaris 8 machine before, but now need to port it over to a suse machine.

    this is my emailtest.pl script that gets called by the AJAX function personalemail().

    [php]
    #!/usr/bin/perl

    use DBI;
    use CGI;

    my $page = new CGI();


    #these are the variables taken from the webpage.
    my $personalarea=$page->param('personalarea');
    my $personalforum=$page->param('personalforum');
    my $personalid=$page->param('personalid');


    #these are hard coded variables which when uncommented make the script work if ran from command line eg ./emailtest.pl
    #my $personalid = "jsmith";
    #my $personalarea = "finance";
    #my $personalforum = "test";

    open(DAT, ">params.txt");
    print DAT $personalarea . "\n";
    print DAT $personalforum . "\n";
    print DAT $personalid . "\n";
    close(DAT);


    sub email
    {
    my $subject = $_[0];
    my $content = $_[1];
    my $to = $_[2];
    my $cc = $_[3];
    my $from = $_[4];

    open (MAIL, '| /usr/sbin/sendmail -t s-oi');

    print MAIL "To: $to\n";
    print MAIL "Cc: $cc\n";
    print MAIL "Bcc:\n";
    print MAIL "From: $from\n";
    print MAIL "Subject: $subject\n\n";

    print MAIL "$content";

    close MAIL || warn "Error closing mail: $!";
    }

    # Pass an id space seperated ids or array of IDs and return associated email address(es)
    sub getEmailAddresses
    {
    my @ids=@_;
    my @emailaddresses;
    foreach my $id1 (@ids)
    {
    my @ids1 = split(/ /,$id1);
    foreach my $id (@ids1)
    {
    my $emailaddress=`./address_lookup.pl $id`;

    if(!($emailaddress =~ m/No entry found/)) # skip dud IDs
    {
    print $emailaddress . "\n";
    chomp($emailaddress);
    push (@emailaddresses,$emailaddress);
    }
    }
    }
    return "@emailaddresses ";
    }

    sub prepareEmail
    {
    #
    #
    #here i get information from the database related to the personal id/area/forum
    #i'm leaving it out as it's not important (for my question) and it works.
    #
    #

    #use getEmailaddresses to convert the id to an email address
    my $to = getEmailAddresses($personalid);
    my $from = "blah\@blah.com";
    my $cc = "";
    my $subject = "$personalarea $personalforum individual list.";

    #if data was found then send email
    if($content ne "")
    {
    email($subject, $content, $to, $cc, $from);
    }
    }

    prepareEmail();
    [/php]i also have the parameters outputted to a file on the server to see what is correctly being passed across.
    if i'm using this

    [php]
    #these are the variables taken from the webpage.
    my $personalarea=$page->param('personalarea');
    my $personalforum=$page->param('personalforum');
    my $personalid=$page->param('personalid');
    [/php]my script fails.

    if i use these
    [php]
    my $personalid = "jsmith";
    my $personalarea = "finance";
    my $personalforum = "test";
    [/php]the params.txt file is created.

    and here is how i call the script through my ajax

    [php]
    function personalemail()
    {
    //have getter functions to grab the fields in the form
    var personalarea = getPersonalAreaValue();
    var personalforum = getPersonalForumValue();
    var personalid = document.getElementById("personalid").value;

    var script3='../cgi-bin/scripts/emailtest.pl';

    var params='personalarea='+personalarea+'&personalforum='+personalforum+'&personalid='+personalid;
    var request3 = getHTTPObject();

    request3.open("POST", script3, true);
    request3.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    request3.send(params);


    return false;
    }
    [/php]

    i know it's probably something so miniscule that i'm overlooking.


Comments

  • Registered Users Posts: 26,579 ✭✭✭✭Creamy Goodness


    found the problem, and it was simple, and it always happens when i post questions on boards :p.

    the files needed to have extensions .cgi and not .pl strange but emails are now flying around so.


Advertisement