r/PHPhelp • u/DownFromHere • Mar 29 '26
Basic Beginner Question for Form Issue
I am working on PHP form handling with a local Wordpress site running on WPEngine using the CSS & Javascript ToolBox. My script is in the header. I can't get the form input to pass to PHP successfully no matter what I try.
I have tried POST, GET, and REQUEST. I have tried writing the php file manually in the HTML code and writing it with <?php echo $_SERVER['SCRIPT_NAME'];?> and both $_SERVER['SCRIPT_FILENAME'] and $_SERVER['PHP_SELF']. I have tried removing the ".php" tag on the end of the file name. None of them have worked.
My only output is "not registered". Since I have tried to so many methods, I think the input is not successfully passing to PHP. It's also changing my page layout that I've already written with CSS. I have an onclick() function written in Javascript for the submission button as well.
HTML code:
<section><form id="form1" action="<?php echo $_SERVER['SCRIPT_NAME'];?>" method="post">
<ol id="form2">
<li>
<label for="choice1">Choice 1 </label>
<input id="opt1" class="choices" name="opt1" required="" type="text"/>
<ul id="infoOpt1" class="optionInfo">
<li>Info about Choice 1</li>
</ul>
</li>
<li>
<label for="choice2">Choice 2 </label>
<input id="opt2" class="choices" name="opt2" required="" type="text" />
<ul id="infoOpt2" class="optionInfo">
<li>Info about Choice 2</li>
</ul>
</li>
</ol>
<Input type="submit" value="submit" onclick="changeColor()"> </form></section>
PHP Code:
<? php
ECHO 'Hello World!<br>';
$opt1 = isset($_POST['opt1']) ? $_POST['opt1'] : 'not registered';
echo htmlspecialchars($opt1);
?>
6
u/PhilsForever Mar 29 '26
Go back to your PHP class book and read about forms. Pay particular attention to method and action.
3
u/Big-Dragonfly-3700 Mar 30 '26
Post all the code needed to reproduce the problem.
To get a form to submit to the same page it is on, simply leave out the entire action attribute.
The opening php tag in the posted form processing code contains a space and may or may not be treated as a php tag. It should be <?php
The form processing code should detect if a post method form has been submitted before referencing any of the form data, e.g.
if($_SERVER['REQUEST_METHOD'] === 'POST'){ // form processing code goes here...}The code for any .php page should be laid out in this general order -
In the form processing code, have you checked if/what the $_POST data is, e.g.
echo '<pre>'; print_r($_POST); echo '</pre>';Have you checked what the 'view source' of the page is in the browser, to make sure it is what you expect? Could you have another opening form tag before the posted one?
While not the cause of the current problem, the posted markup contains some mistakes and obsolete characters. The for='...' attribute in the label must match the id='...' attribute in the field OR you can simply put the closing
</label>tag after the field it corresponds with and leave out the for and id attributes. The / slash before the > is obsolete and should be removed. You should validate the resulting web pages at validator.w3.org2
u/__agletesque Mar 29 '26
The problem is possibly with the js function since you're probably trying to "mimick" submitting the form like you would by clicking the form's input of type submit
1
u/equilni Mar 31 '26
Break the code done to smaller components, test, then continue.
Can you test the form outside Wordpress? If yes, does the form work? If you break it down to simple components, does it work? Example:
Just a note, fixing the code slightly, works, so there is something else happening as others noted.
0
u/HongPong Mar 30 '26
error_log(print_r( $ post )) etc is your most reliable bet to assess the issue