Variable from HTML to PHP error - php

I have a html from that saves the user input in a separate page called "welcome.php"
<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
<input type="submit">
</form>
And if I just echo the input, it works;
Welcome <?php echo $_POST["name"]; ?><br>
So far so good, but when I try to declare the input to a new variable or to a if-statement id does not work as usual. For example;
Welcome <?php echo $_POST["name"]; <br>
$new = $_POST["name"];
echo $new;
?>
or
if ($_POST["name"] === "hi");
{
etc...
}
How do I sort it out? How do I use a user input from a HTML-form with php variables?

Remove <br> from your PHP code.

<?php echo 'Welcome' . $_POST["name"] . '<br>';
$new = $_POST["name"];
echo $new;
?>

You need to con-cat <br> with $_POST["name"]. This has syntax error as <br> is not terminate with semicolumn(;).
Try This
Welcome <?php echo $_POST["name"]."<br>";
$new = $_POST["name"];
echo $new;
?>

<?php $new = $_POST['name'];
echo "Welcome ".$new."<br/>";
echo "<p>".$new."</p>";
?>
Try Using this. I hope this will be helful.
Thanks

Related

How can I not include a line in the action page if the corresponding form field was left empty?

Suppose I have the following form, where the input field Phone is optional.
<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
Phone (optional): <input type="text" name="phone"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
I want to display the submitted data in the action page. So here it is the welcome.php page:
<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br>
Your phone number is: <?php echo $_POST["phone"]; ?>
Your email address is: <?php echo $_POST["email"]; ?>
</body>
</html>
My question is how can I exclude the Your phone number is: <?php echo $_POST["phone"]; ?> section if the user doesn't input any value for the Phone in the form? Simply saying, if user leaves a field empty, the corresponding sentence will not be generated in the action page.
What would be the best possible way to achieve that?
I'm just trying to learn the different approaches of form handling. I'll appreciate any suggestion. Thanks in advance.
Simply add a condition that checks if the variable isn't empty.
If so, print the required line.
<?php if(!empty($_POST['phone'])) {
echo 'Your phone number is: '. $_POST["phone"] . '<br />';
}
?>
Note: Consider validating/escaping the input data prior to printing them.
Update - In case of multiple fields:
$fields = array(
'phone' => 'This is my phone number',
'address' => 'I live at',
'zipcode' => 'And my zip code is:'
);
foreach($fields as $fieldname => $label){
$value = $_POST[$fieldname]; //consider escaping/validating the value...
if(!empty($value)){
echo $label . ' ' . $value . '<Br />';
}
}
Just try:
<html>
<body>
Welcome <?= $_POST["name"]; ?><br>
<?= (!empty($_POST["phone"])) ? 'Your phone number is: ' . $_POST["phone"] : ''; ?>
Your email address is: <?= $_POST["email"]; ?>
</body>
</html>
This uses a ?: ternary operator to determine if the $_POST["phone"] is empty or not. Also use <?= shorthand for <?php echo when echoing inside html tags.
Warning
Also, beware of XSS use htmlentities() to escape the output like htmlentities($_POST["phone"]) also, use more specific validation for filtering phone and email strings.
Check post fields are having values or not like below:
<html>
<body>
<?php if(!empty($_POST['name'])) echo 'Welcome '. $_POST['name'] ?><br>
<?php if(!empty($_POST["phone"])) echo 'Your phone number is: '. $_POST["phone"]; ?>
<?php if(!empty($_POST["email"])) echo 'Your email address is: '. $_POST["email"]; ?>
</body>
</html>

PHP - POST is not catching any value, where GET works well

