PHP $_POST error Please Help me I am learning PHP - php

I am learning PHP. Here is the source code.
<?php
$text = $_POST['text'];
echo $text;
?>
<form action="index.php" method="post">
<input type="text" name="text" />
<input type="submit">
</form>
Here is the result. I don't know where is the problem.
Notice: Undefined index: text in C:\xampp\htdocs\faisal\index.php on line 2

It means there's nothing in $_POST['text'] -- and there won't be, until after the form is submitted. You need to use isset() to check:
<?php
if(isset($_POST['text'])) {
$text = $_POST['text'];
echo $text;
}
?>
<form action="index.php" method="post">
<input type="text" name="text" />
<input type="submit">
</form>

When you first go to the page your special variable "$_POST" is empty, that is why you are getting an error. You need to check to see if anything is in there.
<?php
$text = '';
if(isset($_POST['text']))
{
$text = $_POST['text'];
}
echo 'The value of text is: '. $text;
?>
<form action="index.php" method="post">
<input type="text" name="text" />
<input type="submit">
</form>

$_POST['text'] is only populated when the form is submitted. So when the page is first load it does not exist and you get that error. To compensate you need to check to see f the form is submitted before executing the rest of your PHP:
<?php
if ('POST' === $_SERVER['REQUEST_METHOD']) {
$text = $_POST['text'];
echo $text;
}
?>
<form action="index.php" method="post">
<input type="text" name="text" />
<input type="submit">
</form>

