Customize recipient of mailform

Submitted by Steven Heynderickx on 25 Sep 2012 22:17
rating: 2

Description

The script gets info from a userpage on your wikidot site. For example, a category named "siteguru". This page should hold a form with a field named "name" that has the value of the guru's Unix name. For me that would be "steven-heynderickx".

This value will be read and a mail-form-page will be created where this person's name will be rendered into the "to"-field of the mailform.

After this the script redirects to this page… so the form is visible.

You can see this in action at http://a-simple-plan.wikidot.com/send-form-to-list

Sample Code

<?php
//-----------------------------------------------------------------------------------------------
// General settings
//-----------------------------------------------------------------------------------------------
$page=$_GET['page']; //get pagenamefrom URL
$key="YOUR API KEY";
$username="YOUR USER NAME";
$site="YOUR SITE NAME";
 
$ContentArray=getContentArray($site,$page,$username,$key); //get email
$title="Form will be send to ".$ContentArray["name"]; // build title
$mailFormContent=getPage($site,"mailform:_template","","","",$username,$key); //get mailform_template page info
$mailForm=$mailFormContent["content"]; // get mailform_template content
$mailForm=str_replace("##USERNAME##",$ContentArray["name"],$mailForm);// adjust mailform_template
createPage($site,"form".$page,$title,$mailForm,$username,$key);// create new mailpage
wait($site,"form".$page,10); // redirect after 10 miliseconds
 
/*
If you wish to do some checking, uncomment this block and adjust waiting time
print ("<b>Pagename</b><br>http://$site.wikidot.com/$page");
print ("<hr><b>Email</b><br>".$ContentArray["email"]);
print ("<hr><b>Title</b><br>".$title);
print ("<hr><b>MailForm _template</b><br>".$mailForm);
print ("<hr><b>Adjusted MailForm _template</b><br>".$mailForm);
print ("<hr><b>Mailform creation</b><br>OK");
*/
 
//-----------------------------------------------
// FUNCTIONS
//-----------------------------------------------
//----------------------------------------------------------------------------------------
    function getContentArray($site,$page,$username,$key)  // Gets form-page-content and converts to Array                      
//----------------------------------------------------------------------------------------
    {                                                                         
    $returnarray=getPage($site,$page,$username,$key);
    $content = $returnarray[content]; //copies string to a new string var
    $newarray = array();
    $newarray["parent_fullname"] = $returnarray[parent_fullname];
    $stringar = explode("\n",$content ); //split the content-string into an array of strings 
    foreach($stringar as $data)
        { 
        $data_ar = explode(":", $data, 2); //split the strings into form fields and form data
        $key = $data_ar[0];
        $cont = trim(trim($data_ar[1]),"'\""); //trimming off quotes and spaces
        if($key == '' && $$cont == '') ; //skip empty strings
        else $newarray[$key] = $cont;
        }
    return $newarray;
    }
//----------------------------------------------------------------------------------------
    function getPage($site,$page,$username,$key)  // API methode pages.get_one                       
//----------------------------------------------------------------------------------------
    {                                                                         
    $requestarray=array(    
            'site'=>$site,                                    
            'page'=>$page);                                   
 
    $request = xmlrpc_encode_request("pages.get_one",$requestarray);          
    $context = stream_context_create(array('http' => array(                   
            'method' => "POST",                                               
            'header' => "Content-Type: text/xml",                             
            'content' => $request                                             
            )));                                                              
    $file = file_get_contents("https://".$username.":".$key.                  
    "@www.wikidot.com/xml-rpc-api.php", false, $context);                     
    $response = xmlrpc_decode($file);                                         
    if ($response && xmlrpc_is_fault($response))                              
        {                                                                 
        return ($response);
        }                                                                 
    else                                                                      
        {                                                                 
        return ($response);                                               
        }                                                                 
    } 
 
//----------------------------------------------------------------------------------------
    function createPage($site,$page,$title,$content,$username,$key)                     //
//----------------------------------------------------------------------------------------
        {
        $requestarray=array(    
                'site'=>$site,
                'title'=>$title,
                'content'=>$content,//
                'page'=>$page
               );                                                                       //
                                                                                        //
        $request = xmlrpc_encode_request("pages.save_one",$requestarray);               //
        $context = stream_context_create(array('http' => array(                         //
                'method' => "POST",                                                     //
                'header' => "Content-Type: text/xml",                                   //
                'content' => $request                                                   //
                )));                                                                    //
                                                                                        //
        $file = file_get_contents("https://".$username.":".$key.                        //
        "@www.wikidot.com/xml-rpc-api.php", false, $context);                           //                        
        $response = xmlrpc_decode($file);                                               //
        if ($response && xmlrpc_is_fault($response))                                    //
                {                                                                       //
                Print("Could not save page ".$page);                                    //
                trigger_error("Wikidot XML-RPC Error:                                   //
                $response[faultString] ($response[faultCode])");                        //
                }                                                                       //
    }                                                                                   //
//----------------------------------------------------------------------------------------
    function wait($site,$page,$time)
//----------------------------------------------------------------------------------------
        {
        print("
<script>
function redirect()
        {top.location.href=\"http://".$site.".wikidot.com/".$page."\";}
setTimeout(\"redirect()\",",$time.");
</script>"
        );
        }
//----------------------------------------------------------------------------------------
?>

Live Demo

http://a-simple-plan.wikidot.com/send-form-to-list

Related Files

No files attached to this page.


Rate this Code

rating: +2+x

Discuss this Code

Add a New Comment
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License