PHP linked to button

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Chronix
Forum Newbie
Posts: 3
Joined: Wed Nov 24, 2010 10:32 am

PHP linked to button

Post by Chronix »

Hi, im completely new to PHP and I am trying to link a button to a PHP so that it will send an email to the specific one in the PHP and was wondering if anyone knows how I will be able to link these.

BTW: New here, hope my first post is correct :P
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: PHP linked to button

Post by califdon »

Welcome to the forum and to PHP. Let me restate what I think you are asking: How can I run a PHP script by clicking on a button on an HTML page? The answer is that you can either enclose the button in a hyperlink, like this:

Code: Select all

<a href="http://mysite.com/myscript.php">
<input type='button' value='Click Here' /></a>
or by using Javascript, like this:

Code: Select all

<input type='button' value='Click Here' onClick='document.location="mysite.com/myscript.php";' />
or by using an HTML form, like this:

Code: Select all

<form name='xxx' method='post' action='myscript.php'>
Enter your name here: <input type='text' name='myname' /><br />
<input type='submit' value='Click Here to Submit' /></form>
Read some PHP tutorials.
tutigan
Forum Newbie
Posts: 18
Joined: Thu Oct 28, 2010 5:43 am

Re: PHP linked to button

Post by tutigan »

If you want to send an email to someone using PHP, have a look at the mail function: http://php.net/manual/en/function.mail.php

You can set up a little form to send emails to someone as well, have a look at this:

Code: Select all

<?php
//this checks whether the form has been submitted
if(!$_POST['submit']) {
//it hasnt, so display the form
echo "<form method='post' action=''>
Please enter the email address you wish to send to:<br />
<input type='text' name='email' /><br />
Please enter the subject for the email:<br />
<input type='text' name='subject' /><br />
Please enter the body of the email:<br />
<textarea name='body' rows='5' cols='25'></textarea>
<input type='submit' name='submit' value='submit' />
</form>";
} else {
//form has been submitted
//collect inputs, send email
$to = $_POST['email'];
$subject = $_POST['subject'];
$body = $_POST['body'];
mail = ($to, $subject, $body);
echo "Email to " . $to . " has been sent!";
}
?>
you can also change who it comes from and more using headers, but this is just basic!
Chronix
Forum Newbie
Posts: 3
Joined: Wed Nov 24, 2010 10:32 am

Re: PHP linked to button

Post by Chronix »

Hi again, ive got the button working to send me an email whenever I click submit.

Now what I have to do is figure out how to add different variables(?) to the php so that when you click submit, it sends all the data to the email and not just the email address and the subject.

Heres the code im using at the moment, can you tell me where ive gone wrong?

Code: Select all

<?php
  $email = $_GET['Email'] ;
  $message = $_GET['Test1 . Test2 . Test3 . Test4 . Test5'] ;

  mail( "test1049@live.co.uk", "Test PHP",
    $message, "From: $email" );
?>
Chronix
Forum Newbie
Posts: 3
Joined: Wed Nov 24, 2010 10:32 am

Re: PHP linked to button

Post by Chronix »

Can anyone help me? I really need to know this
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: PHP linked to button

Post by califdon »

Code: Select all

$message = $_GET['Test1'] . $_GET['Test2'] . $_GET['Test3'] . $_GET['Test4'] . $_GET['Test4'] ;
Post Reply