Basic test of checkbox submit button fails? - php

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

Related

I can send form data using GET method but not POST, why?

So I'm trying to learn html+php but it seems like I've hit a wall. If I use the GET method in my html form, parameters are sent to my php file just fine, but if I try to do the same using the POST method no parameters are sent.
#Edit: I've taken down the initial code sample displayed here as I found out its not a problem specific to that code. Instead I'm posting a basic form and a basic php script that have the same problem:
HTML FILE:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="testingForm.php" method="POST">
INPUT: <input type="text" id="iTesting" name="nTesting"/><br/>
<input type="submit" value="SUBMIT"/>
</form>
</body>
</html>
PHP FILE:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<title></title>
</head>
<body>
<?php
/* THIS WORKS: */
/*if (isset($_GET["nTesting"]))
echo "It is working! ".$_GET["nTesting"];
else
echo "It is NOT working! input: ".$_GET["nTesting"];
echo "<br/>".$_SERVER['REQUEST_METHOD']."<br/>";
echo "<br/>".var_dump($_GET);//*/
/* THIS DOESN'T: */
if (isset($_POST["nTesting"]))
echo "It is working! ".$_POST["nTesting"];
else
echo "It is NOT working! input: ".$_POST["nTesting"];
echo "<br/>".$_SERVER['REQUEST_METHOD']."<br/>";
echo "<br/>".var_dump($_POST);//*/
?>
</body>
</html>
As stated before, if I change the form method to GET, I get no problem at all. However, data doesn't seem to be sent when using POST method.
This is the output using the GET method:
It is working! input: test
GET
array(1) { ["nTesting"]=> string(4) "test" }
This is the output using the POST method:
It is NOT working! input:
POST
array(0) { }
Also, using the developers tool I can see there is a parameter nTesting:test in the formData section of the network tab. Yet, nothing is displayed.
If you're using Chrome
Go to index.html
Press f12. Developer tools will show up
Click Network
Tick Preserve Log and Disable Cache (I usually just do it that way)
From index.html click the submit button (do not close dev tools)
Go back to developer tools click 'teste.php'
Click headers, then expand general. You will see 'Request Method'. It should be POST
After working with #MuriloRM we ended up with the fundamental bug with PHPStorm
Please vote up the resolution of the issue
https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000097930-Can-not-use-POST-method-in-PhpStorm

Buffer (tampon) Page between two php pages

So I need a Buffer (Tampon) page between two pages. The first page contains an form that redirects you to a second page that sends out an email then redirects you back to the first page. I do have a Buffer page between them but it does not do what is supposed to do. I need to redirect to the buffer page as soon as I hit the POST button from the first page in order to disable it's functionality. So when I hit the button I want to be redirected to the buffer page witch will say Processing Request and then redirect to the second page. At the moment it does that but the buffer page is not displayed.
First page
<form method="POST" action="buffer.php">
<input text id='met' />
<input text id='myval' />
</form>
Buffer page
<!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>
</head>
<body>
Wait for redirect
</body>
<?php
$method=$_POST['met'];
$value=$_POST['myval'];
header('location:2ndpage.php?met='.$method.'&myval='.$value.'');
exit;
?>
</html>
Second Page
<?php
$method=$_GET['met'];
$value=$_GET['myval'];
mail('Send mail based on method and value');
header('location:2ndpage.php');
?>

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.

Cannot save array to session, comes up empty

If you can get this simple SESSION VARIABLES example to work, I'll hug you!
I can't get Session Variables to work at all on my site. I'm running php5, on Windows 7, using Chrome browser.
Here's the send page:
<?php
session_start();
$red='red';
$blue='blue';
$green='green';
$SESSVARS=array($red,$blue,$green);
$_SESSION['USERVARS']= $SESSVARS;
?>
<p>Set Sessionvars</p>
<form action="SessVarCheck.php" method="post">
<input name="Submit" type="submit">
</form>
Here's the result page:
<?php
session_start();
echo "val 1:".$_SESSION['USERVARS'][0];
echo "val 2:". $_SESSION['USERVARS'][1];
echo "val 3:". $_SESSION['USERVARS'][2];
?>
<!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" />
<title>SessVarCheck.php</title>
</head>
<body>
The Result page
</body>
</html>
I get empty on all three echos.
Any ideas would be welcome!
Make sure that the call to session_start() happens before any page contents are output, on both pages. (See "Note" section here)
(I assume you may already know about this, but given that I don't see a "<html>" on the send page I thought there might be more to that script. Also, I don't know how sensitive this requirement is - just the spaces before the php tag might be enough to be a problem.)
Try a few things:
1) do a var_dump($_SESSION) on your results page, to see what the raw contents of the session are
2) Verify that the sessions are working correctly. Add echo "SessionID: " . session_id(); to both scripts, somewhere AFTER the session_start() calls. If you don't get the same session ID on each page, you're getting a new session each time and it's most likely a cookie problem.

Getting input from text area and echoing it out

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.

Categories