0

I am very new to PHP and currently working on developing my first form. With the below code sample I am trying to achieve two things.

  1. If the fields for "name" or "email" are filled out in the form, then it will post "Your name is: "name" and "Your email is:"email".
  2. If a field is not filled out, then it will display nothing at all.

I believe this needs to be done with an IF statement but I am having a hard time finding anything showing the protocol for if the field is empty. Any help would be greatly appreciated.

<form action="Test.php" method="post">
    Name: <input type="text" name="name"><br>
    E-mail: <input type="text" name="email"><br>
    <input type="submit">
</form>

<?php 

$name = $_POST["name"];
if($name="name") {
    echo "Your name is: $_POST["name"]" ;
} else {
    echo "";
}
?>
<br>

<?php 
$email = $_POST["email"];
if($email = "email") {
    echo "Your email is: $_POST["email"]" ;
} else {
    echo "";
}
?>

2
  • 2
    You have multiple PHP syntax errors... you canNOT use " quotes within a "-quoted string, nor are you allowed to use quoted array keys within "-quoted strings. Your if() tests are doing ASSIGNMENTS (=), not testing for equality (==). Basically, you need to study basic PHP syntax a bit more. Commented Feb 10, 2014 at 3:14
  • 1
    All you need is echo "Your name is: " . $name; etc. If you want to check if a name was entered, if(!empty($_POST['name'])) or if(isset... - Wait for the answers to roll in...vvvvv Commented Feb 10, 2014 at 3:14

3 Answers 3

1

Your conditional statements are incorrect.

You're presently making an assignment using = with ($name="name") and ($email = "email") when you should be checking if something is "equal to" using ==.

Also, as Marc B stated in his comment: "you can NOT use " quotes within a " quoted string, nor are you allowed to use quoted array keys within " quoted strings..."

However, if you were to keep that strategy; i.e.: if($name=="name") then your form would only work if the words name and email were entered in the form's inputs.

There are a few ways to achieve this, checking if a field is (not) empty using if(!empty or if it is set if(isset.

In this case, I used if(!empty. Meaning, that if the fields are not empty, then echo the results afterwards.

This is a basic example: (There are other ways to achieve the same result)

<form action="Test.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>

<?php
$name = $_POST['name'];
if(!empty($_POST['name'])) {
    echo "Your name is: " . $name;
} else {
    echo "";
}
?>
<br>

<?php 
$email = $_POST['email'];
if(!empty($_POST['email'])) {
    echo "Your email is: " . $email;
} else {
    echo "";
}
?>
Sign up to request clarification or add additional context in comments.

Comments

0

This is all you need to write:

<html>
<body>

<form action="Test.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>

<?php

if(!empty($_POST['name']))
    echo "Your name is {$_POST['name']}. <br />";


if(!empty($_POST['email']))
    echo "Your email is {$_POST['email']}. <br />";

?>

</body>
</html>

P.S.: I really recommend reading an introductory book, at least to cover the basic functions. After that you can go directly to php.net and look for functions on-a-need basis.

Comments

0

You can use isset() to check if the variable is actually set. You could also use empty() to check that it exists and if there's a value stored. For example:

if (!empty($_POST['email')) {
    // do things
}

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.