PHP - Validform Builder Help
I am setting up a site that is using MODx 2.2 Revolution. I had been using ValidForm Builder as my contact form generator and have used it for a while and really like it. One of the things I liked about MODx is that I was not tied to strictly using their plugins and so forth and that its generally pretty easy to integrate php code into the site via what they call snippets. Anyway, Im sure many here are familiar with MODx.
However, the issue I am having is getting ValidForm to work in MODx. When you make a 'snippet' in MODx, it has to have a return. I have posted the code for a sample form below from the ValidForm Builder website. I created a snippet and added the below code to it to test things out. Knowing that the snipped had to have a return, I tried return ($strOutput);. That generates the form on the page, but it doesnt actuall process and send the form. The validation all works, but nothing else. So Im not sure if Im doing things wrong or what. On static pages you simply use: Code: [Select] <?php echo $strOutput; ?> If I do that only, the form doesnt show up. So ultimately I guess Im trying to figure out how one would write a 'return' into the code so that it works. I tried putting the return after the closing bracket, but Im not sure that is the correct way to do it. Does anyone have any ideas or input? I know MODx has the FormIt extension, but I really like ValidForm and would love to get it to work since, at least on static pages, it is easy to work with and set up. I need to redirect, back to the home page, after the form is submitted, but MODx handles that a little differently and I will have to figure that out after I get the form working. Thanks in advance for any help. Code: [Select] <?php //*** Include the library. require_once("libraries/ValidForm/class.validform.php"); //*** Create an instance of the form. $objForm = new ValidForm("contactForm", "Required fields are printed in bold."); //*** Add the Name field. $objForm->addField("name", "Your name", VFORM_STRING, array( "maxLength" => 255, "required" => TRUE ), array( "maxLength" => "Your input is too long. A maximum of %s characters is OK.", "required" => "This field is required.", "type" => "Enter only letters and spaces." ) ); //*** Add the Email field. $objForm->addField("email", "Email address", VFORM_EMAIL, array( "maxLength" => 255, "required" => TRUE ), array( "maxLength" => "Your input is too long. A maximum of %s characters is OK.", "required" => "This field is required.", "type" => "Use the format name@domain.com" ), array( "tip" => "name@domain.com" ) ); //*** Add the Remarks field. $objForm->addField("remarks", "Remarks", VFORM_TEXT, array( "maxLength" => 2000 ), array( "maxLength" => "Your input is too long. A maximum of %s characters is OK.", "type" => "Enter only characters, punctuation, numbers and spaces" ) ); //*** Set the general alert. $objForm->setMainAlert("One or more errors occurred. Check the marked fields and try again."); //*** Set the label of the submit button. $objForm->setSubmitLabel("Send"); //*** Default output. $strOutput = ""; //*** Check the form. if ($objForm->isSubmitted() && $objForm->isValid()) { //*** HTML body of the email. $strMessage = "<html><head><title></title></head><body>"; $strMessage .= $objForm->valuesAsHtml(); $strMessage .= "</body></html>"; //*** Mail headers. $strHeaders = "MIME-Version: 1.0\r\n"; $strHeaders .= "Content-type: text/html; charset=utf-8\r\n"; $strHeaders .= "From: Awesome website <form@awesomesite.com>\r\n"; //*** Send the email. mail("owner@awesomesite.com", "Contact form submitted", $strMessage, $strHeaders); //*** Set the output to a friendly thank you note. $strOutput = "Thank you for your interest. We will contact you as soon as possible."; } else { //*** The form has not been submitted or is not valid. $strOutput = $objForm->toHtml(); } ?> Similar TutorialsHave you guys ever build a smart queries for users too generate reports basically on anything they may want. I am looking on making something very user friendly. I do not have any ideas where to start
Hi guys, I'm having a bit of PHP problems trying to combine two bits of code which is definitely possible as it's simple if statements, but I'm a bit confused. Not sure if you guys are familiar, but there's a PHP Form Builder Class part of the Google Code Hosting Project. There's two forms I'm playing with one 'Google Spreadsheets' form which allows the data submitted via a form to be entered in a Google Spreadsheet, and one which sends the data to an email address. http://www.imavex.com/php-form-builder-class/examples/email.php http://www.imavex.com/php-form-builder-class/examples/google-spreadsheets.php I have these working separately, but ideally want I need is to be able to combine them both, per se, and allow the data submitted to go to both the spreadsheet and the email address defined. The code for the email is: Code: [Select] <?php error_reporting(E_ALL); session_start(); include("../class.form.php"); if(isset($_POST["cmd"]) && in_array($_POST["cmd"], array("submit_0"))) { $form = new form("email_" . substr($_POST["cmd"], -1)); if($form->validate()) { $result = $form->email("my_username", "my_password", array( "to" => "my_recipient(s)", "subject" => "my_subject", "from" => "my_from", "replyto" => "replyto", "cc" => "my_cc", "bcc" => "my_bcc", "preHTML" => "my_prehtml", "postHTML" => "my_posthtml", "css" => '<style type="text/css">...</style>', "cssFile" => "my_css.css or http://www.my_domain.com/my_css.css", "textonly" => "true/false" )); if($result) header("Location: email.php?errormsg_" . substr($_POST["cmd"], -1) . "=" . urlencode("Congratulations! The information you enter has been emailed from your Google Gmail account.")); else header("Location: email.php?errormsg_" . substr($_POST["cmd"], -1) . "=" . urlencode("Oops! The following error has occurred while sending information from your Google Gmail account. " . $form->getEmailError())); } else header("Location: email.php"); exit(); } elseif(!isset($_GET["cmd"]) && !isset($_POST["cmd"])) { $title = "Email w/PHPMailer + Google's Gmail Service"; include("../header.php"); ?> <p><b>Email w/Google's Gmail Service</b> - This project's email function provides the ability to email a form's submitted data using PHPMailer and Google's Gmail service. This function has four parameters as seen below.</p> <ul style="margin: 0;"> <li>Google Account Email Address - Email address (including domain) of your Google Gmail account.</li> <li>Google Account Password - Password of your Google Gmail account.</li> <li>Additional Parameters (optional) - This parameter allows you to set various email settings through an associative array of key/value pairs. Available settings are provided below. <ul style="margin: 0;"> <li>to - Sets the email's to address. If blank, this parameter will be set to the Google Gmail account email address used in the first parameter.</li> <li>subject - Sets the email's subject.</li> <li>from - Sets the email's from address. If blank, this parameter will be set to the Google Gmail account email address used in the first parameter.</li> <li>replyto - Sets the email's reply to address. If empty, the from address will be used.</li> <li>cc - Sets the email's CC address.</li> <li>bcc - Sets the email's BCC address.</li> <li>preHTML - Allows you to prepend html content above the form's submitted data.</li> <li>postHTML - Allows you to append html content below the form's submitted data.</li> <li>textonly - Sends text-only version of the form's submitted data. By default, the email function will send an email containing both an html and text version.</li> <li>css - Gives you the ability to style the html email as needed. This parameter should be passed as a string beginning with <style type="text/css"> and ending with </style></li> <li>cssFile - Gives you the ability to style the html email as needed by specifying a css include file.</li> </ul> </li> </ul> <p>Before getting started, you'll want to review the checklist of information provided below to ensure you have a good understanding on how this functionality works.</p> <ol style="margin: 0;"> <li>You'll need a Google Gmail account. If you don't have one, you can create one by clicking the "Create an account" link at <a href="http://mail.google.com">http://mail.google.com</a>.</li> <li>to, replyto, cc, and bcc can contain multiple email addresses - just separate them with commas.</li> <li>to, from, replyto, cc, bcc can contain email addresses formatted as either "my@email.com" or "My Email <my@email.com>"</li> <li>Within the email function, a call is made to another public function - getEmail - to get the email's html/text content. If you already have an existing system in place for sending email, you can use this function instead of the project's email function to build a string containing an html/text representation of the form's submitted data. By default, this function will return html, but you can pass true as the first and only parameter to return text.</li> </ol> <p>In the php source code of this example file, you'll see that the email function call currently contains demo authentication/email settings ("my_email", "my_password", etc). You'll want to replace these with your information. Another important thing to note is that the various email settings that can be applied through the email function's fourth parameter are optional. In the php source code of this example file, you'll find all of them listed for reference in the email function call. Feel free to include as many or few as needed.</p> <?php $form = new form("email_0"); $form->setAttributes(array( "map" => array(2, 2, 1, 3), "width" => 500 )); if(!empty($_GET["errormsg_0"])) $form->errorMsg = filter_var(stripslashes($_GET["errormsg_0"]), FILTER_SANITIZE_SPECIAL_CHARS); $form->addHidden("cmd", "submit_0"); $form->addTextbox("First Name:", "FName"); $form->addTextbox("Last Name:", "LName"); $form->addEmail("Email Address:", "Email"); $form->addTextbox("Phone Number:", "Phone"); $form->addTextbox("Address:", "Address"); $form->addTextbox("City:", "City"); $form->addState("State:", "State"); $form->addTextbox("Zip Code:", "Zip"); $form->addButton(); $form->render(); include("../footer.php"); } ?> And for the spreadsheet: Code: [Select] <?php error_reporting(E_ALL); session_start(); include("../class.form.php"); if(isset($_POST["cmd"]) && in_array($_POST["cmd"], array("submit_0"))) { $form = new form("googlespreadsheets_" . substr($_POST["cmd"], -1)); if($form->validate()) { if($form->sendToGoogleSpreadsheet("my_email", "my_password", "my_spreadsheet_title", "(optional) my_worksheet_title")) header("Location: google-spreadsheets.php?errormsg_" . substr($_POST["cmd"], -1) . "=" . urlencode("Congratulations! The information you enter has been sent your Google Docs spreadsheet.")); else header("Location: google-spreadsheets.php?errormsg_" . substr($_POST["cmd"], -1) . "=" . urlencode("Oops! The following error has occurred while sending information to your Google Docs spreadsheet. " . $form->getGoogleSpreadsheetError())); } else header("Location: google-spreadsheets.php"); exit(); } elseif(!isset($_GET["cmd"]) && !isset($_POST["cmd"])) { $title = "Google Spreadsheets"; include("../header.php"); ?> <p><b>Google Spreadsheets</b> - This project's sendToGoogleSpreadsheet function provides the ability to send a form's submitted data direclty to a Google Docs spreadsheet using the Google Spreadsheet API. This function has four parameters as seen below.</p> <ul style="margin: 0;"> <li>Google Account Email Address - Email address (including domain) of your Google account.</li> <li>Google Account Password - Password of your Google account.</li> <li>Google Docs Spreadsheet - The title of the spreadsheet where you'd like the form's submitted data to be sent.</li> <li>Worksheet (optional) - The title of the worksheet to be used within the specified spreadsheet. This parameter will default to the the spreadsheet's first worksheet.</li> </ul> <p>Before getting started, you'll want to review the checklist of information provided below to ensure you have a good understanding on how this functionality works.</p> <ol style="margin: 0;"> <li>You'll need a Google account. If you don't have one, you can create one by clicking the "Create an account now" link at <a href="http://docs.google.com">http://docs.google.com</a>.</li> <li>If the spreadsheet title you specify in the sendToGoogleSpreadsheet function does not exist, a new spreadsheet will be created for you with the appropriate title and column headers.</li> <li>If you're creating your spreadsheet manually through the Google Docs GUI, an important thing to keep in mind is that Google will treat the initial row of cells as column identifiers. These column identifiers must match an element's label used in your form ("First Name:", "Last Name:", etc), which enables the form's submitted data to be correctly placed within the appropriate column. If the spreadsheet you specify in the sendToGoogleSpreadsheet function exists but has no inital row of column headers, your form's data will not populated upon submission. Your spreadsheet does not need to contain a column for every element used in the form - data for those elements that are not included will just not be collected. Likewise, your spreadsheet can contain column identifiers that don't match an element's label used in the form - data for those columns will be left blank.</li> <li>Elements of type hidden, captcha, button, html, and htmlexternal will not be included in the information that is sent to your Google spreadsheet.</li> <li>The "ignoreGSSend" element attribute can be applied to form elements that you do not want to be send to your Google Docs spreadsheet. The hidden field "cmd" has this attribute set in the form below.</li> <li>If you're populating an existing spreadsheet in your Google Docs account, you can store the web server's date/time at the moment the form's data is submitted by adding "Timestamp" as a column header.</li> </ol> <p>In the php source code of this example file, you'll see that the sendToGoogleSpreadsheet function call currently contains demo authentication/spreadshet settings ("my_email", "my_password", etc). You'll want to replace these with your information.</p> <?php $form = new form("googlespreadsheets_0"); $form->setAttributes(array( "map" => array(2, 2, 1, 3), "width" => 500 )); if(!empty($_GET["errormsg_0"])) $form->errorMsg = filter_var(stripslashes($_GET["errormsg_0"]), FILTER_SANITIZE_SPECIAL_CHARS); $form->addHidden("cmd", "submit_0"); $form->addTextbox("First Name:", "FName"); $form->addTextbox("Last Name:", "LName"); $form->addEmail("Email Address:", "Email"); $form->addTextbox("Phone Number:", "Phone"); $form->addTextbox("Address:", "Address"); $form->addTextbox("City:", "City"); $form->addState("State:", "State"); $form->addTextbox("Zip Code:", "Zip"); $form->addButton(); $form->render(); include("../footer.php"); } ?> I have managed to speak to one of the admins, who can't help me further, but states: 'My recommendation would be to use both the $form->email() and $form->sendToGoogleSpreadsheet() methods after your form has been submitted. It's not a "one or the other" type of scenario.' Could you help me out and show me how this would be possible which in turn I can learn from? Thanks, and sorry for the long post but felt it was better as a code include rather than an attachment? Richard In attachment is the code I generated from Flex Builder: My issue is that the UTF8 (coding of the MySQL database) isn't correctly interpreted, the solution is to use : $stmt = mysqli_prepare($this->connection, "SET NAMES UTF8;"); But I don't know how to enter it in this syntax? It is I think the: $stmt = mysqli_prepare($this->connection, "SELECT * from $this->tablename"); that has to be modified, but how? Really searching a long time on the correct syntax, but without any result... Please help, thanks a lot Wimmerke |