PHP AND IF statement - php

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.

Related

Variable from HTML to PHP error

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

Form not posting - PHP

I want to make this form where people can send a email to me. I've done this before but this time it won't post... I won't get a error message or anything like that. Even my custom made error messages won't show up.
<?php
$err = "<br/>";
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
if (!empty($name) && !empty($email) && !empty($message)) {
if (strstr($name, " ")) {
if(strlen($email) >=5 && strstr($email, "#") && strstr($email, ".")) {
} else {
$err = "<p style='color:#b30000'>Fill in a valid email!</p>";
}
} else {
$err = "<p style='color:#b30000'>Fill in a valid name!</p>";
}
} else {
$err = "<p style='color:#b30000'>Fill in all the fields!</p>";
}
} else {$name = ''; $email = ''; $message = '';}
echo $err . "
<form id='form' action='#form' method='post'>
<p>Full Name:</p>
<input class='input' type='text' name='name' placeholder=' Dirk Vondiek' value=". $name . ">
<p>E-Mail:</p>
<input class='input' type='text' name='email' placeholder=' example#domain.com' value=". $email . ">
<p>Your Message:</p>
<textarea class='textarea' name='message' placeholder=' Can you pls add me on steam?'>". $message ."</textarea>
<center><input class='submit' type='submit' name'submit' value='Send'></input></center>
</form>
"
?>
action='#form', it's invalid for an action syntax, so change that to action="" or action="handler.php" if you later decide to use two seperate files.
Edit for above: After testing for myself, that does indeed work, but the major culprit here is that you have a typo here name'submit' and your conditional statement
if (isset($_POST['submit'])) {...} will never happen because of it.
You forgot the equal sign.
So modify it to read as name='submit'.
You also have </input> you can safely get rid of it, it's not a valid closing tag.
And you forgot to close this statement (but in testing this didn't throw me an error and I find that rather a bit bizarre).
</form>
"
?>
So.
</form>
";
?>
That alone with error reporting would have thrown you something about it.
Nota:
However, if you have more code below the </form>" line, then you will get a parse error such as:
(Nota: I added an echo "Testing"; for this example error) but will throw you a parse error related to what you may be using:
Parse error: syntax error, unexpected 'echo' (T_ECHO), expecting ',' or ';'
As you mention in your question that it's meant to send mail with, but that wasn't included in your original post.
If you do have more code below that line and inside the same <?php ...?> tags, then you will need to close off that statement as stated in my answer.
For example.
This will throw a parse error:
</form>
"
mail();
?>
This won't throw an error (valid) so it needs to be closed (mail() is just a fill-in example here).
</form>
";
mail();
?>
Add error reporting to the top of your file(s) which will help find errors.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Then the rest of your code
Sidenote: Displaying errors should only be done in staging, and never production.
Please check your form action. You need to mention a URL here like example.php.
In case your PHP and HTML script are in the same page, you can keep it blank.
<form id='form' action='' method='post'>
Hope this helps.
You should change
<form id='form' action='#form' method='post'>
to
<form id='form' action='' method='post'>

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";
}
}
?>

Using Sessions To Remember User on PHP Form

