LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   php form action : second action ?? (https://www.linuxquestions.org/questions/programming-9/php-form-action-second-action-4175541688/)

esteeven 05-05-2015 10:35 AM

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

maples 05-05-2015 10:53 AM

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>


NevemTeve 05-05-2015 10:55 AM

Yes, you can do two different things in a PHP program.
Code:

<?php
    if (test-is-finished) {
/* #1 send email */
        ...

/* #2 print output */
      ...
    }
?>


esteeven 05-07-2015 11:08 AM

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? :)

maples 05-07-2015 11:45 AM

Did you double-check that "submit" is properly defined as the name for your Submit button?
Code:

<input type="submit" name="submit" value="Send" />

esteeven 05-07-2015 12:50 PM

Quote:

Originally Posted by maples (Post 5359200)
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.

esteeven 05-07-2015 12:53 PM

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.

sundialsvcs 05-08-2015 08:06 AM

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.


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