Print the content of text area not working - php

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.

Related

PHP function argument not processed to output

<?php
Function runSearch($name)
{
If(isset($_POST['submit']))
{
$name = $_POST['name'];
echo "Results for " .$name;
}
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Search String: <input type="text" name="name"><br>
<input type="submit" name="submit" value="Submit"><br>
</form>
This code is suppose to display what is entered into the Search String text box. When I don't use a function it works fine. But as soon as I place the code into the function runSearch there is no output. I'm new to php can an argument be sent to a php function and then displayed on the screen?
you need to call your function, otherwise nothing will happen. Also you need to removed the $name-parameter:
<?php
function runSearch()
{
if(isset($_POST['submit']))
{
$name = $_POST['name'];
echo "Results for " .$name;
}
}
runSearch();
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Search String: <input type="text" name="name"><br>
<input type="submit" name="submit" value="Submit"><br>
</form>

How to create a form that accept number only

How can I create a form that accept number only?
<!DOCTYPE html>
<html>
<body>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
<label>x:</label><br>
<input type="text" name="xx" ><br>
<input type="submit" value="Submit">
</form>
<?php
if (isset($_POST['xx']) || !empty($_POST['xx']) || is_numeric($_POST['xx'])){
$x = $_POST['xx'];
}else{
echo "vous devez taper des chiffres!";
}
echo $x . "<br />";
?>
</body>
</html>
I am new in PHP please an easy answer.
Thank you :-)
If you only want to accept a number, you can also do so using the html element <input>.
Like this:
<input type= "number" name= "xx">
As a precaution, check the server-side $ _POST["xx"] element. Whether the element contains the expected values.
Like this:
check whether the element has been filled in and does not contain only white characters.
And remove any white space before and after the number using trim()
$xx = trim($_POST["xx"]);
if(!empty($xx) && is_numeric($xx)) {
// if "if-condition" true
} else {
// if "if-condition" false
}
I have this message if it is empty
enter image description here
<!DOCTYPE html>
<html>
<body>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"], ENT_QUOTES, "utf-8"); ?>">
<label>x:</label><br>
<input type="number" name="xx" ><br>
<input type="submit" value="Submit">
</form>
<?php
$value = trim($_POST["xx"]);
if(!empty($value) && is_numeric($value)) {
$xxx = $_POST['xx'];
} else {
echo "vous devez taper des chiffres!";
}
echo $xxx . "<br />";
?>
</body>
</html>
It is not an answer this, I don't know how to add code in the comment.

How to print or echo some text in new page if the if statement is true in php

Hello Every One i am new to coding and now i am learning php and html below i submited the code and i want to print Hello World in a new page if the given user name and password is correct but it is printing in the same page can any one help me Thanks in Advance
<!doctype html>
<html>
<body>
<form action="#" method="post">
<input type="text" name="username" placeholder="Enter The Username"><br>
<input type="password" name="password" placeholder="Enter Password"><br>
<input type="submit" name="submit" value="Login">
<input type="reset" value="Cancel">
</form>
<?php
$a = 123;
$b = 234;
$c = $_POST["username"];
$d = $_POST["password"];
$e = "Hello World!!";
$f = "Error 404 Page Not Found";
if(isset($_POST['submit'])){
if ($a == $c && $b == $d )
{
print "$e";
}
else
{
echo "$f";
}
}
?>
</body>
</html>
you could use the header function and it would look like this:
if ($a == $c && $b == $d )
{
header("location: newpage.php");
exit;
}
else
{
echo "$f";
}
or indeed as stated above me redirect the form to the new page
First of all this will be the html part in a separate part like login.html:-
<!doctype html>
<html>
<body>
<form action="login.php" method="post"><!-- if you will not give action then form will posted to the same page -->
<input type="text" name="username" placeholder="Enter The Username"><br>
<input type="password" name="password" placeholder="Enter Password"><br>
<input type="submit" name="submit" value="Login">
<input type="reset" value="Cancel"><!-- about this i cannot say anything -->
</form>
</body>
</html>
Now in login.php:-
<?php
$original_user_name = 123; // take variable name that are self descriptive
$original_user_password = 234;
$form_username = $_POST["username"];
$form_password = $_POST["password"];
// $e = "Hello World!!"; no need of extra variable
// $f = "Error 404 Page Not Found"; no need of extra variable
if(isset($_POST['username']) && isset($_POST['password'])){ // check with POSTED values not with button value
if ($original_user_name == $form_username && $original_user_password == $form_password){
echo "hello World!";
}else{
echo "Error 404 Page Not Found!";
}
}else{
echo "please fill both user name and password!";
}
?>
Note:- both files must be in the same working directory.
Create a new .php file where you can put your PHP code & in the html file, change form tag to < form action="path of that php file" method="post">

