How Can I Fix My PHP Form Handling Issue on WordPress?

0
3
Asked By CuriousCoder84 On

I'm working on a PHP form for a WordPress site that I'm developing locally using WPEngine, and I'm running into some trouble with passing form inputs to my PHP script. My code is located in the header and no matter what I try, I can't seem to get the data from the form to successfully pass to PHP.

I've experimented with POST, GET, and REQUEST methods, and I've tried manually embedding the PHP file in the HTML using variations of `$_SERVER` variables such as `SCRIPT_NAME`, `SCRIPT_FILENAME`, and `PHP_SELF`. Despite those attempts and removing the `.php` extension from the filename, I'm still only seeing the output "not registered" for the input values.

Moreover, it seems that my CSS layout is also being affected. I have a JavaScript function tied to the submit button that changes the color upon submission, but I'd really appreciate any guidance on getting this form functioning properly! Here's the HTML and PHP code I'm working with:

**HTML Code:**
```html

<form id="form1" action="" method="post">

    • Info about Choice 1
    • Info about Choice 2

```

**PHP Code:**
```php
<?php
ECHO 'Hello World!
';
$opt1 = isset($_POST['opt1']) ? $_POST['opt1'] : 'not registered';
echo htmlspecialchars($opt1);
?>
```

3 Answers

Answered By DevGuru99 On

It sounds like you're trying to execute PHP code after using the CSS & Javascript Toolbox, which just injects the code as plain text instead of executing it. To handle forms properly in WordPress, you should use `admin-ajax.php` with `wp_ajax_` hooks, or manage it directly in a template that WordPress processes as PHP. Also, I noticed you have a space in your PHP opening tag; it should be `<?php` not `<? php`. I've had a bit of a confusing afternoon dealing with similar issues, so don't feel too bad about it! If this WordPress hooks route seems overwhelming, you could also hire a developer through Codeable who specializes in custom form handling.

FormNewbie123 -

Do I have to place my code directly in the `admin-ajax.php` file, or should I implement it in the file invoked by my action link?

Answered By CodeNinja42 On

The issue might be with your hosting provider. Many managed WordPress hosting services like WPEngine impose restrictions on running custom scripts. They have a list of disallowed plugins and functionalities, which might be blocking your requests at the server level. I'm not entirely familiar with the CSS & Javascript Toolbox, but it might also be worth checking with their support for potential issues as well.

Answered By NightOwlDev On

Looking at your code, there are a couple things that stand out. You have two forms with the same ID, which can lead to issues. Also, in the second form, your `method` and `action` attributes seem to be swapped, and the PHP tag is incorrectly spaced. In WordPress, PHP added into page content won’t execute properly, so try to handle it in a plugin, theme template, or use an admin-post hook instead. For now, consider removing your JavaScript `onclick` since it could be interfering with the submission process.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.