1

I have a below code which has a input text box and its value I want to set is php variable. Below is the code:

index.php

<!DOCTYPE html>
<html>

    <head>
        <title>My Website</title>
        <link href="main.css" rel="stylesheet" type="text/css" href="" />
    </head>

    <body>
        <form enctype="multipart/form-data" action="upload.php" method="POST">
            <input type="hidden" name="MAX_FILE_SIZE" value="512000" />
            Select file: <input name="userfile" type="file"/>

            <input type="text" name="filename" value=<?php echo $filename; ?> />

            <input type="submit" value="Send File" multiple/>
        </form>
    </body>

</html>

Now the above code is working fine and it does what is written in upload.php. But for some reason, inside the input type text box, there is <br in it. I am not able to make it clear. Also it says undefined variable filename. Below is the screenshot of the webpage:

enter image description here

Can anyone please help in why it says filename undefined variable and why <br is shown on input type text box. I have tried below approach but nothing worked:

<input type="text" name="filename" value="<?php echo $filename; ?>" />

<input type="text" name="filename" value='<?php echo $filename; ?>' />

enter image description here

2
  • $filename isn't defined in script. Initialize it prior to using it. Commented Aug 10, 2019 at 5:00
  • I have defined it as $filename = ""; but doesnt worked. Commented Aug 10, 2019 at 5:11

2 Answers 2

1

It just becuase of you are not getting $filename from php.

<?php $file = isset($filename) ? $filename : ''; ?> //if value doesn't exist it'll take '' value.
<input type="text" name="filename" value="<?php echo $file; ?>" />
Sign up to request clarification or add additional context in comments.

1 Comment

No, its not working. I have attached a screenshot of its response.
1

You need to correctly set up variable value first. What you have done is not assignation, it is not in PHP tags. This is correct:

<?php $file = isset($filename) ? $filename : ''; ?>
<input type="text" name="filename" value="<?php echo $file; ?>" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.