r/PHPhelp • u/Tricky_Box_7642 • Dec 08 '25
Solved Help with PHP variables
So, i'm new to php, and i'm trying to build a customer satisfaction sheet for a made up business. i have 2 php documents. at the top of the main one (which we'll call doc1.php), i have a require once for the second document (let's call it doc2.php).
so:
<?php
require_once "SteamlineLogisticsForm.php";
?>in doc2, i have defined 5 different variables that work perfectly fine when i call them in that same document. however, when i try to call them in doc1, despite the require_once, they come up as undefined.
//doc2:
$fname = $_REQUEST["fname"];
$lname = $_REQUEST["lname"];
$email = $_REQUEST["email"];
$city = $_REQUEST["city"];
$pcode = $_REQUEST["pcode"];
//doc1:
<label for="fname">First Name*:</label>
<input id="fname" type="text" maxlength="50" name="fname" value="<?php echo $fname;?>"><br>
<label for="lname">Last Name*:</label>
<input id="lname" type="text" maxlength="50" name="lname" value="<?php echo $lname;?>"><br>
<label for="email">Email*:</label>
<input id="email" type="email" maxlength="100" name="email" value="<?php echo $email;?>"><br>
<label for="city">City*:</label>
<input id="city" type="text" maxlength="50" name="city" value="<?php echo $city;?>"><br>
<label for="pcode">Postcode*:</label>
<input id="pcode" type="text" maxlength="4" name="pcode" value="<?php echo $pcode;?>"><br>here is full script right now:
doc1
<?php
require_once "doc2.php";
console_log("fname");
?>
<!DOCTYPE html>
<html lang="en">
<body>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_"]);?>">
<label for="fname">First Name*:</label>
<input id="fname" type="text" maxlength="50" name="fname" value="<?php echo $fname;?>"><br>
<label for="lname">Last Name*:</label>
<input id="lname" type="text" maxlength="50" name="lname" value="<?php echo $lname;?>"><br>
<label for="email">Email*:</label>
<input id="email" type="email" maxlength="100" name="email" value="<?php echo $email;?>"><br>
<label for="city">City*:</label>
<input id="city" type="text" maxlength="50" name="city" value="<?php echo $city;?>"><br>
<label for="pcode">Postcode*:</label>
<input id="pcode" type="text" maxlength="4" name="pcode" value="<?php echo $pcode;?>"><br>
<input type="submit">
</form>
</body>
</html>doc2
<?php
$fname = filter_input(
INPUT_POST
, "fname");
/*$fname = $_POST["fname"]??'';*/
$lname = $_POST["lname"]??'';
$email = $_POST["email"]??'';
$city = $_POST["city"]??'';
$pcode = $_POST["pcode"]??'';
function console_log($output, $with_script_tags = true) {
$js_code = 'console.log(' . json_encode($output,
JSON_HEX_TAG
) .
');';
if ($with_script_tags) {
$js_code = '<script>' . $js_code . '
<
/script>';
}
echo $js_code;
}
/*$fnameErr = array("empty" => "This field is required", "")*/
$pcodeErr = array("empty" => "This field is required", "tooShort" => "Postcode must be four digits", "notDigits" => "Please only use numbers", "clear" => "")
?>
<!DOCTYPE html>
<html lang="en">
<body>
Name: <?php echo $_POST["fname"];?>
<?php echo $_POST["lname"]; ?><br>
Email: <?php echo $_POST["email"]; ?><br>
City: <?php echo $_POST["city"]; ?><br>
Postcode: <?php echo $_POST["pcode"];?><br>
<?php
switch ($pcode) {
case "":
echo $pcodeErr["empty"];
break;
case strlen($pcode)<4:
echo $pcodeErr["tooShort"];
break;
case (!preg_match("/^\d{4}$/",$pcode)):
echo $pcodeErr["notDigits"];
break;
case (preg_match("/^\d{4}$/",$pcode)):
echo $pcodeErr["clear"];
break;
}
?>
</body>
</html>
1
Upvotes
5
u/xreddawgx Dec 09 '25
Why are you using request? For what purpose
3
u/Big-Dragonfly-3700 Dec 09 '25
Firstly, forget about $_REQUEST variables. They combine get, post, and cookie variables, making for more work keeping track of data. You should use the correct $_POST or $_GET variables that you expect data in.
You are likely referencing the inputs before they exist, before the form has been submitted. The code for any page should be laid out in this general order -
Post method form processing code needs to detect if a post method form was submitted before referencing any of the form data. Use `if($_SERVER{'REQUEST_METHOD'} === 'POST'){`
Here are a handful of standard implementation practices -
1
u/xreddawgx Dec 09 '25
How are you requesting them?
1
u/[deleted] Dec 09 '25
I know it doesn’t answer your question, but this code is susceptible to XSS injections btw.
2
u/Commercial_Echo923 Dec 10 '25
Its because youre not sending a post request on the first page load.
You go to localhost/doc1.php, the browser sends a GET localhost/doc1.php request.
At this point the $_REQUEST array is empty because no data was sent to the server.
The page renders and the form is displayed, you enter values and submit it.
Now the server sends a POST localhost/doc2.php with the form data in headers which get processed by php into $_REQUEST and $_POST variables.
You should give your submit button a name:
<input type="submit" name="submitButton" />and in doc2.php check if its present in the data:1
u/[deleted] Dec 10 '25
Which part is giving you an error? If you don’t see one, google how to make php echo the errors onto the page.
Your switch statement is wrong. You don’t do the comparison at each case, you have one comparison at the top, then each case is your possible answer. I would suggest an if/else for your use case.
Last since you are requiring one document into another, you will have a resulting page with two DOCTYPEs so be careful of that. In your browser view the page source so make sure it’s not mangled
1
u/rmb32 Dec 10 '25
It looks like the form action should be:
$_SERVER[‘PHP_SELF’]
Your code shows: $_SERVER[‘PHP_’]
Which I don’t think is a valid item in the server superglobal.
Also, your doc2 file creates the fname, lname, etc… variables but doesn’t use them below it. It’s getting them again from $_POST. That’s not an error but it’s unnecessary when you have your nice variables.
Your switch/case is also faulty because some of your case statements will evaluate to true/false (like strlen or preg_match). The actual postcode will never match true/false, because it’s a piece of text (a string) so those cases will never get run.
Congratulations on starting your PHP adventure though! 🙂
A couple of other small tips:
-3
u/CitruS_cakE Dec 09 '25
I am on windows 11 but for some reason when I installed xAmpp it has that cannot validate error , & for some reason I don't have php .exe extention file in xAmpp>php folder. I did find a php file (no .exe extention) & it's same 139 kb but when I paste its path ,the errordoes not go . Please help (I am a rock beginner btw)
0
u/[deleted] Dec 09 '25
[deleted] — view deleted comment
1
u/[deleted] Dec 12 '25
[removed] — view removed comment