<?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 : ''; ?>
Related
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
I'm building a form, and I want that all the inserted values will be kept, in case of form submit failure. This is my code:
<?php
$error = "";
$name = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST["name"];
// Verify $_POST['name'] greater than 4 chars
if ( strlen($name) < 4 ){
$error= 'Name too short!';
}
}
?>
<html>
<head>
</head>
<body>
<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" name="myForm" id="idForm">
<input type="text" placeholder="Name" id="name" name="name" value=""/>
<input type="submit" value="submit"/>
</form>
<?php
echo "<h2>Input:</h2>";
echo $name;
if($error) {
// No Update AND refresh page with $name in text field
echo "<br/>" . $error;
} else {
// Update name in DB
}
?>
</body>
</html>
I would like that name field keeps the inserted input text, after submit. I tried to do with php code in input value but doesn't work. Any ideas?
Solved. This is the solution that I was looking for.
I added in value tag of input the following:
<?php if (isset($_POST['name'])) echo $_POST['name']; ?>
Therefore input field would look like:
<input type="text" placeholder="Name" id="name" name="name" value="<?php if (isset($_POST['name'])) echo $_POST['name']; ?>"/>
Thanks for your responses, helped me.
<?php
$error = "";
$name = isset($_POST["name"])?$_POST["name"]:""; //Added condition
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST["name"];
// Verify $_POST['name'] greater than 4 chars
if ( strlen($name) < 4 ){
$error= 'Name too short!';
}
}
?>
<html>
<head>
</head>
<body>
<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" name="myForm" id="idForm">
<input type="text" placeholder="Name" id="name" name="name" value="<?php echo $name; ?>"/>
<input type="submit" value="submit"/>
</form>
<?php
echo "<h2>Input:</h2>";
echo $name;
if($error) {
// No Update AND refresh page with $name in text field
echo "<br/>" . $error;
} else {
// Update name in DB
}
?>
</body>
</html>
You can just echo $_POST['name'] in the value attribute of the input.
Make sure you check POST values to avoid XSS.
I also put up the update DB function, as you don't need to show the form to the user if the name in longer the 4 chars!
<?php
$error = "";
$name = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (isset($_POST['name'])){ //change name content only if the post value is set!
$name = filter_input (INPUT_POST, 'name', FILTER_SANITIZE_STRING); //filter value
}
// Verify $_POST['name'] greater than 4 chars
if ( strlen($name) < 4 ){
$error= 'Name too short!';
} else {
// Update name in DB
// Redirect
}
}
?>
<html>
<head>
</head>
<body>
<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" name="myForm" id="idForm">
<input type="text" placeholder="Name" id="name" name="name" value="<?php echo $name; ?>"/>
<input type="submit" value="submit"/>
</form>
<?php
echo "<h2>Input:</h2>";
echo $name;
if($error) {
// No Update AND refresh page with $name in text field
echo "<br/>" . $error;
};
?>
</body>
</html>
If you want to keep all values/inputs for further use you could achieve that with php session values.
Besides - you should use $_SERVER['SCRIPT_NAME'] instead of $_SERVER['PHP_SELF']
Mars
php:
if(isset($_POST['submit_']))
{
if(isset($_POST['textEditor']) && !empty($_POST['textEditor']))
{
echo 'hello';
$msg = $_POST['textEditor'];
echo ($msg);
}
}
html:
<input type="submit" name="submit_" value="Add" />
<textarea name="textEditor" rows="20" cols="60" > </textarea>
I want to print the content of the text area when submit button is clicked. But even when the textarea is non-empty, it prints nothing.
For testing, I printed 'hello', but it still prints nothing that is the second ' if ' statement is not satisfied. I do not understand why does the second ' if ' statement fail !
And if I remove the second if statement, then I get an error:
Notice: Undefined index: textEditor in...
seems you have texteditor outside of form you need to try like
<?php
if(isset($_POST['submit_']))
{
if(isset($_POST['textEditor']) && !empty($_POST['textEditor']))
{
echo 'hello';
$msg = $_POST['textEditor'];
echo ($msg);
}
}
?>
<form method="post">
<textarea name="textEditor" rows="20" cols="60" > </textarea>
<input type="submit" name="submit_" value="Add" />
</form>
if form is set already try to check form method="post" or print_r($_POST); in php code
Try this:
<html>
<?php
if(isset($_POST['submit_']))
{
if(isset($_POST['textEditor']) && !empty($_POST['textEditor']))
{
echo 'hello';
$msg = $_POST['textEditor'];
echo ($msg);
}
}
?>
<head>
</head>
<body>
<form name="myForm" method="post">
<textarea name="textEditor" rows="20" cols="60" > </textarea>
<input type="submit" name="submit_" value="Add" />
</form>
</body>
</html>
Hope it helps
Might be debugging the code will help you.
as put this code under if(isset($POST['submit'])) { line
echo "<pre>";
print_r($_POST);
echo "</pre>";
hope this helps.
I have two .php files in the same folder on my computer. The first file is called "Client Instructions.php" and the second file is called "form_data_checker.php".
In the "Client Instructions.php" file, I have this snippet of code:
$required = array('name', 'comments');
require 'form_data_checker.php';
In the "form_data_checker.php" file, I have this code:
if(empty($temp) && in_array($key, $required)){ // etc.}
I'm using DreamWeaver and it looks like my "require" statement is correctly combining the two files. However, when I run my program I get error messages that the $required variable is not recognized as an array.
Can someone help me figure out why my $required array is not being recognized in my code in the "form_data_checker.php" file?
Here is the exact error message I'm getting:
Notice: Undefined variable: expected in C:\xampp\htdocs\introducingphp\form_data_checker.php on line 16
Warning: in_array() expects parameter 2 to be array, null given in C:\xampp\htdocs\introducingphp\form_data_checker.php on line 16
Thanks!
Ok, I'm including the entire contents of the two files because I'd really like to get this solved. Here is "Client Instructions.php":
<?php
require './includes/form_data_checker.php';
$myErrors = array();
$somethingsMissing = array();
$expectedInfo = array();
$requiredInfo = array();
if (isset($_POST['send'])){
$to = 'test#test.com';
$subject = 'Feedback from Client Information form';
$expectedInfo = array('name', 'email', 'comments');
$requiredInfo = array('name', 'comments');
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Client Contact Information</title>
</head>
<body>
<h1>Client Information</h1>
<?php if ($myErrors || $somethingsMissing) { ?>
<p class="warning"> Please fix the item(s) indicated. </p>
<?php } ?>
<form name="contact" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<p>
<label for="name"> Name:
<?php if ($somethingsMissing && in_array('name', $somethingsMissing)) { ?>
<span class="warning"> Please enter your name </span>
<?php } ?>
</label>
<input type="text" name="name" id="name">
</p>
<p>
<label for"email"> Email (optional):
<?php if ($somethingsMissing && in_array('email', $somethingsMissing)) { ?>
<span class="warning"> Please enter your email </span>
<?php } ?>
</label>
<input type="email" name="email" id="email">
<p>
<label for="address"> Address:
<?php if ($somethingsMissing && in_array('address', $somethingsMissing)) { ?>
<span class="warning"> Please enter your address </span>
<?php } ?>
</label>
<input type="text" name="address" id="address">
</p>
<p>
<input type="submit" name="send" id="send" value="Submit Information">
</p>
<form>
<pre>
<?php
if($_GET){
echo 'Contents of the $_GET array: <br>';
print_r($_GET);
} elseif ($_POST) {
echo 'Contents of the $_POST array: <br>';
print_r($_POST);
}
?>
</pre>
</body>
</html>
and here is "form_data_checker.php":
<?php
foreach ($_POST as $formFieldKeyName => $clientEnteredDataItem){
$tempClientData = is_array($clientEnteredDataItem) ? $clientEnteredDataItem : trim($clientEnteredDataItem);
if(empty($tempClientData) && in_array($formFieldKeyName, $requiredInfo)){
$somethingsMissing[]=$formFieldKeyName;
$$formFieldKeyName='';
} else if(in_array($formFieldKeyName, $expectedInfo)){
$$formFieldKeyName = $tempClientData;
}
}
?>
The problem is that
require './includes/form_data_checker.php';
is the first line, which is called before you actually declare $requiredInfo, so to ensure that the file is called after the array is declared, I moved the require statement. I think this should solve the issue.
<?php
$myErrors = array();
$somethingsMissing = array();
$expectedInfo = array();
$requiredInfo = array();
if (isset($_POST['send'])){
$to = 'test#test.com';
$subject = 'Feedback from Client Information form';
$expectedInfo = array('name', 'email', 'comments');
$requiredInfo = array('name', 'comments');
}
require './includes/form_data_checker.php';
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Client Contact Information</title>
</head>
<body>
<h1>Client Information</h1>
<?php if ($myErrors || $somethingsMissing) { ?>
<p class="warning"> Please fix the item(s) indicated. </p>
<?php } ?>
<form name="contact" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<p>
<label for="name"> Name:
<?php if ($somethingsMissing && in_array('name', $somethingsMissing)) { ?>
<span class="warning"> Please enter your name </span>
<?php } ?>
</label>
<input type="text" name="name" id="name">
</p>
<p>
<label for"email"> Email (optional):
<?php if ($somethingsMissing && in_array('email', $somethingsMissing)) { ?>
<span class="warning"> Please enter your email </span>
<?php } ?>
</label>
<input type="email" name="email" id="email">
<p>
<label for="address"> Address:
<?php if ($somethingsMissing && in_array('address', $somethingsMissing)) { ?>
<span class="warning"> Please enter your address </span>
<?php } ?>
</label>
<input type="text" name="address" id="address">
</p>
<p>
<input type="submit" name="send" id="send" value="Submit Information">
</p>
<form>
<pre>
<?php
if($_GET){
echo 'Contents of the $_GET array: <br>';
print_r($_GET);
} elseif ($_POST) {
echo 'Contents of the $_POST array: <br>';
print_r($_POST);
}
?>
</pre>
</body>
</html>
Try to use global:
global $required;
if(empty($temp) && in_array($key, $required)){ // etc.}
its not necessary to use globals. it should work even without it.
$_GLOBALS['required'] = array('name', 'comments');
require 'form_data_checker.php';
//form_data_checker.php
if(empty($temp) && in_array($key, $_GLOBALS['required'])){ // etc.}
The above method can also work. However, it's not good practice. Use $_SESSION[] variables instead.
The problem is that you are including the "form_data_checker.php" file before you are defining $requiredInfo. Since $requiredInfo is not defined yet it results in the warning that parameter 2 is not an array.
Update "Client Instructions.php" so that "form_data_checker.php" is executed after you define and populate the $requiredInfo array.
"Client Instructions.php" should look like this after the change:
<?php
$myErrors = array();
$somethingsMissing = array();
$expectedInfo = array();
$requiredInfo = array();
if (isset($_POST['send'])){
$to = 'test#test.com';
$subject = 'Feedback from Client Information form';
$expectedInfo = array('name', 'email', 'comments');
$requiredInfo = array('name', 'comments');
}
// now when 'form_data_checker.php' is called, $requiredInfo will be defined
require './includes/form_data_checker.php';
?>
<!DOCTYPE HTML>
<html>
<head>
....
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";
}
?>