erikjan Posted February 5, 2007 Share Posted February 5, 2007 Please can someone help me? Thanks in advance!! I have a page with two forms, form 1 and form 2. <FORM METHOD="post" ACTION="a.php" id=form1 name=form1> .................. <INPUT name="Submit" class="sendbox" type="submit" value="Pay"> </FORM> <FORM METHOD="post" ACTION="b.php" id=form2 name=form2> .................. <INPUT name="Submit" class="sendbox" type="submit" value="Pay by bank"> </FORM> How can I, when someone submits form 2, send an email using the PHP mail() function? <?php $email = "my@example.com"; $subject = "subject"; $message = "message . .."; mail($email, $subject, $message, "From: $email"); ?> So, only when form 2 is submitted the mail will be sent. If someone submits form 1, no mail is sent. Quote Link to comment Share on other sites More sharing options...
Xinil Posted February 5, 2007 Share Posted February 5, 2007 you're already pointing the forms to different pages. Assuming you aren't nesting the forms, then they'll be submitting to "a.php" and "b.php". So long as both pages don't use the mail() function, only one page will send the mail. If you click on "submit" under Form 1, then it will submit a.php (and any code written on a.php will be executed). If you click on "submit" under Form 2, then it will run code for b.php. So, it depends on whatever you write for those two files. Action="whatever.php" is the key. Quote Link to comment Share on other sites More sharing options...
erikjan Posted February 5, 2007 Author Share Posted February 5, 2007 Thanks for your time, but the b.php (in form 2) is not written by me, but is running on a server to which i don't have access. So, this is not the solution I am looking for. Quote Link to comment Share on other sites More sharing options...
erikjan Posted February 5, 2007 Author Share Posted February 5, 2007 the solution must be something like if action is b.php then send a mail. But how do I code it? Quote Link to comment Share on other sites More sharing options...
Xinil Posted February 5, 2007 Share Posted February 5, 2007 You shouldn't do it that way then. Make it one form. Then you have two submit buttons. One with name="submit1", the other with name="submit2". Then you just add this to the page: if (isset($_POST['submit1'])) { // submit my mail() } if (isset($_POST['submit2'])) { // don't submit mail } Quote Link to comment Share on other sites More sharing options...
erikjan Posted February 5, 2007 Author Share Posted February 5, 2007 Sorry, this cannot work either. Because: 1. the forms are totally different, so how to make one form with 2 submit buttons? This does not make sense i Think. 2. The if (isset($_POST['submit1'])) etc . . . cannot work , unless it is implemented in both a.php and b.php. But I cannot edit b.php. Quote Link to comment Share on other sites More sharing options...
erikjan Posted February 5, 2007 Author Share Posted February 5, 2007 Should I redirect b.php having the code <?php if (isset($_POST['submit2'])) { $email = "....."; $subject = "subject"; $message = "message"; mail($email, $subject, $message, "From: $email"); } ?> to the address where b.php initially goes to? Is that an option? Quote Link to comment Share on other sites More sharing options...
Xinil Posted February 5, 2007 Share Posted February 5, 2007 You can't redirect to b.php, because then you'll lose the form $_POST data. Oh, I think I understand what you're saying. You want the page to still submit to b.php, but also send an e-mail? Not easy unfortunately, the problem is that once you submit the form, you've lost control over the user until he goes back to that form page...but you're saying you dont' have access to b.php. Soo, the only solution I can think of is AJAX. When a user clicks "submit form 2", you can have AJAX call a separate page (say c.php) and this will send an e-mail. Once AJAX has returned with a successful value (the page ran successfully), submit the form. AJAX isn't hard to learn if you use jQuery ( www.jquery.com ). It's not a beginner job though, so if you can't figure out this solution, you're pretty much out of luck (unless you want to make two submit pages, one to submit the e-mail, and then the second submits the actual form to b.php). Or, you could alternatively make a pop-up that submits an e-mail (but it pop-up blockers are enabled that won't work). So, good luck with AJAX. Quote Link to comment Share on other sites More sharing options...
erikjan Posted February 5, 2007 Author Share Posted February 5, 2007 Dear Xinil, Oh, I think I understand what you're saying. You want the page to still submit to b.php, but also send an e-mail? Not easy unfortunately, the problem is that once you submit the form, you've lost control over the user until he goes back to that form page...but you're saying you dont' have access to b.php. IS THE EXACT POINT. Thanks for informing me about AJAX. I'll try to figure that out. Is ths really the only solution - concerning the fact that i have no access to b.php? Anyway, thanks a lot! Quote Link to comment Share on other sites More sharing options...
erikjan Posted February 5, 2007 Author Share Posted February 5, 2007 "You can't redirect to b.php, because then you'll lose the form $_POST data." However, can't i redirect b.php (with transporting the $_POST data to it) to the URL where b.php initially has to go? Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted February 5, 2007 Share Posted February 5, 2007 I dont quite understand what you are trying to achieve here, you can pass data through either the url or sessions, are those what you are looking for? Ted Quote Link to comment Share on other sites More sharing options...
erikjan Posted February 5, 2007 Author Share Posted February 5, 2007 Dear Ted, Well, i'll try to explain with more details involved. The site which I built is a shop. You can order 6 different products on page 1, and choose a quantity of each of them. By clicking a button you get page 2, in which you see the order specification. On this page you fill in a billing address and/or a delivery address. Depending on the delivery address the shipping costs are calculated. By clicking on a button "order confirmation and check out", you are sent to page 3, in which you see the order specification, including calculated shipping costs plus delivery and/or billing address data. On this page you have two hidden forms, each of them having a different submit button. One is "pay by bank transfer", which goes to a page a.php, which sends an email to the customer that his/her goods will be sent after the products have been paid. The other form (number 2) goes to a banking system, (like PayPal): a file i don't have access to, but which sends an url back when the payment is succesfull. In case 2, the client - just as in case 1, has to recieve a mail that his order has been received. But, because i don't have access to the file at the banking system, i have to implement a mail to a customer when he/she submits option 2. Is that more clear? Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted February 5, 2007 Share Posted February 5, 2007 yeap, i understand, in your case, i think sessions would be a better choice, or i would even considering writing into some temporary txt files to store these information, since you have few steps before the action actually begins. But I dont find sessions very reliable when you are talking about payments and stuff, storing in txt files would make them last much longer. Ted Quote Link to comment Share on other sites More sharing options...
Xinil Posted February 5, 2007 Share Posted February 5, 2007 erikjan, performing a simple AJAX statement would be much, much less painful than storing all that information in a text file (why on earth?). You don't want to be storing any of this sensitive ORDER information anywhere that isn't secure (anywhere that isn't a database). Sessions aren't reliable and neither are cookies. I'll help you out some more. http://docs.jquery.com/Ajax#.24.get.28_url.2C_params.2C_callback_.29 Use that to call the AJAX code. Then the page you called can execute the e-mail to the user. On the callback function, do a simple javascript: document.formname.submit(). Quote Link to comment Share on other sites More sharing options...
erikjan Posted February 5, 2007 Author Share Posted February 5, 2007 Dear Xinil, Thanks again, for following this thread! I will try your suggestions. Heard about AJAX, and am studying it at this right moment. Ciao. Quote Link to comment Share on other sites More sharing options...
erikjan Posted February 5, 2007 Author Share Posted February 5, 2007 Isn't it a problem however, that not all browsers support AJAX? Quote Link to comment Share on other sites More sharing options...
Xinil Posted February 5, 2007 Share Posted February 5, 2007 The only browsers that don't support AJAX are the ones that has Javascript disabled. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.