Executing PHP code based on a form being submitted or not?

I have some PHP code and an HTML form in xxx.php. My question is simple (hopefully), how do I execute a certain piece of PHP code based on the form being successfully submitted?

You would detect if a post method form has been submitted -

if($_SERVER['REQUEST_METHOD'] == 'POST')
{
    // post method form processing code goes here...
}
1 Like

Hmm…upon testing, it didn’t work. Here is the exact PHP code snippet:

And the form HTML. I didn’t write this. It was free and pre-coded. I simply added some PHP for validation to work with my invite system:

It does use some JS, but I don’t know if that is affecting it. Basically, it ignores the code inside what you showed me, and hence the code never gets changed to “activated”.

Further insight would be most appreciated!

What does this ‘contact us’ form have to do with processing the invite code activation link?

What is the overall work-flow?

Also, why is there a hidden email field, since passing data through the form is insecure and cannot be trusted.

I have a private forum and existing members are allowed to recruit new ones via my invite system. They enter their friend’s e-mail via a form and a message is sent to the friend and a random code is created and placed in the DB as well as their e-mail. Once they follow the link in said message, it passes the code on a new form. The code is immediately checked against the one in the DB for security, and the contact form only displays if the code is correct. It is then changed to “activated”. The user only sees the message part of the form and their e-mail is retrieved from the DB based on their unique code. They click send and the message and hidden e-mail field are sent to me so I can finalize their account. :slight_smile:

The issue is that it changes the code upon going to the second form, but before the form is submitted. If the member has to come back, or for some reason reloads the page, it won’t work since the code was already changed.

I’d like it to only change once the form is correctly processed. :smiley:

Did you forget the action link on the form? Just look at this link for sample.

https://www.w3schools.com/php/php_forms.asp

It seems to use handler.php for processing. The form works great with the code I pasted. Here is handler.php:

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
/*
Tested working with PHP5.4 and above (including PHP 7 )

 */
require_once './vendor/autoload.php';

use FormGuide\Handlx\FormHandler;

$pp = new FormHandler(); 

$validator = $pp->getValidator();
$validator->fields(['email'])->areRequired()->maxLength(50);
$validator->field('email')->isEmail();
$validator->field('message')->maxLength(200);

$pp->attachFiles(['text']);

$pp->sendEmailTo('xxxx'); // ← Your email here

echo $pp->process($_POST);

There is also a form.js…not sure if you need that.

Any ideas? Thanks!!!

Sponsor our Newsletter | Privacy Policy | Terms of Service