Intro: I'm trying to learn PHP on $_SESSION. What I was trying to do is call the value assigned through sessions that when you close your tab will keep the value assigned and echoes it on the browser when you open a tab in the browser.
Issue: There's something wrong with my code where for some reason I couldn't echo the value entered in on a form.
The form looks like this:
Name:_____________
Email:_____________ Remember me? __ SUBMIT
I made it so that $_SESSION['name'] = "John" and $_SESSION['email'] = "someemail#email.com" only when user click on "remember me".
If you close a "tab" on the browser but not the browser itself should echo...
John
someemail#email.com
Here's your download link (some link here)...
But of course if you close the browser, session is lost. Cookies can be used but I'm working on sessions to learn more.
Below's code runs but for some reason I couldn't echo values from $_SESSION variables.
<?php
//Start session
session_start();
// session
if (isset($_POST['remember'])) {
$customer_name = $_SESSION['name'];
if (!($customer_name)) {
$customer_name = $_POST['name'];
}
$customer_email = $_SESSION['email'];
if (!($customer_email)) {
$customer_email = $_POST['email'];
}
}
//If form submit validate
if (isset($_POST['Submit'])) {
// Santize fields here but FILTER_VALIDATE_STRING isn't necessary as there is no absolute way
//to validate names absolutely
// Also shows error message if there's error
if ($_POST['name'] != "") {
$_POST['name'] = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
if ($_POST['name'] == "") {
$errors .= 'Please enter a valid first name.<br/><br/>';
}
} else {
$errors .= 'Please enter your first name.<br/>';
}
// Sanitize and validate email
// Error message shows if any
if ($_POST['email'] != "") {
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors .= "$email is NOT a valid email address.<br/><br/>";
}
} else {
$errors .= 'Please enter your email address.<br/>';
}
// If no errors, submitted form is emailed
if (!$errors) {
echo "I did something!<br /><br />"; // might add some message
//downloadLink();
echo "<br /><br />";
}
} else {
echo '<div id="error">' . $errors . '<br /></div>';
}
?>
<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Name:
<?php
if (isset($_SESSION['name'])) {
echo $_SESSION['name']."<br />";
}
else {
?>
<input type="text" name="name" value="<?php echo $_POST['name']; ?>" size="25" /><br />
<?php } ?>
Email:
<?php
if (isset($_SESSION['email'])) {
echo $_SESSION['email']."<br /><br />";
// echo link.. downloadLink();
}
else {
?>
<input type="text" name="email" value="<?php echo $_POST['email']; ?>" size="25"/>
<input type="checkbox" name="remember" /> Remember me
<input type="submit" name="Submit" />
<?php } ?>
</form>
</div>
Put this at the beginning under session_start();
if (!isset($_SESSION['name'])) {
echo "Your session is not good";
} else { echo "Session is set";
}
then replace $customer_name = $_SESSION['name']; with $_SESSION['name']=$_POST['remember']; and you will start getting results.

PHP Undefined index:

I am new to PHP...
Just trying this simple piece of code inside my about.php file (which links to index.php file via a hyperlink):
<form action ="about.php" method="POST">
<ul>
<li>
<label for="name"> Enter your name please:</label>
<input type="text" name="name"/>
</li>
<li>
<label for="comments" rows="20"> Enter your comments :</label>
<textarea id="comments" name="comments" rows="5" cols="38">
<?php
$name = $_POST["name"];
if (!empty($name)) {
echo "Your name is ".$name."and you like ny site!";
} else {
echo "Please enter your name";
}
?>
</textarea>
</li>
<li>
<input type="submit" />
</li>
</ul>
I get the following error:
Notice: Undefined index: name in D:\xampp\htdocs\stathis1\about\about.php on line 71
Please enter your name
Because $_POST["name"] is not set, you get that notice. So you need to test if it exists first, and then assign it to your $name variable, or empty string if it's not set.
You can change
$name = $_POST["name"];
to
$name = isset($_POST["name"])?$_POST["name"]:'';
use isset() instead of empty() because isset() function not triggering notice when the index not defined.
It's because $_POST['name'] hasn't been submitted/set yet. You need to either use an if statement or a ternary operator:
if (isset($_POST['name'])) {
$name = $_POST['name'];
if (!empty($name)) {
echo 'Your name is ' . $name . ' and you like ny site!';
} else {
echo 'Please enter your name';
}
}
Or, a shorter method:
$name = isset($_POST['name']) ? $_POST['name'] : '';
Right after starting your php part add this if sentence surrounding the code, so it only executes when the "name" exists.
if(isset($_POST["name"]))
Like this:
<?php
if(isset($_POST["name"])){
$name = $_POST["name"];
if (!empty($name)) {
echo "Your name is ".$name."and you like ny site!";
} else {
echo "Please enter your name";
}
} else {
echo "Please enter your name";
}
?>

Categories