Getting input from text area and echoing it out - php

I have written some basic code so that a text area is displayed, and when the user inputs the text, clicks submit and it shows on the page in the method=''.
The code i have for the form is :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>Fun Translator</h1>
<form method="post" action="query.php">
<textarea name="txtarea">
</textarea>
<input type="submit" />
</form>
The code on the page query.php is:
<html>
<body>
<?php
echo $_POST["txtarea"];
?>
</body>
</html>
By looking at google, other questions and such, this should work, but doesnt!
Solution:
Thanks to Marc Audet, I put in phpinfo and all that came up in a big table as it does, I took it out and it started working. Any explanations?

this code is perfectly fine. something else is going wrong.

The textarea is not link to your form, you should add form="" in textarea like this:
<form method="post" action="query.php" name="myform">
<textarea name="txtarea" form="myform">

use
echo htmlspecialchars($_POST['name']);
to get value.

some browsers like the ID...
add an ID like this:
<form method="post" action="query.php">
<textarea name="txtarea" id="txtarea">
</textarea>
<input type="submit" />
</form>

Best guess: Your server doesn't support PHP.
Check the source code as delivered to the browser. If the <?php bit shows up, then the server hasn't run the page through a PHP engine.

As far as I know you should be doing textarea in the quotes not txtarea, I am not really sure though, it might be beneficial to use an ID instead.

Related

need help getting form to submit/go from a submit area

so I'm currently following this tutorial on PHP forms and ive made it to the bottom of this page where it says to test the form so far, http://www.webreference.com/programming/php/search/2.html
"Save your file and test your form at this point (search_display.php)."
When I go to type in a users name in thew design view of dreamweaver and hit submit nothing happens (as if the page is refreshing) however, when i navigate to my file from my MySQL (XAMP and phpMyADmin) server directory i can view my search page but there is no text box/area for me to enter in any data. I have followed/copied this tutorial verbatim and im quite unsure on why I can not get my data to submit to the query. Here is the code for my search page
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Search Contacts</title>
</head>
<p>
<body>
<h3>Search Contacts Details</h3>
<p>You may search either by first or last name</p>
<form method="post" action="search.php?go" id="searchform">
<p>
<input type="submit" name="submit" value="Search">
</p>
</form>
</body>
</p>
</html>
Thanks guys for the help.
Your HTML form is missing a text field. Please check the HTML sample on the first tutorial page.
Side note: you really should not be following this tutorial as it is greatly outdated, teaches bad practices and will not work when using PHP 7+ due to the removed mysql extension.
1. Place this code where you need the form. For example index.php . There is no input field in your form. So add input fields. For example i am adding email and mobile no as input fields
<h3>Search Contacts Details</h3>
<p>You may search either by first or last name</p>
<form method="post" action="search.php" id="searchform">
<input type="text" name="email" value="">
<input type="text" name="mobileno" value="">
<input type="submit" name="submit" value="Search">
</form>
2. Create another page called search.php and place the below code here
if(isset($_POST['submit'])){
$email = $_POST['email'];
$mobileno = $_POST['mobileno'];
}
Validate all the fields before inserting into database.

Simple HTML Form with PHP Validation

