Joefa
Joefa

Reputation: 9

How to print input with php code

I just start learning php and html and I wanted to make something that gets input and print out the output with php code. However, with the code I have, it lets me input, but not printing anything when I click submit. Why?

<html>
<head><title>Noob</title></head>
<body>
<form action="" method="post">
<input type="text" name="username" value="" />
<input type="submit" name="submit value="Submit" />
</form>

<?php
 if (isset($_POST['submit'])) { //to check if the form was submitted
 $username= $_POST['username'];
 echo $username;
}
?>

</body>
<html>

Upvotes: 0

Views: 14608

Answers (5)

codeParane
codeParane

Reputation: 29

are you sure you run the page via php interpreter? in local you can use xampp or wampp(product by windows) in mac you can use mampp

Upvotes: 0

codeParane
codeParane

Reputation: 29

you missed the quotes in button name

<html>
<head><title>Noob</title></head>
<body>
<form action="" method="post">
<input type="text" name="username" value="" />
<input type="submit" name="submit" value="Submit" />// you missed here
</form>

<?php
 if (isset($_POST['submit'])) { //to check if the form was submitted
 $username= $_POST['username'];
 echo $username;
}
?>

</body>
<html>

Upvotes: 1

Afshan Shujat
Afshan Shujat

Reputation: 551

There is a syntax error you have missed the double quotes name="submit"

    <?php
     if (isset($_POST['submit'])) { //to check if the form was submitted
    // For printing whole post data
     echo '<pre>'; print_r($_POST);
     $username= $_POST['username'];
     echo $username;
    }
    ?>

You can also use var_dump for printing out. Hope this will help you.

Upvotes: 0

Andrew
Andrew

Reputation: 1324

Your HTML is missing a quotation mark on the line where you have

<input type="submit" name="submit value="Submit" />

it should be

<input type="submit" name="submit" value="Submit" />

Upvotes: 1

Sebastian
Sebastian

Reputation: 89

In the below line in your code

<input type="submit" name="submit value="Submit" />

the word name = "submit does not have a closing double colon

Upvotes: 3

Related Questions