LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-05-2015, 10:35 AM   #1
esteeven
Senior Member
 
Registered: Oct 2001
Location: Bristol UK
Distribution: Arch Slackware Ubuntu
Posts: 1,082

Rep: Reputation: 52
php form action : second action ??


I am trying to build an online multiple choice test and got some fantastic help here: http://www.linuxquestions.org/questions/programming-9/replace-numbers-with-a-sequence-in-php-ising-vim-or-geany-4175539014/">

When I click the "submit" button on the test page, the script displays the total correct:

Code:
echo "<div id='results'> $totalCorrect / 100 correct</div>";
I also want to email the test result to myself. Can I run a second form process on the same php file? If yes, I imagine that this would be using the mail function. Would it need to look something like this?

Code:
<?php
if($_POST['send']) { //send is the name of my submit button
    mail("me@myemailaddress.co.uk"');
}
?>
Am I going in the right direction? I feel that I am but have tried many variations and have had no success (yet!) I don't understand how I can get
Code:
echo "<div id='results'> $totalCorrect / 100 correct</div>";
in to the email.

I hope this makes sense. I'm really at the edge of my (current) abilities

Thanks
 
Old 05-05-2015, 10:53 AM   #2
maples
Member
 
Registered: Oct 2013
Location: IN, USA
Distribution: Arch, Debian Jessie
Posts: 814

Rep: Reputation: 265Reputation: 265Reputation: 265
Here's an example email form that I recently did in my web design class at school.

Code:
<?php

if ( isset($_POST[submit]) ) {
  $to = "(insert your email address)";
  $subject = "Test email"; //
  $message = "$_POST[emailMessage]";  //This variable should contain the entire message.
    //I've only used plain text, so I can't tell you how to use HTML formatting.
  $headers = "From:$_POST[emailAddress]\n"; // You probably won't need this one
  mail( $to, $subject, $message, $headers ); // This actually sends the email.
}

?>

<html>
<head>
<title>PHP Email</title>

</head>
<body>

<form method="post" action="<?php print "$_SERVER[PHP_SELF]"; ?>" >

Enter the source email address:
<input type="text" name="emailAddress" maxlength="50" size="30" />
<br /><br />
Enter the message to send:
<textarea name="emailMessage" rows="5" columns="8"></textarea>
<br /><br />
<input type="submit" name="submit" value="Send" />
</form>

</body>
</html>
 
Old 05-05-2015, 10:55 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Yes, you can do two different things in a PHP program.
Code:
<?php
    if (test-is-finished) {
/* #1 send email */
        ...

/* #2 print output */
       ...
    }
?>
 
Old 05-07-2015, 11:08 AM   #4
esteeven
Senior Member
 
Registered: Oct 2001
Location: Bristol UK
Distribution: Arch Slackware Ubuntu
Posts: 1,082

Original Poster
Rep: Reputation: 52
I am trying to add the mail function to my php file. Unfortunately, things aren't going as smoothly as I would like.

I started from the ground and worked upwards.

The following worked:

Code:
mail('me@myaddress.com', 'test test test', 'my message');
This also worked:

Code:
$to = 'me@myaddress.com';
    $subject = 'Test Result';
    $message = 'my message';
mail($to, $subject, $message);
The problem comes when I add:

Code:
if ( isset($_POST [submit]) )
Submit is the name of my submit button.

So. The following fails to send an email:

Code:
if ( isset($_POST [submit]) )
{
    $to = 'me@myaddress.com';
    $subject = 'Test Result';
    $message = '$totalCorrect / 100';
mail($to, $subject, $message);
}
This doesn't work at all. No message comes through.

I can see that the issue is with the "if" part - when I remove it, a message (without the $totalCorrect / 100) comes through.

What am I doing wrong?
 
Old 05-07-2015, 11:45 AM   #5
maples
Member
 
Registered: Oct 2013
Location: IN, USA
Distribution: Arch, Debian Jessie
Posts: 814

Rep: Reputation: 265Reputation: 265Reputation: 265
Did you double-check that "submit" is properly defined as the name for your Submit button?
Code:
<input type="submit" name="submit" value="Send" />
 
Old 05-07-2015, 12:50 PM   #6
esteeven
Senior Member
 
Registered: Oct 2001
Location: Bristol UK
Distribution: Arch Slackware Ubuntu
Posts: 1,082

Original Poster
Rep: Reputation: 52
Quote:
Originally Posted by maples View Post
Did you double-check that "submit" is properly defined as the name for your Submit button?
Code:
<input type="submit" name="submit" value="Send" />
Yes. That part all seems fine.

Code:
var_dump($_POST);
The output of the above shows that submit is working.
 
Old 05-07-2015, 12:53 PM   #7
esteeven
Senior Member
 
Registered: Oct 2001
Location: Bristol UK
Distribution: Arch Slackware Ubuntu
Posts: 1,082

Original Poster
Rep: Reputation: 52
Code:
$message = $totalCorrect;
This gives me what I want

Actually, I mean that it gives me what I want until I hit my next road block.

Onwards and upwards.

Thanks.
 
Old 05-08-2015, 08:06 AM   #8
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,633
Blog Entries: 4

Rep: Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931
It's very useful ... critical, really ... to use the debugging ("developer") facilities of your browser to s-e-e what is actually being sent to the server, and what the server actually sends back.

Very often, the hardest thing about troubleshooting a program is: to s-e-e what it is now doing.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Passing i nformation from 'form action' to php file. Dafydd Programming 4 02-10-2014 12:40 PM
HTML form action not passing content to PHP file Dafydd Programming 3 12-22-2013 01:41 AM
[SOLVED] Problems using php variable in form "action" attribute lrtward Programming 6 04-27-2012 05:26 PM
[SOLVED] PHP Automatic Form Action dstu Programming 3 08-08-2011 07:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration