Jump to content
foundherent

Newbie post of the year, guessing fundamental syntax error

Recommended Posts

Hello Team,

My job changed a few years back but I'm once again trying to learn to become better with php. I wanted to catch up to where I was around this time a few years back and honestly the language is coming back quickly but the quirky bottle-neck errors are still lingering. Hoping someone can take a look at the simple page I created below - post/get/session globals do not seem to be working, additionally, the semicolon after echo command does not seem to end the php command on the semicolon(displays everything). It may be a browser or something more simple but am hoping someone can at least look at my code below to see if there are any red flags.

<?php
print_r($_GET);
echo '<p>Hello World</p>';
?>
<html>
<body>

<h2>Campus Information</h2>

<form action="campus_test_form.php" method="get">
  Campus Name:<br>
  <input type="text" name="campus_name" required>
  <br>
  Campus Abbreviation:<br>
  <input type="text" name="campus_abbreviation" maxlength="8" required>
  <br>
  <input type="submit" value="Submit">
  <input type="reset">
</form>

</body>
</html>

Edited by foundherent

Share this post


Link to post
Share on other sites

You will be executing the print before you even display the page the first time. I'm assuming ' campus_test_form.php ' is this page. You need to put that in an if block.

<?php
if (isset($_GET)) {
	print_r($_GET);
	echo '<p>Hello World</p>';
/* You may want different html after the submit */
}
else {
?>
<!-- Your initial html code here -->
<?php
}
?>

 

Edited by gw1500se

Share this post


Link to post
Share on other sites
6 minutes ago, gw1500se said:

<?php
if (isset($_GET)) {
	print_r($_GET);
	echo '<p>Hello World</p>';
/* You may want different html after the submit */
}
else {
?>
<!-- Your initial html code here -->
<?php
}
?>
 

This needs a semicolon (;). However, you will be executing the print before you even display the page the first time. I'm assuming ' campus_test_form.php ' is this page. You need to put that in an if block.

Thank you this worked for the second part, which helped me realize another egregious error I was making thereby preventing the get from functioning at all. Thank you thank you!

 

Share this post


Link to post
Share on other sites

The principle is sound, however $_GET is always set. You need to check for a specific value
 

if (isset($_GET['campus_name'])) {
    echo '<pre>' . print_r($_GET, true) . '</pre>';    // easier to read
}

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.