why i cant view what i fill up in my html form. is that something wrong with my code in php code?
<html>
<head>
<title>What's your name?</title>
</head>
<body>
<h1>What's your name?</h1>
<h3>Writing a form for user input</h3>
<form method = "post" action = "User.php">
Please type your name:
<input type = "text" name = "userName" value = " "><br>
<input type = "submit">
</form>
</body>
</html>
Code:
<html>
<head>
<title>Hi User</title>
</head>
<body>
<h1>Hi User</h1>
<h3>PHP program that receives a value from "whatsName"</h3>
<?
print("<h3>Hi there , $userName </h3>");
?>
</body>
</html>
Unless you have Register Globals on (which you shouldn't so turn it off if so), the form variables won't automatically be expanded, so you need to pick them up from the $_POST array:
if($_SERVER['REQUEST_METHOD'] == 'POST'){
print("<h3>Hi there , " . htmlspecialchars($_POST['userName']) . "</h3>");
}
Maybe you have been used to badly configured servers running with register_globals turned on.
Or maybe you have moved to a version of PHP where register_globals has been removed i.e. PHP5.4 or greater.
You should address any data coming from a HTML <form> using the proper
$_POST['variableName']
or
$_GET['variableName']
With that in mind your code might look like this
print('<h3>Hi there , ' . $_POST['userName'] . '</h3>');
Note: You should really be sanity checking the values passed in this type of data, and also checking if it is actually there. Although you should have been doing that anyway even if register_globals was turned on.
Related
What I have learnt is: At a time, only HTTP POST or GET method is possible. I have the following piece of code named: index.php
<?php
if($_SERVER['REQUEST_METHOD'] == "POST") {
echo "Request Method is: ". $_SERVER['REQUEST_METHOD'] .'<br>';
echo "Get variable is: " . $_GET['getname'] . '<br>';
echo "Post variable is: " . $_POST['posttitle'];
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Testing Get or Post</title>
</head>
<body>
<form action="" method="POST">
<input type="text" name="posttitle" value="somepost"/>
<button type="submit">Submit</button>
</form>
</body>
</html>
I navigate the form via : http://localhost/testing/index.php?getname=someget. I submit the form, and the form data is sent via HTTP POST method.
Now my question is:
In this scenario, the HTTP method is POST, and the GET variable $_GET['getname'] should have been unavailable. But, both POST and GET variables are available and printed.
This is just down to PHP having poor names for $_GET and $_POST.
$_GET will contain data from the query string of the requested URL. This is completely independent of the request method used.
PHP probably picked the name because an HTML form with method="GET" will put the data in the query string, but that isn't the only way a query string can be created.
I am writing a PHP code in Adobe Dreamweaver. My code is as shown below. I am expecting my code to output two boxes, into which I write something. And when I click on the submit button, I expect to see the two separate things that I entered into the box to be concatenated. But I'm not getting a concatenation. In fact, NOTHING happens when I click on submit. I am confused. Is this code not something I should be using Dreamweaver for? I am relatively new to PHP and I do not understand all the bits. My suspicion is that Dreamweaver does not recognize "twoFieldForm.php". Can someone please tell me how I can overcome this issue?
<!DOCTYPE html>
<html>
<head> </head>
<body>
<h1>Form Filler</h1>
<?php
function joined($s1, $s2) {
return array($s1.$s2, strlen($s1.$s2));
}
$first = $_GET[fieldOne];
$second = $_GET[fieldTwo];
$answer = joined($first, $second);
echo "The concatenated string is \"{$answer[0]}\"
and its length is \"{$answer[1]}\";
?>
<form action = "twoFieldForm.php" method = "get">
Field 1 : <input type = "text" name = "fieldOne"/>
Field 2 : <input type = "text" name = "fieldTwo"/>
<input type = "submit">
</form>
</body>
</html>
<!DOCTYPE html>
<html>
<head> </head>
<body>
<h1>Form Filler</h1>
<?php
function joined($s1, $s2) {
return array($s1.$s2, strlen($s1.$s2));
}
$first = $_GET['fieldOne'];
$second = $_GET['fieldTwo'];
$answer = joined($first, $second);
echo "The concatenated string is \"{$answer[0]}\"
and its length is \"{$answer[1]}\"";
?>
<form action="" method="GET">
Field 1 : <input type="text" name="fieldOne"/>
Field 2 : <input type="text" name="fieldTwo"/>
<input type="submit">
</form>
</body>
</html>
Explanation: You have to add quotes in $_GET['']. Remember indention please, always when I see people using adobe dreamweaver, their code is horrible... If you are refering to the same file, you don't have to use an action="somewhat.php". Just leave it empty. Aswell you have missed a " after your echo statement.
This will work now. Please start using a good IDE then you won't have those basic mistakes because the IDE will show you your mistakes already...
Try add quotes:
$first = $_GET['fieldOne'];
$second = $_GET['fieldTwo'];
$answer=$_GET['fieldOne'].$_GET['fieldTwo'];
echo "concatenated string is:".$answer;
try this...
I have a simple form and I'm trying to pass the form variable to php and output the value. I have tried to solve this myself with the almighty google but wasn't successful. The html form code is as follows
<html>
<head>
<title>Form</title>
</head>
<body>
<form method="post" action="test1.php">
<input type="text" name="username">
<input type="submit">
</form>
</body>
</html>
Then the php to handle the form is:
<html>
<head>
<title>Form</title>
</head>
<body>
<?php
echo "<h1>Hello " . $_POST["username"] . "</h1>";
?>
</body>
</html>
The output I'm getting no matter what I type into the html form is , Hello " . $_POST["username"] . ""; ?>
I don't know why it also outputs the ending semi colon and ending php tag also, maybe this is evidence of what's going wrong here?
PHP is
misconfigured or
not configured or
not working or
not working within HTML files.
You have to configure your webserver that it parses PHP inside HTML files, see here for example:
=> Server not parsing .html as PHP
The syntax is fine, try to write a sample code snippet like below
<?
$a = "hello word!";
echo $a;
?>
and check if php is working.
I came across someone asking the question,
How can I pass a variable up the page (on the same page?).
I had a think about it but couldn't think of how to do it myself, so I was wondering if it is even possible?
So what he was trying to do was change the value of $a at the top of the page at the same time as the bottom value of $a.
Is it possible? If so how?
Thanks in advanced.
<?php
echo("Begining " . $a);
?>
<html>
<head>
<title>Test Variables</title>
</head>
<body>
<form>
<form action="<?=$PHP_SELF?>" method="POST">
<input type="TEXT" name="testf" size="10">
<input type="submit" name = "submit" value = "submit"></form><?php
if ("submit" == $submit) {
$a = $testf;
echo( "Bottom " . $a);
}
?>
</body></html>
Edit:
After seeing answers, maybe it can be done with jquery, ajax or javascript?
No, you'll need to move the if statement to the top, and any variables you calculate that need to be in the if statement also to the top.
Something like:
<?php
if ("submit" == $submit) {
$a = $testf;
}
echo("Begining " . $a);
?>
<html>
<head>
<title>Test Variables</title>
</head>
<body>
<form>
<form action="<?=$PHP_SELF?>" method="POST">
<input type="TEXT" name="testf" size="10">
<input type="submit" name = "submit" value = "submit"></form><?php
echo( "Bottom " . $a);
?>
</body></html>
It's considered a good practice to have all the logic before you start outputting the HTML anyways. Your HTML should ideally have as less logic as possible.
More info: https://stackoverflow.com/a/95027/320615 and https://stackoverflow.com/a/1088791/320615
You can probably bend over backwards to make that work somehow.
But the real answer is to handle all your business logic before you start outputting any HTML. You need to decide at the beginning of your code whether the current request is a form submission or not and set variables and HTML templates accordingly. Never mix business logic into the middle of your HTML templates.
You have to use the $_POST['testf'] and $_POST['submit'] variable.
And also check if it exists with isset : php manual isset
<?php
echo("Begining " . $a);
?>
<html>
<head>
<title>Test Variables</title>
</head>
<body>
<form>
<form action="<?=$PHP_SELF?>" method="POST">
<input type="TEXT" name="testf" size="10">
<input type="submit" name = "submit" value = "submit"></form><?php
if ("submit" == $submit) {
$a = $_POST['testf'];
echo( "Bottom " . $a);
}
?>
</body></html>
You can't do it in PHP, but you can't change HTML once it's fully loaded using javascript (and jquery to make it easier).
EDIT : ok I read your code a bit quickly the first time :
I don't understand the echo before the <html> tag, doesn't seem right. Also you want to give the value of a POST var to $a, so just :
if(isset($_POST['testf'])) {
echo $_POST['testf'];
}
I'm trying to write a program where the basic idea is I ask the user for input in a textarea, and then the text gets stored into a word file. Here is the code I'm trying to use:
<html>
<head>
<title>Simple Guestbook</title>
</head>
<body>
<h1>Simple Guestbook Comment Creator</h1>
<br>
<form method = "post"
action = "mysite.php">
<textarea name = "text"
rows = "10"
cols = "20">Write Here</textarea>
<input type = "submit"
value = "Submit Comment">
</form>
<?
if($_POST['text'] !== NULL){
$comment = $_POST['text'];
$file = fopen("texttest.txt", "a");
fputs($file, "<br>\n$comment");
fclose($file);
}
?>
</body>
</html>
I can't seem to get this to work properly. I was also thinking about somehow making the form action store the text and then reload the site, but I haven't gotten that to work (the original file is mysite.php, so the action is to just reload the page).
If anyone has any better ideas of an algorithm to use/different syntax to use, please let me know, as I just started learning basic PHP syntax.
Thanks
Check the following:
Does php have the permission to write files in that directory?
Is that php file called "myfile.php"?
Anyway, when something does not work and you want to know what's causing the arror, place error_reporting(-1); at the beginning of your php - it will output any error or warning, including the ones trown by fopen().
Also, you might want to check whether the variable has been correctly submitted: echo $comment right after you assign it.
Something like this might work.
You might want to do more with the values they are entering and all, but this will basically do what you are asking.
You will also want to make sure that you have the correct path of the file you are trying to write to and that that file has the correct permissions to allow it to be written to:
<html>
<head>
<title>Simple Guestbook</title>
</head>
<body>
<h1>Simple Guestbook Comment Creator</h1><br>
<?php
if (isset($_POST['submit'])) {
if (strlen(trim($_POST['comment']))) {
$file = fopen("texttest.txt", "a");
fputs($file, "$_POST['comment'])\n");
fclose($file);
}
} else {
?>
<form method = "post" action = "<?php echo($_SERVER['PHP_SELF']); ?>">
<label>Leave your comment
<textarea name="comment" rows="10" cols="20"></textarea>
</label>
<input type="submit" name="submit" value="Submit Comment" />
</form>
<?php
}
?>
</body>
Also, since you are returning to the same page you may want to put some kind of message letting the person know that they succeeded in entering something into your address book.