<body onload="cmdform.command.focus()">
<form method="POST" action="#" name="cmdform">
<textarea style="width:100%; height:90%; background:black; color:green;" name="textarea" cols="20" >
<?php
$name="My Name";
if (isset($_POST['submit'])){
if (!empty($_POST['command'])){
echo $me = $_POST['textarea'];
echo "\n";
echo $_POST['command'];
}else{
echo $me = $_POST['textarea'];
}
}
?>
</textarea>
<input type="text" name="command">
<input type="submit" value="Submit" name="submit">
<input type="reset" value="Reset" name="B2">
</form></body>
When I enter $name in textbox("command")
'My Name' should display in textarea
You can solve this with a simple str_replace() call.
$content = str_replace('$name', $name, $content);
Or you use an if() statement to check if the content is exactly the string "$name".
if ($_POST['command'] == '$name') {
$content = $name;
}
Related
I want to validate if the textbox has a value or not. Right now what I have is a textbox that has a value but the output says it is empty here is it it is like nothing is being conditioned on the code please see me code, thank you
Full Code
-Here is the full code of my form please take a look thank you very much
<form>
<div class="row">
<form method="POST">
<div class="col-md-8">
<?php
$code = 'Code';
$code2 = 'PIN';
if(isset($_POST['btnSubcode'])) {
$lblCode = isset($_POST['lblQrTxt']) ? $_POST['lblQrTxt'] : '';
$code = $lblCode;
$code = explode(":",$code); // code = array("QR Code","444444444|123")
$code = explode("|",$code[1]); // code[1] = "444444444|123"
$code = trim($code[0]); // 444444444
$code2 = $lblCode;
$code2 = explode(":",$code2); // code = array("QR Code","444444444|123")
$code2 = explode("|",$code2[1]); // code[1] = "444444444|123"
$code2 = trim($code2[1]); // 123
}
?>
<div class="form-group">
<label class="form-control-label">code</label>
<input type="text" name="input" id="card-code" value='<?php echo $code ?>' class="form-control">
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label class="form-control-label">pin</label>
<input type="text" id="card-pin" value='<?php echo $code2 ?>' class="form-control" maxlength="3">
</div>
<?php
if(isset($_POST['txtQrtxt']) && $_POST['txtQrtxt'] != '') {
echo "Text Present";
} else {
echo "Text Not Present";
}
?>
<div class="caption">
<div class="jumbotron">
<input type="text" name='txtQrtxt' value='Hello World' class="form-control" >
<textarea class="form-control text-center" id="scanned-QR" name="lblQrTxt"></textarea><br><br><br>
</div>
</div>
</form>
<div class="form-group float-right">
<input value="Topup" class="btn btn-primary topup-button">
</div>
</div>
</div>
</form>
<?php
$txtCodeqr = isset($_POST['txtQrtxt']) ? $_POST['txtQrtxt'] : '';
if (!empty($txtCodeqr)) {
echo "Text";
} else {
echo "Empty Textbox";
}
?>
my textbox
<input type="text" name='txtQrtxt' value='Hello World' class="form-control" >
You might be over complicating it. It is pretty simple.
<?php
if(isset($_POST['txt']) && $_POST['txt'] != '') {
echo "Text Present";
} else {
echo "Text Not Present";
}
?>
Additionally I would recommend you filter all input on post or get. Basically anything that gets information from a user.
Check here - http://php.net/manual/en/function.filter-input.php
<?php
$my_txt = filter_input(INPUT_POST, 'txt');
if(isset($my_txt) && $my_txt != '') {
echo "Text Present";
} else {
echo "Text Not Present";
}
?>
Also you need to add a submit button between your form tags. Like this.
<input type="submit" value="Submit">
Also you should have only one closing tag for every opening tag. This is called valid HTML.
For example a valid form is like
<form method="post">
First name:<br>
<input type="text" name="firstname" value="Mickey"><br>
Last name:<br>
<input type="text" name="lastname" value="Mouse"><br><br>
<input type="submit" value="Submit">
</form>
Ok I have made a simple php test file and tested it works. Your problem is:
You don't have a submit button. The $_POST will not be there if you do not submit a form first.
It would be easier to validate your textarea using javascript instead.
Here is my test file and it works:
<html>
<body>
<form method="POST">
<textarea name="txtQrtxt">
</textarea>
<input type="submit">
</form>
<?php
$var = $_POST['txtQrtxt'];
if (strlen($var)<=0) {
echo "Textarea empty";
} else {
echo "Textarea Okay";
}
?>
</body></html>
I'm trying to add 'guess' so that it shows in the text area without deleting the previous input. So that if you guessed '12' then '15' then '20'.
It would display in the text area as
12
15
20.
Rather than just displaying the most current value that you had entered for guess.
I've tried to put it into an array inside of the session, but the submit button is messing up the array when I try to display it inside of the textarea.
Any help would be greatly appreciated, thank you.
<?php
session_start();
if (!isset($_POST["guess"])) {
$_SESSION["AmountofGuesses"] = 0;
$message = "Guessing Game";
$_POST["Answer"] = rand(0,1000);
}
else if ($_POST["guess"] > $_POST["Answer"]) {
$message = $_POST["guess"]." is too high, try guessing lower.";
$_SESSION["AmountofGuesses"]++;
}
else if ($_POST["guess"] < $_POST["Answer"]) {
$message = $_POST["guess"]." is too low, try guessing higher.";
$_SESSION["AmountofGuesses"]++;
}
else {
$_SESSION["AmountofGuesses"]++;
$message = "You've guessed the correct number in,
".$_SESSION["AmountofGuesses"]." guess/guesses! Click restart to start a new
game.";
unset($_SESSION["AmountofGuesses"]);
}
if (isset($_POST["guess"])) {
$button= $_POST["button"];
$ArrayofNumbers = array();
array_push($ArrayofNumbers,$_POST["guess"]);
if ($button=="Restart"){
$message = "Guessing Game";
$_POST["Answer"] = rand(0,1000);
$_SESSION["AmountofGuesses"] = 0;
}
if ($button=="Answer"){
$message = "You've given up, your answer is above. Click restart to start a
new game.";
$_SESSION["AmountofGuesses"] = 0;
echo $_POST["Answer"];
}
}
?>
<title>Guessing Game</title>
<h3><?php echo $message; echo $ArrayofNumbers;?></h3>
<form action "program1.php" method="POST">
<table border="2" cellspacing="6">
<td>
<br/>
<p>Guess: <input type="text" name="guess"/> </p>
<input type="hidden" name="Answer" value="<?php echo $_POST["Answer"]; ?>" >
<p>Number of guesses: <?php echo $_SESSION["AmountofGuesses"]; ?> </p>
<center><input type="submit" name="button" value="Submit"></center>
<br/>
<center><input type="submit" name="button" value="Restart"></center>
<br/>
<center><input type="submit" name="button" value="Answer"></center>
</td>
<td>
<textarea name="paragraph_text" cols="50" rows="10">
<?php if (isset($ArrayofNumbers)) {echo implode("\n", $ArrayofNumbers);}?>
</textarea>
</td>
</table>
</br>
Back to Home
You can add another New Session by using the command condition for your array as below:
<?php session_start();
if (!isset($_POST["guess"])) {
$_SESSION["AmountofGuesses"] = 0;
$message = "Guessing Game";
$_POST["Answer"] = rand(0,1000);
}else if ($_POST["guess"] > $_POST["Answer"]) {
$message = $_POST["guess"]." is too high, try guessing lower.";
$_SESSION["AmountofGuesses"]++;
}else if ($_POST["guess"] < $_POST["Answer"]) {
$message = $_POST["guess"]." is too low, try guessing higher.";
$_SESSION["AmountofGuesses"]++;
}else {
$_SESSION["AmountofGuesses"]++;
$message = "You've guessed the correct number in,
".$_SESSION["AmountofGuesses"]." guess/guesses! Click restart to start a new
game.";
unset($_SESSION["AmountofGuesses"]);
}
if (isset($_POST["guess"])) {
$button= $_POST["button"];
if(#$_SESSION["guess"]!=''){
array_push($_SESSION["guess"],$_POST["guess"]);
}else{
$ArrayofNumbers = array();
array_push($ArrayofNumbers,$_POST["guess"]);
$_SESSION["guess"]=$ArrayofNumbers;
}
if ($button=="Restart"){
$message = "Guessing Game";
$_POST["Answer"] = rand(0,1000);
$_SESSION["AmountofGuesses"] = 0;
unset($_SESSION["guess"]);
}
if ($button=="Answer"){
$message = "You've given up, your answer is above. Click restart to start a
new game.";
$_SESSION["AmountofGuesses"] = 0;
unset($_SESSION["guess"]);
echo $_POST["Answer"];
}
}
?>
<title>Guessing Game</title>
<h3><?=$message?></h3>
<form action "" method="POST">
<table border="2" cellspacing="6">
<td>
<br/>
<p>Guess: <input type="text" name="guess"/> </p>
<input type="hidden" name="Answer" value="<?=#$_POST["Answer"]?>" >
<p>Number of guesses: <?=#$_SESSION["AmountofGuesses"]?> </p>
<center><input type="submit" name="button" value="Submit"></center>
<br/>
<center><input type="submit" name="button" value="Restart"></center>
<br/>
<center><input type="submit" name="button" value="Answer"></center>
</td>
<td>
<textarea name="paragraph_text" cols="50" rows="10">
<?php if (#$_SESSION["guess"]!='') {
foreach ($_SESSION["guess"] as $value) {
echo $value."\n";
}
}?>
</textarea>
</td>
</table>
</form>
If you are ok with the output being similar to 12 15 20, then you can change $ArrayofNumbers to perhaps $PastGuesses and then append each next answer as a regular string.
$PastGuesses .= " " . $_POST["guess"];
And make sure $PastGuesses is still stored in $_SESSION.
I'm learning PHP. A beginner. The code from the tutorial I follow is below.
<?php
if (isset($_POST['submit']) && (!empty($_POST['submit']))) {
$from = 'Alexey Pazukhin (alexey.pazukhin#mail.ru)';
$subject = $_POST['subject'];
$text = $_POST['elvismail'];
$output_form = FALSE;
if (empty($subject) && empty($text)){
echo 'Subject and text fields are empty. <br/>';
$output_form = TRUE;
}
if (empty($subject) && (!empty($text))) {
echo 'Subject field is empty. <br/>';
$output_form = TRUE;
}
if ((!empty($subject)) && empty($text)) {
echo 'Text field is empty. <br/>';
$output_form = true;
}
if((!empty($subject)) && (!empty($text))){
$dbc = mysqli_connect('localhost', 'root', 'root', 'elvis_store')
or die ('Connection failed. MySQL');
$query = "SELECT * FROM email_list";
$result = mysqli_query($dbc, $query)
or die('DB query error');
while ($row = mysqli_fetch_array($result)) {
$first_name = $row['first_name'];
$last_name = $row['last_name'];
$msg = "Dear $first_name $last_name, \n $text";
$to = $row['email'];
mail($to, $subject, $msg, 'From:' . $from);
echo 'Message sent:' . $to . '<br/>';
}
mysqli_close($dbc);
}
}
else {
$output_form = TRUE;
}
if ($output_form) {
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<label for="subject">Subject of email:</label><br />
<input id="subject" name="subject" type="text" value="<?php echo $subject; ?>" size="30" /><br />
<label for="elvismail">Body of email:</label><br />
<textarea id="elvismail" name="elvismail" rows="8" cols="40"><?php echo $text; ?></textarea><br />
<input type="submit" name="Submit" value="Submit" />
</form>
<?php
}
?>
The problem is that code doesn't send any mails after clicking Submit button (either I fill the fields or don't) and returns an empty (new) form in browser (Chrome).
Replace
<input type="submit" name="Submit" value="Submit" />
with
<input type="submit" name="submit" value="submit" />
you can only check - if(isset($_POST['Submit'])). You dont want to check for !empty.
In your input tag you have given name attribute as name="Submit".
so, use $_POST['Submit']
instead of $_POST['submit']
Because post variables are CASE SENSITIVE.
I'd change if (isset($_POST['Submit')) to if (isset($_POST['sendIt'))
and then i'd change <input type="submit" name="Submit" value="Submit" />
to:
<input type="submit" name="sendIt" value="Submit" />
This way your form has it's own unique send value rather than the bog standard submit which could lead to future issues if and when you decide to add anymore forms into the website.
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.
here is my code. I am not sure why after i input the first and last name the second page does not show the proper text.. The form is suppose to take in first name and last name into a text box.. Then on the next page when person submits it should validate that the proper type of data was input, and then print out text if it was not, or print out text if it was successful.
<body>
<h2 style="text-align:center">Scholarship Form</h2>
<form name="scholarship" action="process_Scholarship.php" method="post">
<p>First Name:
<input type="text" name="fName" />
</p>
<p>Last Name:
<input type="text" name="lName" />
</p>
<p>
<input type="reset" value="Clear Form" />
<input type="submit" name="Submit" value="Send Form" />
</form>
my second form
<body>
<?php
$firstName = validateInput($_POST['fName'],"First name");
$lastName = validateInput($_POST['lName'],"Last name");
if ($errorCount>0)
echo <br>"Please use the \"Back\" button to re-enter the data.<br />\n";
else
echo "Thank you for fi lling out the scholarship form, " . $firstName . " " . $lastName . ".";
function displayRequired($fieldName)
{
echo "The field \"$fieldName\" is required.<br />n";
}
function validateInput($data, $fieldName)
{
global $errorCount;
if (empty($data))
{
displayRequired($fieldName);
++$errorCount;
$retval = "";
}
else
{
$retval = trim($data);
$retval = stripslashes($retval);
}
return($retval);
}
$errorCount = 0;
?>
</body>