PHP - Processing after input is entered twice in a row

I have a problem with my form.
I have an HTML Form as follows:
<form action="index.php" method="post">
<input type="text" name="text" id="text">
<input type="submit" value="Send" name="send">
</form>
Now my problem is:
Every time I process this form for the first time, it should output an error. The error has to be shown every time, if this is the first try.
If I enter the same input value twice, it should succeed.
I thought about setting a session variable $_SESSION['first_visit] if I'm processing the form for the first time.
I also thought about saving the $_POST['text'] - Value into Session but it's being overwritten every time.
Thank you for your answers.
<?php
extract($_POST);
if( isset($send) )
{
if( $fired <= 0 )
{
$fired++;
echo "Nope, error. send again to succeed";
}
else
{
echo "Yay success";
}
}
?>
<form action="" method="post">
<input type="hidden" name="fired" value="<?php echo isset($fired) ? $fired : 0 ?>">
<input type="text" name="text" id="text">
<input type="submit" value="Send" name="send">
</form>
Here's some quick and dirty snippet. We set a hidden input field with an initial value of 0 and count it up everytime the form has been send. The only thing to do afterwards is to check if the value of the hidden field is bigger than 0
You can use cookies instead of session
<form action="index.php" method="post" id="exampleForm">
<input type="text" name="text" id="text">
<input type="button" value="Send" name="send" onClick="checkVisit()">
</form>
<script>
function checkVisit(){
var isFirstVisit = getCookie("first_visit");
if(isFirstVisit == "" || isFirstVisit == 1){
setCookie("first_visit",0,1);
}else if (isFirstVisit == 0){
setCookie("first_visit",1,1);
document.getElementById("myForm").submit();
}
}
</script>
Thanks for this fast Answer! Your Tipp helped me with my problem a lot.
Here is the working Script:
<?php
session_start();
if(isset($_POST['go'])) {
$text = $_POST['text'];
$fired = $_POST['fired'];
if($fired <= 0) {
$fired++;
echo "Nope";
$_SESSION['val'] = $_POST['text'];
}
else {
if($fired > 0 && $_SESSION['val'] == $_POST['text'] ) {
echo "Success";
}
else {
echo "Failed";
$_SESSION['val'] = $_POST['text'];
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="test2.php" method="post">
<input type="text" name="text" id="text">
<input type="hidden" name="fired" value="<?php echo isset($fired) ? $fired : 0 ?>">
<input type="hidden" name="val" id="val" value="">
<input type="submit" value="Senden" name="go">
</form>
</body>
</html>

set value of input field by php variable's value

I have a simple php calculator which code is:
<html>
<head>
<title>PHP calculator</title>
</head>
<body bgcolor="orange">
<h1 align="center">This is PHP Calculator</h1>
<center>
<form method="post" action="phptest.php">
Type Value 1:<br><input type="text" name="value1"><br>
Type value 2:<br><input type="text" name="value2"><br>
Operator:<br><input type="text" name="sign"><br>
Result:<br><input type"text" name="result">
<div align="center">
<input type="submit" name="submit" value="Submit">
</div>
</form>
</center>
<?php
if(isset($_POST['submit'])){
$value1=$_POST['value1'];
$value2=$_POST['value2'];
$sign=$_POST['sign'];
if($value1=='') {
echo "<script>alert('Please Enter Value 1')</script>";
exit();
}
if($value2=='') {
echo "<script>alert('Please Enter Value 2')</script>";
exit();
}
if($sign=='+') {
echo "Your answer is: " , $value1+$value2;
exit();
}
if($sign=='-') {
echo "Your answer is: " , $value1-$value2;
exit();
}
if($sign=='*') {
echo "Your answer is: " , $value1*$value2;
exit();
}
if($sign=='/') {
echo "Your answer is: " , $value1/$value2;
exit();
}
}
?>
All I want to do is that answer should be displayed in the result input field instead of echoing them separately. Please help? I Know it's simple but I am new in PHP.
One way to do it will be to move all the php code above the HTML, copy the result to a variable and then add the result in the <input> tag.
Try this -
<?php
//Adding the php to the top.
if(isset($_POST['submit']))
{
$value1=$_POST['value1'];
$value2=$_POST['value2'];
$sign=$_POST['sign'];
...
//Adding to $result variable
if($sign=='-') {
$result = $value1-$value2;
}
//Rest of your code...
}
?>
<html>
<!--Rest of your tags...-->
Result:<br><input type"text" name="result" value = "<?php echo (isset($result))?$result:'';?>">
inside the Form, You can use this code. Replace your variable name (i use $variable)
<input type="text" value="<?php echo (isset($variable))?$variable:'';?>">
Try this
<input class="qtytext-box" type="number" value= <?php echo $colll2; ?> >

Categories