Script Request: Perform More Than One Action in Response to Single Form Submit Button Click

You would think the answer would be all over the Internet and easy to find, but it’s not. I have searched many times, and in all the multitude of search results I have still never found an adequate usable answer. The MOST you ever find is someone saying how easy it is with PHP, but they don’t tell you how, even when the person they are answering asks them (odd). You can be that one in a billion person who finally answers it for real and helps someone out.

I have a simple HTML form with data fields first_name, last_name, email, phone, country, a few hidden inputs, and a single submit button, like so:

(Please note: the method is GET, not Post.)

    <form action="https://MyDomainOnMyServer.com/MyPHPScript.php”>
    <input type="text" name="first_name" value="" />
    <input type="text" name="last_name" value="" />
    <input type="text" name="email" value="" />
    <input type="text" name="phone" value="" />
    <input type="hidden" name="type" value="type123">
    <input type="hidden" name="project" value="new123">
    <select required name="country">
    <option value="">Choose your country</option>
    <option value="US">United States</option>
    <option value="CA">Canada</option>
    <option value="GB">United Kingdom</option>
    <option value="Many More">Many More Countries</option>
    </select>
    <input type="submit" value="Submit Form" />
    </form>

NOTE: Originally, the form action would have been:

action="https://TheirExampleDomainOnTheirRemoteServer.com/TheirRemotePHPScript.php" name="form1234"

Upon clicking the single submit button only, what I need to have happen is this:

  1. Send me an email to whatever@whatever.tld containing all the form submission data

  2. Place the form submission data into a MySQL database having the corresponding data fields

  3. Send the form submission contents including the hidden input values to "https://TheirExampleDomainOnTheirRemoteServer.com/TheirRemotePHPScript.php" name="form1234" AS IF that had remained set as the original form action to begin with

So basically what I’m trying to obtain is the cleanest possible PHP script that will do those three things, which is essentially what others have asked for over the years in search results I have found, but no one has ever provided it in a clear instance that works. If I can just see such a script, I should be able to see how it works and then do what I need. Thanks.

It’s probably not talked about much because if you break it up into smaller problems then it should be pretty trivial.

  1. send form to your own site instead of the external url
  2. validate the form
  3. send the email
  4. store the data in the db
  5. send the request to the external site

Example in Symfony
The code leaves a lot to be desired, first of all it doesn’t really validate that the exernal request was successfull. And it could be a lot cleaner by pulling out code from the controller

// src/Controller/TaskController.php
namespace App\Controller;

use App\Entity\Task;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\HttpFoundation\Request;

class TaskController extends AbstractController
{
  public function newTask(Request $request, \Swift_Mailer $mailer)
  {
    $task = new Task();
    $form = $this->createForm(TaskType::class, $task);

    $form->handleRequest($request);
    if ($form->isSubmitted() && $form->isValid()) {
        $task = $form->getData();

        // send email
        $message = (new \Swift_Message('Hello Email'))
        ->setFrom('noreply@yoursite.com')
        ->setTo('whatever@whatever.tld')
        ->setBody(
            $this->renderView(
                'emails/task/new.html.twig',
                ['task' => $task]
            ),
            'text/html'
        )
        ->addPart(
            $this->renderView(
                'emails/task/new.txt.twig',
                ['task' => $task]
            ),
            'text/plain'
        );

        $mailer->send($message);

        // save to db
        $entityManager = $this->getDoctrine()->getManager();
        $entityManager->persist($task);
        $entityManager->flush();

       // send request to external site
       $client = HttpClient::create();
       $task['name'] = 'form1234';
       $response = $client->request('GET', 'https://TheirExampleDomainOnTheirRemoteServer.com/TheirRemotePHPScript.php', [
         'query' => $task,
       ]);

        // show success page
        return $this->redirectToRoute('task_success');
    }

    return $this->render('task/new.html.twig', [
        'form' => $form->createView(),
    ]);
  }
}

Using the symfony docs example for processing a form

sending an email

And sending a request

Thanks a lot for your reply. I’ll check that out when I have time to pour over it. Not familiar with Symfony and am just looking for the bare bones PHP to get it done, but perhaps your answer will put me on track. I don’t really do PHP and have only dabbled in it a few times, but I know it’s good stuff.

Sponsor our Newsletter | Privacy Policy | Terms of Service