Jump to content

Mail() function PHP


erikjan

Recommended Posts

 

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
}

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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().

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.