I am having a problem with a simple HTML form with PHP validation. It is a generic question so here are some stack overflow answers I already looked at:
HTML form with PHP - uses javascript
Tried: Html form with php validation and honeypot
But it gave me: Server Error in '/' Application.
Here is my HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<form action ="process.php" method ="POST">
First Name: <input type="text" name="fName">
<input type="submit">
</form>
</body>
</html>
And PHP:
<?php
if($_SERVER["REQUEST_METHOD" =="POST"]) {
//collect value of input fields:
$fName=$_REQUEST['fName'];
echo $fName;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
</body>
</html>
The form submits and calls my process.php page, but it does not echo my fName value.
Typo over here
$_SERVER["REQUEST_METHOD" =="POST"]// wrong close of squre bracket
^^
It would be
$_SERVER["REQUEST_METHOD"] =="POST"
Read http://php.net/manual/en/reserved.variables.server.php
You can also do like this.
<?php
if(isset($_POST['fName'])) {
//collect value of input fields:
$fName=$_POST['fName'];
echo $fName;
}
?>
There are some basic problem in your code, Thomas.
It is interesting, when passing parameters with GET or POST to get them somewhere else, specify the enctype.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<form action ="process.php" method ="POST" enctype="multipart/form-data">
First Name: <input type="text" name="fName">
<input type="submit">
</form>
</body>
</html>
But this is not what is going to solve your problem. Just an advice.
Your problem is in the fact you are doing it wrong in your PHP code.
First, there is a syntax error here:
if($_SERVER["REQUEST_METHOD" =="POST"]) {
This line should read:
if($_SERVER["REQUEST_METHOD"] =="POST") {
As you may see, you misplaced the closing bracket.
Besides, since you're using POST, the correct place to get the post variables is $_POST, not $_REQUEST. Then, instead of
$fName=$_REQUEST['fName'];
it should be
$fName=$_POST['fName'];
Finally, the right test to be done is not "what type of method my request used?" as you did with
if($_SERVER["REQUEST_METHOD"] =="POST")
but "is the variable I want to get here really defined?", as in
if (isset($_POST["fName"))
because the lack of this test could generate an error when you try to assign a value that does not exist to a variable.
But then I have a question: Why do you think you need this in your PHP code?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
</body>
</html>
This is doing NOTHING in your PHP code. You're not even using this HTML structure to display your data. This reminds me of an essential rule in programming: Take out all you don't need, so you may focus on what really matters.

Basic test of checkbox submit button fails?

Here are the two pages, the 1st has a checkbox and submit button, the 2nd is php code to examine whether the checkbox was checked. But instead of getting a yes or a no answer I get absolutely nothing, just a blank page.
1st page:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<body>
<form name="input" action="submit.html" method="GET">
cb1<input type="checkbox" name="cb1" value="cb1">
<input type="submit" value="Submit">
</form>
</body>
</html>
2nd page:
<?php
if isset($_GET['cb1']) {
echo 'Checkbox set';
}
else
{
echo 'Checkbox is not set';
}
?>
There is a mistake in your PHP-Code - you should enable "error_reporting" and "display_errors"!
The if Statement should be like this:
if(isset($_GET['cb1'])) {
Edit: Also the Submit-Page should be an PHP-File
You seem to be submitting to an html page ("submit.html"). Unless you do some url rewritting, that should be a php page.
Take a look at this <form name="input" action="submit.html" method="GET">. When you click submit button, then all the data will be send to submit.html. It's impossible to run php script with html extension.
Create the second file as a php file, instead of creating it as .html, create it as submit.php. Then it will work

Guidance for Php for a beginner

I've just started to learn PHP. I found the $_POST variable is not working and posted the same at the below link
$_POST[] not working in php
and as per the advise i installed XAMPP. But still the proble of $_POST variable is not solved.
Now i've a doubt whether i need to configure any global variable to make $_POST work. I'm totally lost on this and dont know how to proceed.
Any help on this is verryy much appreciated.
Below is the html code - report.html
<html>
<title></title
<head></head>
<body>
<form action="report.php" method="POST" >
<label for="firstname">First name:</label>
<input type="text" id="firstname" name="firstname" value="TestOnly" /><br />
</form>
</body>
</html>
and below is the php code - report.php
<html>
<head>
</head>
<body>
<?php
print( $_POST['firstname']);
?>
</body>
</html>
Below is the view that i got from chrome network data
Thanks.
See, try this.
Source of index.htm File:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>POST</title>
</head>
<body>
<form method="post" action="post.php">
<input type="text" name="name" />
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
Source of post.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>Output</title>
</head>
<body>
<?php
if(isset($_POST["name"]))
echo "You have posted " . $_POST["name"];
else
echo "Nothing has been set!";
?>
</body>
</html>
Now try saving these two files under same directory. Enter something in the text box and click on submit. Let us know what you have got.
A few things to check:
Your script will have no data in the POST array unless a form has been submitted (Posted) to that script.
If you have a script and it's submitting properly, make sure the form has method="Post" in its opening tag. If your form submits and in the result script you see a bunch of variables in the address bar separated by ampersands (&) then it's using GET instead of POST. Make sure you have the above statement method="Post" in the form tag.
If you've submitted the script and you still can't see anything, try putting print_r($_POST) or var_dump($_POST) at the top of the target script to see a dump of the $_POST array. If this gives you nothing, try var_dump($_REQUEST) and see if your form data shows up there.
Have you ever tried if your php is correct?
<?php
phpinfo();
?>
or
<?php
print_r($_POST);
?>
what is your code? in default $_POST settings is active in every server unless in server ignore posted data for securitical reasons. i think you mistake is in your code ,don't change XAMP settings
not change the settings of php
either you use get method in html file and use post in action file in both you should use post.

how do I POST two consecutive html tags to PHP script

I am sending cms updates from a textarea edited by CKeditor to a PHP handler using jQuery. The POST contains the HTML for the page. Not a problem, or so I thought.
Now when I send two consecutive tags like <p><strong> or even just >< the server throws a forbidden error.
I haven't had a problem with another site I built using the same principles, running on the same server, and I tried the consecutive tags scenario without a hitch.
I built a simple test(code below, a form with a textarea to POST to a PHP handler that echoes the results of the POST. Even if I don't even read the POST content the server still throws the error.
So it would appear that I have to process the HTML before POSTing.
I cannot for the life of me see what is different in the site that works as there is no data handling before POSTing.
Does anyone know of this problem, and any solutions?
Oh! and it works fine on my WAMP server(5.3.5)(public server is 5.2.17), so is there a PHP setting I could look at over-riding?
And now we have entered the twilight zone. I changed the textarea field name from name="test1" to S and there are no hitches??? So I now have an odd solution but this may not be the end of it, so If anyone is aware of what the issue could be, I'd be very thankful if not amazed.
Code:
post-form.php
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Post Form</title>
</head>
<body>
<form action="post-print.php" method="post">
<textarea name="test1" cols="50" rows="5"></textarea>
<!--name this and the php code text1 and it works-->
<textarea name="test2" cols="50" rows="5"></textarea>
<textarea name="test3" cols="50" rows="5"></textarea>
<input name="submit" type="submit" value="Send" />
</form>
</body>
</html>
post-print.php
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<p>Print result:</p>
<p><?php echo $_POST['test1']; ?></p>
<p><?php echo $_POST['test2']; ?></p>
<p><?php echo $_POST['test3']; ?></p>
</body>
</html>
First, you need to be sure about what are the values that your script containing the form is passing.
Change your script "post-print.php" (or "update_divs.php", not clear from your sample) to be something like this:
<?php
print_r($_POST);
?>

Categories