Beginner php "Warning: Undefined array key"

lavendercake

So, I'm learning PHP and I keep getting "Warning: Undefined array key" in $GET_["fname"] and $GET_["age"]:

<main>

    <form action="inputs.php" method="get">
        Name: 
        <br/>
        <input type="text" name="fname">
        <br/>
        Age:
        <br/>
        <input type="number" name="age">
        <br/>
        <input type="submit" name="submit">

    </form>
        <br/>
        Your name is <?php echo $_GET["fname"]; ?>
        <br/>
        Your age is <?php echo $_GET["age"]; ?>

</main>
Joshua Angnoe

I'll assume you want to know how to get rid of this error message.

The first time you load this page you display a form and $_GET is empty (that's why it is triggering warnings). Then you submit the form and the fname and age parameters will be added to the url (because your form's method is 'get').

To resolve your issue you could wrap the two lines inside some if-statement, for example:

<?php if(isset($_GET['fname']) && isset($_GET['age'])): ?>
        <br/>
        Your name is <?php echo $_GET["fname"]; ?>
        <br/>
        Your age is <?php echo $_GET["age"]; ?>
<?php endif; ?>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Warning: Undefined array key "submit" in php 8

error Warning: Undefined array key "name" #php

Warning: Undefined array key error in PHP

Warning: Undefined array key in php for unknown reason

PHP: Unable to resolve Warning: Undefined array key 0

Warning: Undefined array key 63 php8

"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP

Little problem with HTML form and input tags and with PHP get method (Warning: Undefined array key)

Warning: Undefined array key "id" in D:\XAMPP\htdocs\iceico\CURD\index.php on line 20

Warning: Undefined array key

wordpress siite: Warning: Undefined array key "scheme"

I've created a password protected Web Page. It works, yet, an error displays. Warning: Undefined array key "password" in .... on line 74

PHP - Regex for beginner

PHP beginner: getting available routes

from php array to javascript (beginner)

Beginner Help: PHP MySQL (PDO) Function

Print a value in php file using html [beginner]

Python Beginner: Passing Selenium WebElement Object to PHP

html+php+database (very beginner)

Beginner Question : How to upload and work w/ PHP files on a website?

php beginner, Can I assign same variable for multiple loop

how can a beginner implementing getaddress.io API using php

Beginner: Delete row from Table and Database PHP/HTML/MySQL

Value not updating when form submitted(PHP, beginner level)

PHP - INSERT Query failing, returns no error. Beginner

Beginner PHP / SQL / AJAX - Query not working when parameter passed to server side script

FragmentPagerAdapter for beginner

Webdesign for beginner

beginner, xampp