PHP Submit Form | Input field with echo'd value in it gives undefined index

2.6k Views Asked by At

Currently I have the following form:

<form id="new_account_form" action="php/new-account.php" method="post">
    <span>name</span>
    </br>
    <input type="text" name="main_name" required></input>
    <br><br>

    <span>email adres</span>
    </br>
    <input type="text" name="main_email" required value="<?php echo $email; ?>" disabled></input>
    <br><br>

    <span>user</span>
    </br>
    <input type="text" name="main_username" required value="<?php echo $username; ?>" disabled></input>
    <br><br>
</form>

As you can see both the email input field and the username input have PHP values in them that are echoed. The input field aren't empty.

The problem I am facing right now is when I submit the form and try to $_POST the input fields in the other page I keep getting the following error:

Notice: Undefined index: main_email

Notice: Undefined index: main_username

Am I echoing the variables in the wrong place or something?

The new-account.php file contains the following code:

<?php
    //get data from form
    $main_name = $_POST['main_name'];
    $main_email = $_POST['main_email'];
    $main_username = $_POST['main_username'];

    echo $main_name;
    echo "</br>";
    echo $main_email;
    echo "</br>";
    echo $main_username;
    echo "</br>";
?>
4

There are 4 best solutions below

1
On BEST ANSWER

after a little googling, disabled forms don't post to the action page. replace disabled with readonly and you should be fine.

1
On

Original answer: The reason why you're getting those notices, is because the variables have not been set in the inputs' values of your form. I was 50% right and 50% wrong.

(Consult my edit below)

  • Use a ternary operator

Change: value="<?php echo $email; ?>"

to

value="<?php $email=!empty($_POST['main_email']) ? $_POST['main_email'] : ''; ?>"

or

value="<?php echo !empty($_POST['main_email']) ? $_POST['main_email'] : ''; ?>"

and do the same for the other input.

Use a conditional !empty() for the other inputs in the other file also.

Sidenote: </input> is an invalid closing tag. </br> is also invalid, it should read as <br/>

Edit: - which is now a supplemental answer:

Upon seeing the other answer about disabled I agree on that point and they were right.

However, you will get undefined index notices in your form inputs, since those variables have not yet been set/defined.

View your HTML source (with error reporting enabled) and you will see something similar to the following:

<input type="text" name="main_email" required value="<br />
<b>Notice</b>:  Undefined variable: email in <b>/path/to/your/file.php</b> on line <b>xxx</b><br />
" disabled></input>

and a similar notice for the other input.

  • The notices will appear in the inputs themselves.

  • Even though the input fields are disabled, PHP will still throw undefined notices for the variables.

Additional edit(s)

You stand at also getting Notice: Undefined variable: email in... and for the username after submission. Least, that's what my test revealed. Again; use a ternary operator for your inputs and it will solve the problem completely.

It is presently unknown as to why you're echoing the values for the inputs. If those are populated from elsewhere (a database, a file, other), either remove the variables, use a ternary operator as I already said, or use "Email" and "Username" as the default values.

Add error reporting to the top of your file(s) which will indicate errors, if any.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Error reporting should only be done in staging, and never production.

0
On
if(isset($main_email))
{ 
    echo $main_email;
}

you can use isset() function... http://php.net/manual/en/function.isset.php

0
On

Replace:

    <input type="text" name="main_email" required value="<?php echo $email; ?>" disabled></input>


with:

 <input type="text" name="main_email" required value="<?php echo $email; ?>" readonly></input>

The disabled field values of forms are not posted.So always use readonly where you want to post the value as well as want that the value remains unchanged.