I'm having some trouble implementing a bit of custom functionality in the Contact Form 7 plugin for Wordpress.
What I want to do is pretty straightforward. The contact form in question is a normal contact inquiry form, so I need to retain the usual functionality (mailing the data). However I also have a checkbox on the form that allows the sender to choose whether to subscribe to the client's mailing list in addition to mailing the contact inquiry.
The client uses Bronto for their mass mailing (similar to CC or Mailchimp). Bronto has a "direct add" feature (more info here) that allows you to send parameters to add contacts to the Bronto account by embedding an image whose url contains the requisite parameters (email address, list to subscribe to, etc).
I can construct the image url with the contact form parameters no problem, but actually getting the image request sent is a different matter. I am over my head both in PHP and JS here and not sure what course to take.
Currently I'm using the wpcf7_before_send_mail php hook built into CF7 and this appears to allow me to collect the form data and build the URL. However, since the plugin uses AJAX and doesn't actually redirect to another page on form submission it seems I can't successfully use any kind of php output (echo, alert, even error_log), presumably because the server doesn't know what it's supposed to write to.
In functions.php:
add_action( 'wpcf7_before_send_mail', 'bronto_contact_add' );
function bronto_contact_add( $cf7 ) {
$emailcf = $cf7->posted_data['email'];
echo $emailcf;
}
This is just a test to see if echo works - it does not. Instead the form just hangs on submission and I see the rotating loading gif forever.
What I need to do is build the image url using parameters from the cf7 object (which I can do no problem) and then somehow send that image request to the Bronto server (this is the part I am confused about). If this was a normal form that redirected to another php page upon submission I would have no problem doing this at all, but it uses AJAX which I don't know much about so I'm pretty lost now.
Can anybody help shed some light on how the best way to accomplish this might be?
If the submit is hanging after you attached your function, at least you know that it had an effect. I'm not terribly familiar with Contact Forms 7, but this is probably not the proper place for an echo, and my guess is that it is hanging because you are writing to the buffer and then it is trying to do a redirect (check your error logs). If you want to see the contents of $cf7, a better way to do it would be:
// first option, using print_r()
error_log(print_r($cf7, true));
// second option, using var_dump() if you need the additional output
ob_start(); // start buffer capture
var_dump($cf7); // dump the values
$contents = ob_get_contents(); // put the buffer into a variable
ob_end_clean(); // end capture
error_log($contents); // log contents of $cf7
The contents of the $cf7 variable will then be in your PHP error log, which will tell you how to access the different components.
I came across your thread while looking for a similar solution to the same problem with CF7 - a hang on submission when trying to pass info to my own database on the back-end for a CRM.
I did not see an answer to this issue anywhere on the web in relation to CF7 integration, so thought I would post here what I found was the problem and how it was fixed. I'm no pro, but the solution works in testing, if anyone has anything to add to this solution please chime in
Basically, if you are using Wordpress and trying to pass the info into a CRM database, I am going to assume your database tables are not on the same database as your Wordpress site database. What you are then trying to do is establish two database connections simultaneously, but the reference ID is being reused for your Wordpress database when trying to connect to your CRM. I found this was the root cause of the hang on submission during testing.
I used a deprecated command from PHP 4 that still works in PHP 5, mysql_connect, :
mysql_connect('localhost', 'root', '', true);
By passing 'true' as your fourth parameter you are able to keep the connection separate from the one running for your Wordpress site. Did this and the CF7 submission doesn't hang, it submits to the CRM and sends it out as an e-mail simultaneously, no problem.
Note too though, if something is wrong with your syntax for the CRM data submission i.e. misnamed variable, etc.. it will also hang. If passing 'true' doesn't work check your code first to make sure it's clean.
If anyone reading this has an equivalent solution for this with 'mysqli' commands I'd be interested to know it, I tried it using mysqli and couldn't get it working.
Is there some reason why you can't just prefix the table names and add them to the same database? It seems that would be a better solution and would work fine with mysqli as opposed to using archaic, insecure drivers.....
I guess, I just don't see the point of using two databases in this case... I would try using one.
Related
I am making a simple form with a submit button inside of it. In order to do this I used a shortcode which returns the form. I would like to prevent the user from submitting the form multiple times. However, the answers I have found tend to rely on disabling the button using javascript in some way. If I understand correctly this has two problems.
If the user stops the page, the button will be permanently disabled.
The client code can be changed manually by the user to send multiple requests even if the button is disabled.
I was wondering if there is some way to handle this on the server side in php?
The main problem I am having is that the shortcode needs to return something. It is possible to store a unique code using a nonce, the database or other methods to uniquely identify the submission. However, I can't find a way to cancel execution before the shortcode starts.
Also, when I was trying things I found that if I sleep the function so I can spam the submit button, only the final shortcode submission is returned. So I can't simply return a working copy of the form either.
It seems this would be a standard problem, so am I missing something?
I have created a page template in WordPress to display a form with action="". Very simply, it will not post any of the form data upon submission. I have tried var_dump($_POST) but it just gives me array(0) {} every single time. I've tried examples provided by some websites for creating a form, but I can't reproduce the same results.
I have thoroughly search for answers to this problem but nothing seems to work. It must be something internal or very advanced. I just want to know why my form data won't post and where it might be going.
Check your data using a non-WP page, meaning put a php file in web-root, and have the form action point to it, such as ...action="/formtest.php", and on that page:
<?php
var_dump($_POST);
?>
This will test if you really aren't getting the POST vars. Then, since you'll 99% likely find they ARE being posted, the likely trouble is that your page IS being redirected somewhere by the theme as mentioned elsewhere (e.g. my theme includes a hook on 'template_redirect'...).
If you are theme savvy and just looking for a quick fix- at the expense of losing it on theme upgrade- you can create an exception just for your temp.php page, but the way that many developers handle it is via Plugins, or handle your form via Ajax. Many others have encountered this same issue, and so, either tacking on code to a Plugin, or performing in-page Ajax (since you are submitting to SELF anyway...).
This page will give you a leg-up on WP Ajax, it's a reputable and well written doc.
In a nutshell.... what i want to do is a install script for my website... but i want everything to be done in 1 page which will connect to another php file that will have all the server side processing code. In order to do that i use JQuery, Ajax & PHP...
Now my question lies in what are the best practices in order to do that. How do I make my server side PHP script to take multiple user inputs that will happen throughout a lengthy installation processes?
I was thinking on using switch statements that will trigger classes & functions and like that being able to access multiple processes (Classes and functions) that will be stored in the same file?... or people normally use other things?? never done this before and couldnt find a straight forward tutorial for it on how is best to design it.
I was thinking on maybe being able to access classes through AJAX in php but that doesn't seem to be possible.... can i trigger functions from AJAX in php?
I really appreciate the help in how normally this is approached
thx
It would be too lengthy to define the whole process in SO in detail. But the basic idea is:
User fills in form clicks NEXT button.
You get all the form values (in JavaScript), post it to a PHP page using AJAX
The PHP page gives you back a flag (SUCCESS/ERROR) [In the background, if it is success, PHP has persisted the data into a database]
If JavaScript receives ERROR, you show user the error message and ask him to correct the form
If java script receives SUCCESS, you call the next PHP script, which generates the next form and sends it to your JavaScript
JavaScript populates this form content within a DIV in your page, effectively wiping out the current form and showing the new form in its place.
..and the whole process restarts from the first step once again for the new form..
Context
I'm working on a project that I'd like to make more dynamic with PHP + AJAX.
I'm using jQuery for the AJAX part because I totally suck in Javascript, and anyway it seems to be worth something.
I reached a point where my application needs to use a form to send a post, and I want to use an ajax call to perform this action. In the page I'd like to send the post, there is also a list of the most recent 15 posts submitted.
First question: Should I just forget about it and use just PHP?
The action
The user writes something in the <textarea></textarea> and clicks on a <a id="myPostSubmit">Submit</a> that is the handler that I'll manage on the jQuery script with something like $("#myPostSubmit").live('click', function() { /* here i make the ajax call */ });. If the post is successfully submitted we are going to do something (I'll talk about it in the next section), either we will alert the user using my showAlert(text) function, that shows a black box for 4 seconds with the text in it.
Second question: Should I manage the click event in any other ways? Should I create a function, such as sendpost(post) and attach it into the HTML onclick="" event?
If the post is successfully sent
I'd open a discussion about 2 options:
We refresh the page [not actually
loading the entire page but making
another ajax call that retrieves the
posts lists and makes disappear the
old one, load the PHP file to
retrieve the new posts (including
the one we just sent), and then make
the post list appear]. Pro: 1) We are sure that what the user is reading after the post list is loaded is the real post sent. So it actually double checks the action. 2) We load also some possible posts sent in the mean while. Cons: 1) We have to create a PHP file that gets the post list template, slicing the template of that page in 2 files. 2) It doesn't really seems that smooth to me.
We just use Javascript to get the post template, add it to the list. Pro: 1) We make it really smooth, without reloading the entire page. 2) We don't need of any PHP file to reload the page. We just use Javascript (jQuery). Cons: 1) How do we get the post html template to add it to the list? 2) How do we get the user (logged) informations without PHP?
Third question: Is it better the 1st or the 2nd solution? Would you provide a even better 3rd solution?
The PHP page
The PHP page that will receive this AJAX call is : ?p=action&a=sendpost. The page require $_POST['myMessage'] to be set and not empty and nothing else. The page itself will get all the user infos from the cookies and will manage to perform the needed query.
The application
It is divided in 3 parts: App, Template, Library. Basically each page of the application has its own .app.php and .tpl.php file.
The .app.php file manages the building
of the basis of the page, using classes
and other stuff from the library. In
our case it retrieves datas from the
database and put them into
variable.
The Template is called at the end of the .app.php file. The .app.php file send to the template the retrieved data and the .tpl.php file outputs them.
The library is used to contain the classes and functions we need in the application file.
Fourth question: Is this a good way of manage my web application?
Edit: What about returning the alert message to the user?
I read about an option, inside $.ajax() that will manage the response on success or in error. The documentation about it is very simple and I didn't get it.
Fifth question: How should I return (from the PHP file) the error
or the success?
First question: Should i just forget about it and use just PHP?
Well, you application will relay on JavaScript if you use ajax, this days i think it just fine ;)
Second question: Should i manage the click event in any other ways? Should i create a function, such as sendpost(post) and attach it into the HTML onclick="" event?
Create a function and bind onclick. Code will be more readable ;)
Third question: Is it better the 1st or the 2nd solution? Would you provide a even better 3rd solution?
My solution: ajax submit the form and on callback insert new comment in to the list or display error message if user can't comment.
Check jQuery serilize() for submitting forms data with ajax.
Fourth question: Is this a good way of manage my web application?
It's just fine ;) When you application get's bigger you will have to redesign it, but don't do it know, do it when current solution becomes to hard to work with.
Read some good book on building MVC framework. And on programming patterns in general.
You seem to be on the right track with everything. There are lot of opinions called "best practices" about how to exactly attach event handlers, how to reload the data on the page and how to organize your application, etc, but I personally would rather build more software instead of worrying about details like that. The details will come to you eventually.
I personally find that updating whole chunks of server-side-rendered HTML on the page is more robust solution, but I have seen people getting excellent results with templates.
I have a form which I'd like to share between 2 different websites. The form is submitted and the data is entered into the database.
Right now each website has its own copy of the script and its own database, so when I want to make updates to the form, I have to make those changes twice. Is there a way to share the form between the 2 websites, yet make it look like its being served by each website like normal. I'm also wondering if it's possible to make all the data go to one database.
The basic options would be...
You could use an html iframe to show the same form on multiple websites.
You could copy the form code between sites
If both websites are on the same server, you may be able to get a php include to include the form (this is possible in some cases even if they are not)
You can certainlly get the database to share information, just ensure the user you use to connect to it is allowed to connect from anywhere - not just localhost and you can connect to the database remotely.
You could include the form inside the other website as an iframe.
There is a short tutorial here on howto do that.
In case the form is displayed inside a whole complex page i recommend placing the form inside its own page and iclude it in both websites using an iframe.
depends what you are looking for, if you use the same process script behind it, do what Mouhannad said, and add a field "return_url" so you come back on the right page.
if it is that you want to send the fields to 2 different locations, create a proxy script and post it by a curl to both locations.
you can simply make both forms (in both websites) point to the same php page (in one of the websites)
<form action="http://www.example.com/action.php" ... >
Then check for the url reference to send the user back to the same page
The user won't feel any difference and you will have the database connection in one place
good luck!
Edit:
I thought your main concerns were the backend code since you mentioned the database. You could use iframes as suggested by the others but I always try to avoid them as much as I can. You can find lots of material online about why you should avoid them.
The other solution is use cURL to get the form's html code from the external page and keep using my above suggestion to change the action url path. This way you have the same code both for the backend and frontend. The downside is that you are making an additional request to get the form's html code which adds to the performance of your website - caching should improve that!
You can use CURL to simulate form submitting on another host, just send POST request with $_POST data.