SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    designer
    Join Date
    Dec 2004
    Location
    Over the hill and through the woods...
    Posts
    306
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    AJAX Contact form

    So, I have a basic php contact form:

    contact.php:
    Code:
    <form onSubmit="return isValid()" action="send.php" method="post" name="emailForm">
    Name: <input name="name" type="text" /><br /><br />
    Email: <input name="email" type="text" /><br /><br />
    Message:<br /><textarea name="comments" cols="75" rows="7" wrap="virtual"></textarea><br /><br />
    <input name="submit" type="submit" value="Send" />
    </form>
    send.php:
    PHP Code:
    <?php

    ## GRAB THE VARIABLES

    $namez $_POST['name'];
    $emailz $_POST['email'];
    $commentz $_POST['comments'];
    $me me@gmail.com";
    $subject = "A message from " . $emailz;

    ## HEADERS

    $headers = "MIME-Version1.0\r\n" .
       "
    Content-typetext/htmlcharset=iso-8859-1\r\n" .
       "
    From: \"" $namez "\" <" $emailz ">\r\n" .
       
    "Date: " date("r") . "\r\n";

    $message "";

    ## START THE (HTML) MESSAGE

    $message .= "<html>";
    $message .= "<head></head>";
    $message .= "<body>";


    ## CONTENT

    $message .= "<small>Sent by " $namez " <" $emailz "> on " date("r") . "</small><br><br>\r\n";
    $message .= $commentz "\r\n";

    ## END THE (HTML) MESSAGE

    $message .= "</body>";
    $message .= "</html>";

    ## SEND

    ini_set(sendmail_from,'$emailz');  // the INI lines are to force the From Address to be used !
        
    mail($me$subject$message$headers);
    ini_restore(sendmail_from); 

    ## REDIRECT TO SUCCESS PAGE

    if (mail){
      print 
    "<meta http-equiv=\"refresh\" content=\"0;URL=../contact?s=1\">";
    }
    else{
      print 
    "<meta http-equiv=\"refresh\" content=\"0;URL=../contact?s=2\">";
    }

    ?>
    Could anyone tell me how to convert this form into ajax? I want to be able to allow people to send messages without reloading the page. I know how to do a basic XMLHttpRequest, but I'm not really sure how to make it send without reloading the page. Could anyone explain to me how to use the XMLHttpRequest command properly? Thanks!

  2. #2
    SitePoint Member
    Join Date
    Feb 2006
    Posts
    0
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This works as long as you have the form processing PHP file on your server.

    Code:
    function submitForm() {
    	//Build URL string
    	var url="subscribetest2.php?";
    	var subdata = "PHPSESSID=9fdc0db03d93038a733f858cf1ff0589&";
        subdata += "meta_web_form_id=1284874142&";
        subdata += "meta_split_id=&";
        subdata += "unit=healingproducts&";
        subdata += "redirect=http://www.healingproducts.com/newslettersubscribe_thanks.htm&";
        subdata += "meta_adtracking=404error&";
        subdata += "meta_message=1&";
        subdata += "meta_required=from&";
        subdata += "meta_forward_vars=0&";
    	subdata += "name=" + document.subform.name.value + "&from=" + document.subform.from.value;
    
    	url = url + subdata;
    	// XMLHTTPRequest object opens a server connection
    	myRequest.open("POST", url, true);
    	//Set Callback function
    	myRequest.onreadystatechange = formReturn;
    	//Tell server it is a post
    	myRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    	myRequest.send(url);
    }
    function formReturn() {
        // we are only interested in readyState of 4,
        // i.e. "completed"
        if(myRequest.readyState == 4) {
            // if server HTTP response is "OK"
            if(myRequest.status == 200) {
    			//Get any return value is needed and do anything else you want
    			var responseTxt = myRequest.responseText;
    		console.log(myRequest.responseText);
            } else {
            // issue an error message for any other HTTP response
                alert("An error has occurred: " + myRequest.statusText);
            }
       }
    }
    What I am having trouble doing is using AJAX with an external autoresponser service like Aweber. I get an error trying to call a script that exists on another server.

    Has anyone else been able to do this?

    I want to send the subscribe to aweber with a reload and then when the callback function is called, make the subscriber form go away.



    Mark McCoid


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •