Joomla module submit form can't access database - forms

I've been searching the net for an answer to my question but I just can't seem to find one, even though it's probably pretty simple.
I have a joomla module that signs up users to a newsletter, when clicking the submit button I navigate to submitsignup.php file. I do this using the form action value like so:
form action="modules/mod_cmsnewslettersignup/otherfiles/submitsignup.php" method="post" id="subForm"
Within this submitsignup.php file I can not access any joomla classes, such as:
$db = JFactory::getDBO();
I know that I can't access any joomla classes because I made direct access to the submitsignup.php file, but I was wondering how do I access this file so that I can have access to all the Joomla classes?
Thanks.

If you are reloading the page when you submit the form, then there is a simple solution that solves both the problem of using a direct URL and of having to load the Joomla framework in that file. Basically change your module code to something like this -
if ($_POST["formsubmitted"]){
the code you run when the form is submitted
echo success or failure message
} else {
the code you run to display the form
<form action="<?php echo JURI::current(); ?>" method="post">
<input type="hidden" value="true" name="formsubmitted">
}
Basically, you submit the form to the page that displays it. Then in your module you add a hook to either process the submitted form or display the form depending on what you find in $_POST. You can add some simple security to make sure that the form is being submitted from your site.

Related

ColdFusion - Form variables empty after form submit

My question references the following question/answer provided in this stackoverflow post: Form Variables are not showing up after form submit. ColdFusion
I wanted to comment in the above referenced post, but I don't have enough reputation points. I see the answer by Samuel Dealey above indicating that using a cflocation or location.replace() could result in Form variables not showing up. That is exactly what is happening in my scenario.
I have a simple registration form submission. Upon form submission the form data is sent to a page named addCampaign.cfm. addCampaign.cfm contains code that will write the registration data to the database. After writing the registration info to database, I verify that info was written to database. At that point I then redirect the user to a specific page if registration was successful, and if not successful then redirect back to the registration form page with an appropriate error message.
I have tried using both for the redirect, and have attempted using javascript location.replace(), both result in the same problem.
The issue I am running into is that:
1) The redirect never occurs
2) I am using to display the struct, but it lists it as empty
3) I have removed the cflocation and location.replace() and can verify that my form elements do exist in the form struct.
4) The form data is being written to the database, which is very strange, considering that the form struct is being displayed as empty.
I don't understand why the form struct is empty when the data is being written to the database, and furthermore I don't know why the redirect does not work. Can anybody provide some clarification on why this would be happening?
Consider this code on a single file
<cfif cgi.request_method EQ "post">
<cfdump var="#form#">
<!--- More importantly, DB inserts --->
</cfif>
<form method="post" action="?">
<!--- Lots of other fields go here too --->
<input type="submit" name="btnSubmit" id="btnSubmit" value="OK" />
</form>
If you do it this way, you don't have to worry about pushing data over redirect of some sort. You are already on the page you want. action="?" basically means submit to the same field as I am already on. Note that the file's behavior is different on a GET vs POST.

Send email when submit button is pressed

I have a really simple form that allows a user to input an email address here:
<form method="post" action="http://www.mydomain.com/page2/">
<input type="email" name="email">
<input type="submit" value="Submit">
</form>
This works correctly and it takes the visitor to www.mydomain.com/page2 when the submit button is clicked.
I am trying to get it to email me this input email address also when the submit button is clicked. I understand how to email using PHP but can the action have two urls?
Or is there a simpler way of doing this?
On /page2/ access the email in the global variable $_POST['email']. And then you can send it to yourself with PHP mail(). Example:
mail('myemail#domain.com', 'Someone submitted my form', 'Their email was: ' . $_POST['email']);
If you are stuck somewhere else, let me know and I can update the answer.
Once a form is submitted, you are no longer on that page. You've navigated away.
The other way you can do this is submit the first action via AJAX, then submit the form naturally to the second destination. I would suggest using jQuery to make your AJAX calls since most of the AJAX code is already there for you to use.
Another option is to have page2 be a php script, and have it perform the two actions once it receives the form data. See: Post to another page within a PHP script
I understand how to email using PHP
Then I would recommend writing some PHP code that sends the email to you.
but can the action have two urls?
No. A web browser can't make two requests at the same time. Which response would take precedence?
Nor does it need to. Now, you have a target already:
http://www.mydomain.com/page2/
Don't you control that page? That would be the page on which you'd put your PHP code for sending an email. If you don't control that page, then you would want an intermediary page. Something like:
sendmailandredirect.php
(Named solely to illustrate intent, you can call it what you like.) What this page would do is send the email, then issue a redirect to your final target. Something like:
header('Location: http://www.mydomain.com/page2/');
In effect, there would be "two urls" but they're invoked in serial instead of in parallel.
If you wanted to keep the code seperate and the action url as /page2/ you could fire off an ajax request on submit to your sendmail handler.

Adding Captcha without accessing ASP

A client would like us to add a Captcha to their form at http://www.vilaswi.com/contact/. However, they use a 3rd party to run their authentication through http://www.innline.com/mailforms/vilasform/Record.asp so the form data displayed to the user is really the only thing I have control over. It looks like I can't add the Google reCaptcha PHP to the form as the action is already specified (see below).
<form action="http://www.innline.com/mailforms/vilasform/Record.asp" method="post" name="frmInfo" id="frmInfo" onsubmit="return validateForm()" class="contactform">
Google recommends download verify.php and adding that to the action. Is there another method I could use?
I have tried the honey pot method and that has not curbed the amount of spam submitted.
Thanks in advance for any help!

Prevent double form submission when coding in wordpress

I'm currently using wordpress 3.5 and creating a page template of my own. I have my own form in that template and when I click the submit button, it is successfully saved the data to my database. Unfortunately, when I click F5 or refresh button on my browser, It prompts an alert which says there will be a double form submission if I continue.
Usually I prevent this by using "redirect to the same page after submitting" technique. But I can't use header("location: ") to redirect because it generates error: Warning: Cannot modify header information - headers already sent by. Probably there are echos on other wordpress file that prevent redirecting.
Does anyone know how to solve this? Or does anybody know other technique to prevent double form submission beside redirecting?
I've always done this with Javascript:
<?php if(isset($_POST['submit_flag'])){ ?>
<script type='text/javascript'>
window.location='URL';
</script>
<?php } ?>
But now that I think about it, you could easily create another PHP page somewhere in your theme that's not included by the rest of your theme to handle the form data and re-direct back to your form.
I'm also about 98% sure that you can include $wpdb without sending headers by simply requiring "wp-blog-header.php".
There is no output besides in your template file. What would that be? Look at the source code of your website, it's only exactly what you tell Wordpress to create.
So that's first: you can use header("Location: ") at the top of the template file that's called on first. Usually header.php.
Secondly, you can (and usually should) use hooks to handle forms. For example:
add_action( 'init', function() {
// Handle stuff
} );
But perhaps with a different hook (I don't recall any best practice). Tutorials here and there will give you suggestions. In this case you will definitely call header("Location: " ) before there is any output. Your theme hasn't even been involved yet.

jQuery form processing

Does anybody know of a simple jQuery form processing tutorial that actually works?
I have a form I want to process via jQuery/Ajax nothing difficult in PHP but in jQuery and AJAX can I get it to work - no, all the tutorials are based round sending e-mails (or just lists of more lists of more lists of tutorials - hate those)
All I want to do is learn how to send a form via jQuery and AJAX to another page, save the data in a DB without having to leave the first page. I have tried all sorts but nothing works properly. Here is my form:
<form id="form">
<input type="text" name="abc" />
<input type="text" name="def"/>
<input type="text" name="ghi"/>
<input type="submit" name="try" id="try" />
</form>
Now what do I actually do? Sounds silly I know (and I guess I'll get another -1 star for this question) but I will be honest a GOOD simple tutorial would be really useful not just to me but to the others. I know php but jQuery/Ajax - just don't know/understand. I will not be alone
This is one of the good tutorials on how to submit forms using ajax and php.
This link is a reference teaching how to submit forms via jQuery/AJAX. Have the form post to a PHP page to handle the form data.
In short, your jQuery code would look similar to this:
$("#form").submit( function()
{
// Handle validation or any extra data here.
// Return true if validation passed and the data should be posted
// Return false if the form should not be submitted
// You can also do any extra work here that you like before returning,
// including changing something on the page or showing the user a message.
}
There's a cracking plugin for this:
http://plugins.jquery.com/project/form/
It's as easy as:
$('#myForm').ajaxForm(function() {
alert("Thank you for your comment!");
});