I don't know how to deal with the error. i just want to pass the value to another page. i use "GET" method , it is okay. However, it is not working with "POST" method.
The error :
Undefined index: data in C:\Users\x\x\x\x\x on line 18
Testing.html:
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<form method="POST" action="testing.php">
<input name="data" type="text">
<input type ="submit" value="send">
</form>
</body>
</html>
testing.php
<?php
$data = $_REQUEST["data"];
if (isset($data)) {
echo $data;
}
else
{
echo "No data ";
}
?>
This example does work - irrelevant of the send method (GET / POST).These cases are handled (and you possibly should too):
Variable "data" NOT TRANSMITTED.
Variable "data" transmitted BUT EMPTY
Variable "data" transmitted and HAS CONTENTS
HTML code
<!DOCTYPE html>
<head>
</head>
<body>
<form method="POST" action="testing.php">
<input name="data" type="text">
<input type ="submit" value="send">
</form>
</body>
</html>
PHP script
<?php
if (isset($_REQUEST["data"])) {
$data = trim($_REQUEST["data"]);
if (strlen($data) > 0) {
echo "Data sent: $data";
} else {
echo "Data sent but EMPTY";
}
} else {
echo "NO DATA sent";
}
?>
Result
Either one of...
NO DATA sent
Data sent: myData
Data sent but EMPTY
IMHO you should check if data is actually being passed or not. change your php as below:
<?php
$data = (isset($_REQUEST["data"]) && $_REQUEST["data"] != "") ? $_REQUEST["data"]: "no data" ;
echo $data;
//this should print value of data (or "no data" if no value passed)
?>
You can't assign it to a local variable before checking if it is present. You need to call isset on the actual thing you care about:
if (isset($_POST["data"])) {
$data = $_POST["data"];
Try this:
<?php
$data=trim($_POST['data']);
if(empty($data){
echo "no data";
} else {
echo $data;
}
?>
Related
I tried to collect data from this form,as a quiz:
<form action="quiz.php" method = "post" id = "questions" name = "quiz">
<label>This is a quiz:</label><br>
<input type="radio" name = "vote1" value = "Yes"/><label>Yes</label>
<input type="radio" name = "vote1" value = "No"/><label>No</label>
<button type = "submit" name="send">Submit</button>
</form>
And I sent it to this php script:
<html>
<?php
if (isset($_POST['send'])){
if (isset($_POST["quiz"])){
if (isset($_POST['vote1'])){
if ($_POST['vote1'] === "Yes"){
echo "Yes,that is the answer.";
}else{
echo "Yes,that is the answer";
};
}};};
?>
<body>
</body>
</html>
But it didn't generate any HTML...
Although you can see in the url,that it definetely tried to pick up data:
http://localhost/php/quiz.php?vote1=Yes&send=
But it didn't really send data and I don't know what to do in that case,can anyone help me?...
It was not displaying output because of if (isset($_POST["quiz"]))
Below code of yours will work, and will display you echo result.
<html>
<?php
if (isset($_POST['send'])) {
if (isset($_POST['vote1'])) {
if ($_POST['vote1'] == "Yes") {
echo "Yes,that is the answer.";
} else {
echo "Yes,that is the answer";
};
}
};
?>
<body>
</body>
</html>
If the form method was GET you would get a querystring as described but the above form uses POST so that would not happen. The only form element of interest in the POST array really is vote1 so ensure that is present using isset - the other items such as the button or the form name ( ? which won't be there, ever! ) are not relevant.
A label element should be associated with a form element in one of two ways: either using the for=ID_of_element type syntax or by having the element as a child, as below.
<html>
<head>
<title>Quiz></title>
</head>
<body>
<form action="quiz.php" method="post" id="questions" name="quiz">
<h1>This is a quiz:</h1>
<label><input type="radio" name="vote1" value="Yes"/>Yes</label>
<label><input type="radio" name="vote1" value="No"/>No</label>
<button type = "submit" name="send">Submit</button>
</form>
<?php
if( isset( $_POST['vote1'] ) ){
$msg='';
switch( strtolower( $_POST['vote1'] ) ){
case 'yes':$msg='Yes,that is the answer.';break;
case 'no':$msg='Sorry, that is not correct';break;
}
echo $msg;
}
?>
</body>
</html>
I have a short sample php code above:
<HTML XMLns="http://www.w3.org/1999/xHTML">
<head>
<title>Check for perfect palindrome</title>
</head>
<body>
<h1>Check for perfect palindrome</h1>
<form method="post">
<label for="stringInput">String:</label><input type="text" id="stringInput" name="stringInput"><br/>
<br/><input type="submit" name="submit" value="Check"/>
</form>
</body>
<?php
if(isset($_POST['stringInput']))
{
$string = $_POST['stringInput'];
if ($string =="")
{
echo "Please fill the form";
} else if ($string == strrev($string))
{
echo "You entered: <b>'$string'</b> is a perfect palindrome.";
} else
{
echo "You entered: <b>'$string'</b> is NOT a perfect palindrome.";
}
}
?>
</HTML>
Imagine that the code is saved under file sample.php and located at localhost/sample.php.
I want to fill the form and trigger the submit button through this link:
localhost/sample.php?stringInput=abc&submit=Check
How can I do that? Thanks for help.
I need to use POST method because the actual form has many inputs not just one and I want to know how it will work with POST. And using PHP only if possible. (Javascript, jQuery are not the first choices). Cheers.
This is a good example to demonstrate what I need.
http://image.online-convert.com/convert-to-jpg?external_url=jhjhj&width=333
I tried GET method and the form doesn't display value.
If you want to include the parameters in the URL you cannot use POST
From wikipedia:
the POST request method requests that a web server accept the data enclosed in the body of the request message
Whereas in a GET request (from w3schools):
the query string is sent in the URL of a GET request
Try this:
You can assign your post values to variables & echo them in your input.
<HTML XMLns="http://www.w3.org/1999/xHTML">
<head>
<title>Check for perfect palindrome</title>
</head>
<body>
<?php
$string = "";
if(isset($_POST['stringInput']))
{
$string = $_POST['stringInput'];
if ($string =="")
{
echo "Please fill the form";
} else if ($string == strrev($string) )
{
echo "You entered: <b>'$string'</b> is a perfect palindrome.";
} else
{
echo "You entered: <b>'$string'</b> is NOT a perfect palindrome.";
}
}
?>
<h1>Check for perfect palindrome</h1>
<form method="post">
<label for="stringInput">String:</label><input type="text" id="stringInput" name="stringInput" value="<?php echo $_REQUEST['stringInput'];?>"><br/>
<br/><input type="submit" name="submit" value="Check" />
</form>
</body>
</HTML>
You are using the wrong http method instead of POST you should use GET
"Note that the query string (name/value pairs) is sent in the URL of a
GET request"
Check more about these two methods here: POST vs GET
It is not possible to read out the $_POST variable with this code. Can you please give me a hint where the problem is? The condition block does not work.
When reading out the POST content from the Browser the values seem to be set and transfered.
<!doctype html>
<?php
// select post operation
echo "post=".$_POST["action"];
if ($_POST["action"] == "add"){
// insert and read out values from DB
echo "add-".$_POST["action"];
header("Location:".($_SERVER['PHP_SELF']));
unset($_POST);
} elseif ($_POST["action"] == "delete"){
echo "add-".$_POST["action"];
header("Location:".($_SERVER['PHP_SELF']));
unset($_POST);
}
else{
echo "why else after submit.";
}
$_REQUEST = $_POST = $_GET = NULL;
?>
<html lang="de">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>test</title>
</head>
<body>
<div>
<form action="<? echo ($_SERVER['PHP_SELF']);?>" method="post">
<input type="hidden" name="action" value="add">
<input type="submit" value="Submit">
</form>
</div>
</body>
</html>
You never check if a POST was actually performed, so your if() chain ALWAYS executes. When you first hit the page, that loads it as a GET, therefore you get the why else after submit error.
You need something more like this:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
... check form submission, echo confirmation/errors, blah blah blah
}
... output html ...
Cause you redirect to an other page with
header("Location:".($_SERVER['PHP_SELF']));
actually you redirect to the same page so a brand new (same) page is served to you with an empty $_POST variable
I want to learn how to keep value after a post request.
In my code, I have these variables:
myvar
myans
result
myvar is a random val between 0-5. I will get myans from my input if myans == myvar, then result will be true (else it will be false).
I see myvar before I submit my ans just for trying, but although I see the var when I send it, what I see it is sometimes false and sometimes true. What am I missing?
example.php:
<?php
session_start();
if (!isset($_COOKIE['result'])) {
setcookie("result","EMPTY");
}
setcookie("myvar",rand(0,5));
if (!empty($_POST)) {
if ($_POST['myans'] == $_COOKIE['myvar']) {
setcookie("result","TRUE");
}
else {
setcookie("result","FALSE");
}
}
?>
<html>
<head>
<title>Example</title>
</head>
<body>
<form method="post" action="">
<?php echo $_COOKIE['myvar'] ?>
<input type="text" name="myans">
<input type="submit" value="Send">
<?php echo $_COOKIE['result'] ?>
</form>
</body>
</html>
The problem you were having was caused by your random number generator making creating a new number before comparing to the original. I made some changes to only set "myvar" if it's empty. this will only set it once, but at least you can see your code working as intended. I recommend you plan out what exactly what functionality you want before adding to it.
I also switched you out from the "$_COOKIE" var to "$_SESSION" vars. They are hidden from the client, and by php default last about 24 minutes if not refreshed. I don't know what you plan on using this for, but using cookies allows the end user to manipulate that info. If this is not a concern for you, by all means just uncomment the "setcookie()" lines.
<?php
session_start();
if (!isset($_SESSION['result'])) {
//setcookie("result","EMPTY");
$_SESSION["result"] = "EMPTY";
}
//setcookie("myvar",rand(0,5));
if(empty($_SESSION["myvar"])){
$_SESSION["myvar"] = rand(0,5);
}
//
if (!empty($_POST)) {
if ($_POST['myans'] == $_SESSION['myvar']) {
//setcookie("result","TRUE");
$_SESSION["result"] = "TRUE";
} else {
//setcookie("result","FALSE");
$_SESSION["result"] = "FALSE";
}
}
?>
<html>
<head>
<title>Example</title>
</head>
<body>
<form method="post" action="">
<?php echo isset($_SESSION['myvar']) ? $_SESSION['myvar'] : ""; ?>
<input type="text" name="myans">
<input type="submit" value="Send">
<?php echo isset($_SESSION['result']) ? $_SESSION['result'] : ""; ?>
</form>
</body>
</html>
I use PHP 5.6 and PHPstorm 10.
html code following.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test</title>
</head>
<body>
<form action="hello.php" method="post">
<input type="text" name="blabla">
<input type="submit">
</form>
</body>
</html>
PHP code following .
<?php
echo $_POST["blabla"];
?>
But if I change the method to GET.
It will be work. But I can not get the post value.
Then I install WAMP Server.when just use WAMP Server to access,it worked.why?why I can not use PHPStorm to get the post value.
You need to check "Request Type" by below way:-
$method = $_SERVER['REQUEST_METHOD'];
if ($method == 'POST') {
// Method is POST
echo "post";
$data = isset($_POST["blabla"]) ? $_POST["blabla"] : 'notset';
echo $data; // print data
} elseif ($method == 'GET') {
// Method is GET
echo "get";
} else {
// Method unknown may be put or delete
echo "unknown";
}
Hope it will help you :)
remove method = "post" of the form
PHP code following .
<?php
echo $_REQUEST["blabla"];
?>