WordPress.org

Support

Support » Plugins and Hacks » Contact Form 7 » [Resolved] [Plugin: Contact Form 7] PHP after form submit?

[Resolved] [Plugin: Contact Form 7] PHP after form submit?

  • Greetings.

    Is it possible to execute some PHP code after form submission?

    I want to send SMS about this letter.

    Code like:

    <?php
    require_once ‘SMS.class.php’;
    $api = new LittleSMS(‘vasya’, ‘qwerty123’, true);
    $api->sendSMS(‘79631112233’, ‘Бугагашенька!’, ‘vasya’);
    ?>

    http://wordpress.org/extend/plugins/contact-form-7/

Viewing 15 replies - 1 through 15 (of 15 total)
  • Plugin Author Takayuki Miyoshi

    @takayukister

    Add this code into the functions.php in your active theme.

    add_action( 'wpcf7_mail_sent', 'your_wpcf7_mail_sent_function' );
    
    function your_wpcf7_mail_sent_function() {
    	require_once 'SMS.class.php';
    	$api = new LittleSMS('vasya', 'qwerty123', true);
    	$api->sendSMS('79631112233', 'Бугагашенька!', 'vasya');
    }

    Great! It works, but I have 2 more questions now. 🙂

    1. Really I have 2 forms on my site, but I need to send SMS only from 1 of them. Is it possible?

    2. May I use data from form in this function? I get a phone from client and want to get it to make a call…

    Plugin Author Takayuki Miyoshi

    @takayukister

    ok, add a parameter ($contact_form in the sample code below) to the your_wpcf7_mail_sent_function(), then you’ll be able to access the title of the contact form and posted data through the contact form via the parameter.

    add_action( 'wpcf7_mail_sent', 'your_wpcf7_mail_sent_function' );
    
    function your_wpcf7_mail_sent_function( $contact_form ) {
    	$title = $contact_form->title;
    	$posted_data = $contact_form->posted_data;
    
    	if ( 'Your Contact Form Title Here' == $title ) {
    
    		$phone = $posted_data['phone']; // if you have a field with name "phone"
    
    		require_once 'SMS.class.php';
    		$api = new LittleSMS('vasya', 'qwerty123', true);
    		$api->sendSMS($phone, 'Бугагашенька!', 'vasya');
    
    	}
    }

    Thank you! 🙂

    Problem solved. Everything seems to work just perfectly.

    I have defined a function like this to update a global variable and to use that to echo at the bottom of the form (after the CF7 form in PHP). No dice.
    I have put the code in the functions.php of my child-theme.

    1 – How can I check if this functions gets triggered after the send mail ? I just put in an echo but that hangs the send.
    2 – What is the scope of the $contact_form variable ? I get a NULL whatever I try.

    Any suggestions more than welcome.

    Plugin Author Takayuki Miyoshi

    @takayukister

    Simple ‘echo’ don’t work when Ajax submitting is working. The $contact_form variable is local scope as it’s a function parameter as you can see in the above code.

    Thanks for the swift reply.

    1 – okay. Understood.
    2 – Here I mean: I have put the code in the functions.php of my child-theme using the $contact_form as a param as in your example, but then it must be available when called from CF7. And I get null.

    Piet, Tsubo-en

    Plugin Author Takayuki Miyoshi

    @takayukister

    Child-theme’s functions.php is no problem.

    Can I see the code?

    The vars are named in Dutch. As far as I can see this never gets executed. I now wonder should this be executed with the additional setting “on_sent_ok:” ?

    Child functions.php:

    <?php
    add_action( 'wpcf7_mail_sent', 'vhv_kortdag_function' );
    
    function vhv_kortdag_function( $contact_form ) {
     global $kortdag;
     $kortdag ='';
    
     $form_title  = $contact_form->title;   // Used as check only
     $posted_data = $contact_form->posted_data;
     $aankomstdatum = $posted_data['vhv_dat_aankomst'];
     $vandaagdatum = date("d-m-Y");
    
     $vandaag = strtotime($vandaagdatum);
     $aankomst = strtotime($aankomstdatum);
    
    	if ( $aankomst <= ($vandaag + 10) )
            {
             $kortdag = '<span style="color: #ff0000;"><strong>Attentie:</strong> indien uw aankomst binnen 10 dagen vanaf heden valt verzoeken wij u óók even te bellen met het telefoonnummer zoals vermeld op de <a href="//www.vakantiehuisverhuur.eu/contact/"" title=""Ga">contactpagina</a> !</span>';
    	}
    }
    ?>

    In my contact (booking) page I have an include with my globals:
    require_once (ABSPATH . 'wp-content/themes/vhv-eu_child-theme/vhv_include.php');
    AND This:
    $kortdag ='.'; // (global)

    Then at the bottom in my contact-page I use the global var to display the message:
    echo "<br />" . $kortdag;

    The CF7 shortcode is used in PHP as I use the “PHP Execution” plugin to execute in-line PHP. This works all very well to populate the page.
    Si I have a contact form running okay but I am now putting in additional validation and function.

    Example:

    Thanks again,

    Sorry, the echo statement got mutilated but it is okay and works, as: echo “
    ” . $kortdag; (the . as init for the var is on purpose. Although I am also learnig PHP this is all okay !!)

    Thanks to Takayuki Miyoshi !!!
    This function works!

    Hello! How to make calculations on contact form 7? I want to multiply textfield 1 to textfield 2. Can you help me with this? Thanks!

    to stealthkaizer
    To my mind, it is more easy to do this with jquery, not with php. But anyway, your task in not clear to me.

    kg69design, hello! I would like to get the result from two fields in contact form 7. For example, I want to multiply the value inputted on text1 to the value of text2. I will be thankful if you can help me with this. Thanks!

    to stealthkaizer
    And what do you need to do with the result? Just send it or show to user or…?
    It is clear about “multiply”, but other stuff… Give me full task, “the idea”, and, maybe, I can help you.

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘[Resolved] [Plugin: Contact Form 7] PHP after form submit?’ is closed to new replies.
Skip to toolbar