You pprobably have to detemine if the form has been submitted or not.
<?php
if (isset($_POST['text'])) {
$text = $_POST['text'];
echo $text;
}
?>
<form action="index.php" method="post">
<input type="text" name="text" />
<input type="submit">
</form>
Alternativly you can use $_SERVER['REQUEST_METHOD'].
if ($_SERVER['REQUEST_METHOD'] == 'POST') {...

We have to check whether user clicked on submit button or not, if yes then we have to set $test variable. If we will not use isset() method, we'll always get error.
<?php
if(isset($_POST['submit']))
{
$text = $_POST['text'];
echo $text;
}
?>
<form action="index.php" method="post">
<input type="text" name="text" />
<input type="submit" name="submit" value="submit">
</form>

Related

calling php function from submit button

I want to pass the value of input field address to php. So that when i click on submit button it will be store in php.
<input type="text" name="address">
<input type="submit" name="go" method="post">
<?php
if($_POST){
if(isset($_POST['go'])){
call();
}else{
echo "Error";
}
}
function call(){
echo "hi";
print(document.address.value);
}
?>
However when i click on button it response nothing.
You are mixing PHP and JavaScript. If you want a pure PHP solution you could do something like:
<?php
if(isset($_POST['go'])){
$address = $_POST['address'];
echo $address;
}
?>
<form method="POST">
<input type="text" name="address">
<input type="submit" name="go" method="post">
</form>
With PHP once the form is submitted you can access form values with $_POST so use $_POST['address'] instead of document.address.value
what you intend to do can be done as below method.
<form method="post" >
<input type="text" name="address">
<input type="submit" name="go">
</form>
<?php
if($_POST){
if(isset($_POST['go'])){
call();
}else{
echo "Error";
}
}
function call(){
echo "hi";
echo $_POST['address'];
}
?>
Try like this,this is working fine as per your requirement:
<html>
<body>
<form action="" method="post">
Address: <input type="text" name="name"><br>
<input type="submit" name="go" value="call" />
</form>
<?php
if (isset($_REQUEST['name'])) {
call();
}
?>
<?php
function call() {
$address= $_POST['name'];
echo "Address:".$address;
exit;
}
Output:-
For output Click here
use this code
<input type="text" name="address">
<input type="submit" name="go" method="post">
<?php
if($_POST){
if(isset($_POST['go'])){
call();
}else{
echo "Error";
}
function call(){
echo "hi";
echo $_POST['address'];
}
?>

submitting result to the end of the url instead of adding an index.php

Id basically like the below submission to place text at the end of the url
example of what i want
http://example.com/(text) -- without the () obviously
example of what i don't want -- http://www.example.com/index.php?firstname=text
<form action="(end of current url)">
<fieldset>
search name
<br>
<input type="text" name="search" value="name">
<br>
<input type="submit" value="Submit"></fieldset>
</form>
id like to fix this via html or php either will do aslong as it submits the request to that :)
thank you in advance.
Using the POST method instead of GET.
<form action="" method="POST">
<fieldset>
search name
<br>
<input type="text" name="search" value="name">
<br>
<input type="submit" value="Submit"></fieldset>
</form>
In short you do the following:
<?php
// Get posted text
$text = strtolower(mysql_real_escape_string($_POST['text']));
// Do some cleanup here
// Redirect to page
if ($text != ''){
header( 'Location: http://www.example.com/' . $text );
}
// HTML output below (not before)
?>
<form method="post" action="">
<fieldset>
Search name<br />
<input type="text" name="text" /><br />
<input type="submit" value="Submit" />
</fieldset>
</form>

php POST array not set

i am trying to collect data from the user in a form and display the data back to him .
i am using WAMP.
here is my html code
<FORM METHOD="POST" ACTION="submit.php">
<INPUT type="text" name="URL" size=17 value="http://">
<INPUT type="text" name="user" size=17>
<INPUT type="text" name="email" size=17>
<INPUT type="submit" value="Submit" name="submit"/>
<INPUT type=reset value="Clear">
</form>
here is my submit.php code
<?php
if (isset($_POST['URL'])){
echo "set";
}
else
{
echo "not set";
}
?>
when i execute this i am always getting "not set" as the output.
thanks.
It should be $_POST not $post, so your code should be :-
<?php
if (isset($_POST['URL'])){
echo "set";
}
else
{
echo "not set";
}
?>
Also your form tag should not contain encytype="text/plain" because PHP doesn't handle it (and it is not a bug)
Valid values for enctype in <form> tag are:
application/x-www-form-urlencoded
multipart/form-data
So remove encytype="text/plain"
<?php
if (isset($_POST))
{
echo "set";
}
else
{
echo "not set";
}
?>
try this
Remove
ENCTYPE="text/plain"
from the form
So the form should look like
<form method="POST" action="submit.php">
And its $_POST not $POST
if (isset($POST['URL'])){
should be
if (isset($_POST['URL'])){
Here is an example of handling the form
<?php
if(isset($_POST["submit"])){
if (isset($_POST['URL']) && $_POST['URL'] != ''){
echo "set";
}
else
{
echo "not set";
}
}
?>
<FORM METHOD="POST" ACTION="submit.php" >
<INPUT type="text" name="URL" size=17 value="http://">
<INPUT type="text" name="user" size=17>
<INPUT type="text" name="email" size=17>
<INPUT type="submit" value="Submit" name="submit"/>
<INPUT type=reset value="Clear">
</form>

How do I multiply values in PHP?

I am trying to find a basic input where user enters one number and the second number and then multiplies it.
I got it to work without the isset function, but now I am trying to echo out the error line when the page first starts up. If you see the input it is named, name and name2 so I call them in PHP.
My original code did not use isset and it worked but I got error before any input. This is my PHP code:
<html>
<style>
<?php include 'style.css';?>
</style>
<body>
<form method="post">
<p> Enter Value 1:<input type="text" name="name"> <br>
<p> Enter Value 2:<input type="text" name="name2"><br>
<input type="submit" value="send">
</form>
<br>
<h3>Your input:</h3><br>
<?php
if (isset($_POST['name'])) && (isset($_POST['name2'])){
$num=$_POST['name'];
$num2=$_POST['name2'];
echo $num*$num2;
}
else{
echo '';
}
?>
</body>
</html>
You have closed your IF parentheses too soon. The line should be like this:
if (isset($_POST['name']) && isset($_POST['name2'])) {
This is working code you have some extra parenthesis. If you are multiplying integer values from user always use intval function so that you always have integer value. If user enters string or characters it intval will change to zero
<html>
<style>
<?php include 'style.css';?>
</style>
<body>
<form method="post">
<p> Enter Value 1:<input type="text" name="name"> <br>
<p> Enter Value 2:<input type="text" name="name2"><br>
<input type="submit" value="send">
</form>
<br>
<h3>Your input:</h3><br>
<?php
if (isset($_POST['name']) && isset($_POST['name2'])){
$num = intval($_POST['name']);
$num2 = intval($_POST['name2']);
echo $num*$num2;
}
else{
echo '';
}
?>
Try this I think it is helpful to you:
<form method="POST">
<input type="text" name="value1" placeholder="Enter 1st Value" required>
<input type="text" name="multiply" value="*" readonly>
<input type="text" name="value2" placeholder="Enter 2nd Value" required>
<input type="submit" name="submit" value="Calculate">
</form>
<?php
if(isset($_POST['submit'])){
$value1 = $_POST['value1'];
$multiply = $_POST['multiply'];
$value2 = $_POST['value2'];
if($multiply == "*"){
echo $value1*$value2;
}
}
?>
The main problem is paranthesis are not closed properly it is
if(condition1)&& (condition2){
}
it should be
if((condition1)&&(condition2)){
}
you can use single condition for this also as shown in below code
<style>
<?php include 'style.css';?>
</style>
<body>
<form method="post">
<p> Enter Value 1:<input type="text" name="name"> <br>
<p> Enter Value 2:<input type="text" name="name2"><br>
<input type="submit" value="send" name="send">
</form>
<br>
<h3>Your input:</h3><br>
<?php
//if (isset($_POST['name'])) && (isset($_POST['name2'])){ problem is here your paranthesis are not closed properly
if (isset($_POST['send'])){ //use this as this will ensure that your send button is clicked for submitting form
$num=$_POST['name'];
$num2=$_POST['name2'];
echo $num*$num2;
}
else{
echo '';
}
?>
</body>
</html>

access $_GET value after form submission

example.php gets value of x by $_GET['x'] from another page.
I have a form in the same page (example.php). Upon submission of the form, I am loosing the value of x.
My question is: How can I keep and access the value of x after submission of the form.
Code is like this:
<body>
<?php
if(isset($_GET['y'])) {
$y = $_GET['y']; // I have also tried with $_REQUEST
}
if($_POST['submit']) {
$x = $_POST['field1'];
echo $y."<br>";
echo $x;
}
?>
<form name="form1" action="example.php" method="post">
<input type="text" name="field1" />
<input type="submit" name="submit" value="Echo the name" />
</form>
</body
I have also tried the following as per suggestion, but its not working:
<body>
<?php
if(isset($_POST['submit'])) {
$x = $_POST['field1'];
$y = $_POST['y'];
echo $y."<br>";
echo $x;
}
?>
<form name="form1" action="dollar_get_and_form1.php" method="post">
<input type="text" name="field1" />
<input type="hidden" name="y" value="<?php htmlentities($_GET['y']) ?>" />
<input type="submit" name="submit" value="Echo the name" />
</form>
</body>
Issue is solved by the following codes:
<body>
<?php
if(isset($_GET['x'])) {
$x = $_GET['x'];
echo $x;
}
if(isset($_POST['submit'])) {
echo $_POST['xValue'].'<br>';
echo $_POST['y'];
}
?>
<form name="form1" action="example.php" method="post">
<input type="text" name="y" />
<input type="submit" name="submit" value="Echo the name" />
<input type="hidden" id="xValue" name="xValue" value="<?php echo $x; ?>"/>
</form>
</body>
Thanks to all for your suggestions. If there are better ways to do this, please suggest so.
Your form is POST not GET so the variable will be gone. The best way to get the desired result is through a hidden form field:
<input type="hidden" name="y" value="<?=htmlentities($_GET['y'])?>" />
Then it will be available as $_POST['y'] when the user submits the form
your form's method is post so, there is not $_GET, and $_GET don't work here...
where is $_GET['y'] comming from?
As HTTP is stateless protocol, Get/Post value persistence is limited to last page and current page. You have to make you use of one of techniques :
"hidden field","session" ,"cookie" or writing/reading to tmp. file to preserve value/state.
use get instead of post method='get'; and create a field(hidden) y like following
<body>
<?php
if(isset($_GET['y'])) {
$y = $_GET['y']; // I have also tried with $_REQUEST
}
if($_GET['submit']) {
$x = $_GET['field1'];
echo $y."<br>";
echo $x;
}
?>
<form name="form1" action="example.php" method="get">
<input type="text" name="field1" />
<input type="hidden" name="y" value=1 />
<input type="submit" name="submit" value="Echo the name" />
</form>
</body

Categories