SORRY FRIENDS FOR THIS QUESTION, I TESTED THE SAME CODE IN REAL TIME SERVER. IT WORKED FINE, BUT WHEN THE CODE RETURN IN PHPSTORM IT NOT WORKED.
(don't know exactly what the reason )
First.php
<form action="second.php" method="post" >
Name: <input type="text" name="name" >
<input type="submit">
</form>
second.php
<?php
if(isset($_POST["name"])){
$username = $_POST["name"];
echo $username;
}else
{
echo "null value";
}
?>
Here i am getting null value.
dont know what the reson.
but i simple testing this will GET (where method="get")
and
second.php is
<?php
echo $_GET["name"];
?>
it worked well.
Try this:
HTML
<form action="second.php" method="post" >
Name: <input type="text" name="name" >
<input type="submit">
PHP
<?php
if($_SERVER['REQUEST_METHOD'] == "POST")
{
$username = $_POST["name"];
echo $username;
}else
{
echo "null value";
}
?>

PHP validation help. First and last name, validated as required fields

This link leads to where my code is ran.
http://erobi022.pairserver.com/phpvalidate1.php
<!DOC TYPE html>
<html>
<body>
This is a simple form
<form method="post" action="send_phpvalidate1.php">
Please enter your first name: <input type="text" name="First"></p>
Please enter your last name: <input type="text" name="Last"></p>
<button type="submit">Submit</button>
</form>
</body>
</html>
Once the submit button is hit, all data is sent and posted here. I can get first and last name to output fine, but if i leave them blank it will only say that i left my first name field blank.
http://erobi022.pairserver.com/send_phpvalidate1.php
<!DOCTYPE html>
<html>
<body>
Welcome,
</P>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name1 = $_POST["First"];
if (empty($name1)) {
echo "You've entered nothing for first name";
echo "<br>";
echo "<a href='phpvalidate1.php?text=hello>Click here to fix</a>";
die; //if you mess up, youll have to fix it
} else {
echo " Your first name is $name1 ";
}
}
echo "<br>";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name2 = $_POST["Last"];
if (empty($name2)) {
echo "You've entered nothing for last name";
echo "<br>";
echo "<a href='phpvalidate1.php?text=hello>Click here to fix</a>";
die; //if you mess up, youll have to fix it
} else {
echo " Your last name is $name2 ";
}
}
?>
<?php // can have multiple php sections
echo "<a href='phpvalidate1.php'>Return to form</a></p>";
//have to use a simple qoute within html to make it work
?> </p>
Return to home page
</body>
</html>
You can just use that using HTML 5, simple
<!DOC TYPE html>
<html>
<body>
This is a simple form
<form method="post" action="send_phpvalidate1.php">
Please enter your first name: <input required="required" type="text" name="First"></p>
Please enter your last name: <input required="required" type="text" name="Last"></p>
<button type="submit">Submit</button>
</form>
</body>
</html>
No need to use two big ifs, just replace your PHP code with this :
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name1 = $_POST["First"];
$name2 = $_POST["Last"];
if (empty($name1) || empty($name2)) {
echo "Please complete both the fields.";
echo "<br>";
echo "<a href='phpvalidate1.php?text=hello>Click here to fix</a>";
die; //if you mess up, youll have to fix it
} else {
echo " Your name is $name1 $name2";
}
}
?>
First, whenever there is a die() statement,only the codes that appear before that run, the ones which come after don't get to run(in relation to your code.)
you can also trim your codes down this way and it will still work
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name1 = $_POST["First"];
$name2 = $_POST["Last"];
if (empty($name1) || empty($name2)) {
echo "Please complete both the fields.";
echo "<br>";
echo "<a href='phpvalidate1.php?text=hello>Click here to fix</a>";
die; //if you mess up, youll have to fix it
} else {
echo " Your name is $name1 $name2";
}
}
?>

PHP AND IF statement

im tring to call php page from a simple html page, and i cant find the syntax error
this is the html
<html>
<body style="background-color:#990000;">
<h5 align="center" style="font-family:tahoma;color:white;font-size:50px;"> Welcome! </h5>
<form align="center" method="post" action="php_site.php">
First name: <input type="text" name="firstname"><br>
Last name: <input type="text" name="lastname"><br>
<input type="submit" value="Search!">
</form>
</body>
</html>
and this is the php
<?php
if (isset($_POST['firstname']) && (isset($_POST['lastname'])))
{
echo "Welcome $_POST['firstname']";
echo "This is your last name $_POST['lastname']";
}
else
{
echo "This is Empty!";
}
?>
thanks!
The syntax of the embedded references to $_POST inside the strings is not quite right. Some ways to fix it are:
Wrap them in {} brackets:
echo "Welcome {$_POST['firstname']}";
echo "This is your last name {$_POST['lastname']}";
Or, remove the single quotes:
echo "Welcome $_POST[firstname]";
echo "This is your last name $_POST[lastname]";
Or, use string concatentation with . instead of embedding:
echo "Welcome " . $_POST['firstname'];
echo "This is your last name " . $_POST['lastname'];
And/or, pull the values into plain-named variables first:
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
echo "Welcome $firstname";
echo "This is your last name $lastname";
See the doc on variable parsing in strings.
Try this:
<?php
if (isset($_POST['firstname']) && isset($_POST['lastname']))
{
echo "Welcome ".$_POST['firstname'];
echo "This is your last name ".$_POST['lastname'];
}
else
{
echo "This is Empty!";
}
?>
<?php
if (isset($_POST['firstname']) && isset($_POST['lastname']) )
{
echo "Welcome ".$_POST['firstname'];
echo "This is your last name $_POST['lastname']";
}
else
{
echo "This is Empty!";
}
?>
First off, you should set your set your $_POST values to variables.
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
Then, ensure that you're splitting the strings from the variables by using the concatenation punctuation .:
echo "Welcome" . $firstname; instead of echo "Welcome $_POST['firstname']";
Also, please post the syntax error you're getting. If PHP is breaking (white screen of death), then please add ini_set("display_errors", "1"); in order to write errors to the screen, and then post the error output.

Embedding PHP into HTML

<?php
If ( isset ($_POST['name'] ) ) {
$name = $_POST['name'];
if (!empty ($name)) {
$sentence = $name . " is the best footballer of his generation. ";
} else {
echo "Please enter a name";
}
}
?>
<html>
<head>
</head>
<body>
<!-- ********************************** -->
<form action="form3.php" method="POST">
Name: <input type="text" name="name"><br>
<input type="submit" value="Send">
</form>
<textarea rows="7" cols="30"> <?php echo $sentence; ?> </textarea>
</body>
</html>
The code works just fine, but for some reason the text inside the textarea shows this error
Notice: Undefined variable: sentence in C:\xampp\htdocs\form3.php on line 29
Please help.
$sentence is only initialized when this statement is true: if (!empty ($name)) {.
To avoid the error, put $sentence = ""; above the if-statement.
You can solve this using different options:
1- define $sentence at top of the page such as:
$sentence = '';
2- or use isset($sentence) before printing it:
<?php echo isset($sentence)? $sentence : ''; ?>

Categories