How to silently "post" from inside a form?

Discussion in 'PHP' started by chackett, Nov 2, 2007.

  1. #1
    Hia,

    I have a form that collects a couple pieces of information including name and email. This form points to a script called collect.php. In it, I generate an email and dynamic url and send it to the new subscriber.

    What I want to do, is to automatically subscribe this person to my autoresponder as well using a form post (as opposed to email subscription). How can I generate a "post" statement from inside collect.php that will send the subscription data to the autoresponder form?

    Thanks!
    Chris
     
    chackett, Nov 2, 2007 IP
  2. Oli3L

    Oli3L Active Member

    Messages:
    207
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    70
    #2
    I didn't get what you want, you want the form to auto-submit?
     
    Oli3L, Nov 2, 2007 IP
  3. ziogas

    ziogas Peon

    Messages:
    32
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    ziogas, Nov 2, 2007 IP
  4. chackett

    chackett Peon

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hrmm .. here's maybe a little clarification on what I'm hoping to accomplish :) I'm a total retard when it comes to programming by the way, which I'm sure is totally obvious from this question and the following example :)

    
    $url2post="http://www.letterlive.com/cgi-bin/arp3/arp3-formcapture.pl?first_name=".$POST['firstname']."&lastname=".$POST['lastname']."&email=".$POST['email']."&id=10"
    $response = fopen($url2post, "r");
    
    Code (markup):
    Basically process goes like this:

    1. User comes to my landing page, completes a signup form and clicks submit
    2. Signup form parses input, generates and send an email to the person.
    3. signup form posts information to the autoresponder signup form.
    4. User gets "Thank You" page.

    Thanks all!
     
    chackett, Nov 2, 2007 IP
  5. brendandonhue

    brendandonhue Peon

    Messages:
    45
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You can use cURL to send POST data from your PHP script.
    Check out the manual page:
    http://php.net/curl

    You'll specifically need to use the "CURLOPT_POST" and "CURLOPT_POSTFIELDS" options:
    http://php.net/curl_setopt

    If your server doesn't have cURL support, you can do the POST yourself using fwrite().
     
    brendandonhue, Nov 2, 2007 IP
  6. exodus

    exodus Well-Known Member

    Messages:
    1,900
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    165
    #6
    AJax is best for this type of thing you are looking to do. When a user clicks the submit set up a javascript function to handle the input then to redirect them to the thank you page. The php will be called using a ajax function. If you need help in processing the form or sending the email then ask, but post in the ajax/javascript area for help over the "silent" posting of information to a php file. :)
     
    exodus, Nov 2, 2007 IP
  7. chackett

    chackett Peon

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks for the help. The only reason I want it to be "silent" is so that the user doesn't have to enter their information twice. I'm not trying to be deceitful in any way.

    I wonder if it would be easier for me to have the autoresponder somehow code the URL and email it, instead of having the formmail script dynamically print the url and then do the subscribe. I bet I can pass a custom field to the autoresponder (ie, link).

    Thanks for all the suggestions and help!!

    Chris
     
    chackett, Nov 2, 2007 IP
  8. QuattroPorte

    QuattroPorte Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Untested but should work.
    No need for cURL or Ajax; keeps it simple, uses plain PHP.
    And it's transparent :)


    
        // Post parameters
        $url2post   = "http://www.letterlive.com";
        $req = "?first_name=".$POST['firstname']."&lastname=".$POST['lastname']."&email=".$POST['email']."&id=10";
        // Build the headers
        $header  = "POST /cgi-bin/arp3/arp3-formcapture.pl HTTP/1.0\r\n";
        $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
        $header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
        // Connect to server
        $fp = fsockopen ($url2post, 80, $errno, $errstr, 30);
        if (!$fp) {
            // Your optional error handler when the connection fails goes here
        }else{
            // Fire off the post
            fputs ($fp, $header . $req);
            // read the response if you want to
            $res = '';
            $headerdone = false;
            while (!feof($fp)) {
                $line = @fgets ($fp, 1024);
                if (strcmp($line, "\r\n") == 0) {
                    // read the header
                    $headerdone = true;
                }elseif ($headerdone) {
                    $res .= $line;
                }
            }
            // optionally parse the response data
            $lines = explode("\n", $res);
            // etc. etc.......        
    }
    
    PHP:

    have fun,
    your php/perl maestro at turborespond.com
     
    QuattroPorte, Nov 3, 2007 IP
  9. exodus

    exodus Well-Known Member

    Messages:
    1,900
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    165
    #9
    QuattroPorte: Why work with sockets? The php fsocket open methods are slow compared to the cUrl ways of doing it. The reason for the suggestion of AJax is they want the information to be saved without having the user to press the submit button themself. Silently save it without them knowing it is saving what they type. At least that is what I understand they want out of it.
     
    exodus, Nov 3, 2007 IP
  10. Krownet

    Krownet Peon

    Messages:
    42
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Exodus,

    What it looks like is:

    User Fills in form at formA.php
    User submits form at formA.php
    Data Sent to collect.php
    Collect.php Works with data (sends email)
    Collect.php posts information to formB.php

    QuattroPorte is on the right track, but doing it using sockets is a bit of overkill. Your just doing what cUrl already does. Granted, cUrl assumes that the server has it installed, but it is less overhead and neater code to use it over sockets.

    www.php.net/curl

    Cheers,
     
    Krownet, Nov 4, 2007 IP
  11. QuattroPorte

    QuattroPorte Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    If you have cURL installed on your box, fine; no need to reinvent the wheel.
    On the other hand, the performace hit using sockets vs cURL is minimal at worst and insignificant most of the time in an application such as this.
    The cUrl library does have problems dealing with certain kinds of servers.
    No portability issues using the socket approach.
     
    QuattroPorte, Nov 4, 2007 IP