Now i made a simple form with 2 input one for name and one for button
i want in php file echo name input
<form action="del.php">
<input type="text" name="name" />
<input type="submit" name="btn" value="button"/>
</form>
in del.php
<?php
session_start();
$_SESSION['name'] = $_POST['name'];
echo "".$_POST['name']."";
giving me white screen when every time
i want to echo any thing that typed in this input
You are missing method attribute in your form.
<form action="del.php" method="POST">
<input type="text" name="name" />
<input type="submit" name="btn" value="button"/>
</form>
The method attribute specifies how to send form-data (the form-data is sent to the page specified in the action attribute).
The form-data can be sent as URL variables (with method="get") or as HTTP post transaction (with method="post").
Source: https://www.w3schools.com/tags/att_form_method.asp
the default method when a form is submitted is GET, you need to specify the POST method (method="post") in your <form> tag:
<form action="del.php" method="post">
Your form is not POSTing its GETing (see the url params which will be set example.com?name=) this is because you not set the from method for POST:
<form action="del.php" method="post">
<input type="text" name="name" />
<input type="submit" name="btn" value="button"/>
</form>
And within your PHP you should check its post and check that the values are set:
<?php
session_start();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = isset($_POST['name']) ? $_POST['name'] : null;
// set your session - not sure you need this :/
$_SESSION['name'] = $name;
// echo out your result but also protect from XSS by using htmlentities
echo htmlentities($name);
}
Related
I am trying to get the value of a form (text field) with _POST, and store it to a text file but it doesn't work. Here's my code:
HTML
<form>
<input type="text" name="test" id="test" value="">
<input type="button" onclick="location.href='example.com/index.php?address=true';" value="ODOSLAŤ" />
</form>
PHP
if (isset($_GET['address'])) {
$email = $_POST['test'];
$myfile = fopen("log.txt","a") or die("Error.");
fwrite($myfile, "\n".$email);
// this prints nothing
echo $email;
}
I can't get the value of that text field. Nor GET nor POST doesn't work for me. What am I doing wrong?
You are missing a post method in your form.
<form method="post" action="example.com/index.php?address=true">
<input type="text" name="test" id="test" value="">
<input type="submit" value="ODOSLAŤ" />
</form>
You also have to change the following line.
if (isset($_GET['address'])) {
With
if (isset($_POST['address'])) {
You want to post after triggering an action as FreedomPride mentioned
Conclusion
You want to declare for example a post methodif you know that you would like to post that data in the future.
As FreedomPride also mentioned :
You are using a GET, but if a user does not input anything your script won't work, there by it is recommended to use a POST
You are not submitting your form.
Change your code as follows....
<form method="post" action="example.com/index.php?address=true">
<input type="text" name="test" id="test" value="">
<input type="submit" value="ODOSLAŤ" />
</form>
You'll get what you want....
This is what you're missing as Tomm mentioned.
<form method="post" action="yourphp.php">
<input type="text" name="address" id="test" value="">
<input type="submit" name="submit"/>
</form>
In your PHP, it should be post if it was triggered
if (isset($_POST['address'])) {
$email = $_POST['test'];
$myfile = fopen("log.txt","a") or die("Error.");
fwrite($myfile, "\n".$email);
// this prints nothing
echo $email;
}
Explanation :-
In a form, an action is required to pass the action to the next caller with action. The action could be empty or pass it's value to another script.
In your PHP you're actually using a GET. What if the user didn't input anything. That's why it's recommended to use POST .
I have following input type
<input id="crp-project-title" class="crp-project-title" type="text" placeholder="Enter project title" value="EXTERIOR" name="project.title">
Its value is value="EXTERIOR". I want to fetch the value and store in a variable. I am not getting any idea regarding this. I am trying to modify plugin (career portfolio) according to my project and its part of that.
Your html form:
<form method="POST" action="process.php">
<input type="text" name="foo">
<input type="submit">
</form>
Your Php form processing page (process.php):
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$foo = filter_input(INPUT_POST, 'foo', FILTER_SANITIZE_STRING);
}
?>
This is front end code, if you are trying to move front end data to server side it must pass through the server. i.e. $_POST...
now you can either do this with AJAX / PHP or PHP / HTML only
<?php
if(isset($_POST[project.title])
$var = $_POST[project.title]; //its now in a varible
?>
<form method="POST" action="thispage.php">
<input id="crp-project-title" class="crp-project-title" type="text" placeholder="Enter project title" value="EXTERIOR" name="project.title">
<input type=submit value="PRESS ME TO SUBMIT VALUE TO VAR">
</form>
I have a script that users can submit certain data, and after a submit they can "review" the output and go back to previous page or submit to it again and there is my proble, the submitted page: How can I submit it again to write it to a file?
form.html
<form method="post" action="vaihe2.php">
<input name="laskuttaja" type="text" value="Nimi" size="25">
<input name="submit" type="submit" value="Lähetä" />
</form>
vaihe2.php
<?
$laskuttaja = $_POST['laskuttaja'];
$data = '<B>'.$laskuttaja.'</b>';
echo $data;
?>
So how can I post that $data to next page(vaihe3.php) with submit and let the script write it to a file. I know how php write file works but the post to third page is not working.
If you wat to go back, the secret is in the value of the input.
<input name="laskuttaja" type="text" value="<?php echo(isset($_POST['laskuttaja'])?$_POST['laskuttaja']:"Nimi";?>" size="25"/>
To 'save' data to the next page use $_SESSIONs. They're simple to use. Just remember everywhere you use them, you must have session_start(); on LINE 1! Can't stress that enough!
$_SESSION['data']=$data;
on your third page:
echo$_SESSION['data'];
More on sessions here.
In vaihe2.php
<form method="post" action="vaihe3.php">
<?
$laskuttaja = $_POST['laskuttaja'];
$data = '<B>'.$laskuttaja.'</b>';
echo $data;
echo "<input name=\"laskuttaja\" type=\"hidden\" value=\"".$laskuttaja."\" size=\"25\">";
?>
<input name="submit" type="submit" value="anything" />
</form>
Here you are passing laskuttaja as hidden field and on post will be available to you in third page.
Now data flow as per your requirement. User fills data in form.html -> reviews on vaihe2 and confirms -> gets written in vaihe3.
Could you post the form conditionally back to itself until validated by checkbox? the action would change to "vaihe3.php" ?
<form method="post" action="<?php if ($_POST["valid"]==1) {echo 'vaihe3.php';} ?>">
<input name="laskuttaja" type="text" value="<?php if ($_POST['laskuttaja']!=='') {echo '$_POST[laskuttaja]'} else {echo 'Nimi';} ?>" size="25">
<?php if (isset ($_POST['laskuttaja') && $_POST['laskuttaja']!=="") {
echo 'Please Confirm your answers: <input name="valid" type="checkbox" value="1" />'; } ?>
<input name="submit" type="submit" value="Lähetä" />
</form>
Otherwise, the mention above about CURL would be another option. Or - since your using PHP anyways, you could write the values of form submission to a session array and make them available to all pages until you empty the array.
I have a form field:
<input type="text" value="" name="Email" id="Email">
and my form action confirmation url is this:
http://.../index.php?Email=<?php echo $_POST['Email']; ?>
However after submitting, the Email parameter is not coming through. Is this something that can be done in PHP, or does it only read the field on initial page load?
Thanks
Your issue is that you are mixing $_GET and $_POST.
See your code here, http://.../index.php?Email=<?php echo $_POST['Email']; ?>, when you post to that one, there will no longer be a $_POST['Email'], but a $_GET['Email']. So the first post will likely work (if you are using <form method="post" action="...">), but the second submit will fail, as $_POST['Email'] no longer exists.
So I recommend that you don't use parameters in the action. Instead, put them in a hidden field or switch to only $_GET parameters.
Option 1, use hidden field
Change the form on your second page to:
<form action="http://.../index.php" method="POST">
<input type="hidden" name="Email" id="Email" value="<?php echo $_POST['Email'];?>" />
...
</form>
Option 2, use only $_GET
Change the form on your first page to <form ... method="GET">
And then change the form on the second page to use $_GET['Email'] and the method to GET.
<form action="http://.../index.php??Email=<?php echo $_GET['Email'];?>" method="GET">
...
</form>
Option 3, Use $_REQUEST instead of $_POST
Simply use http://.../index.php?Email=<?php echo $_REQUEST['Email']; ?> as your action url, as $_REQUEST is a merge of $_GET and $_POST. Be aware that this is a merge of $_GET, $_POST and $_COOKIE.
It depends on the your FORM method
Your form should be
<form method='post' action='http://.../index.php'>
<input type="text" value="" name="Email" id="Email">
<input type='submit' value='Post data'>
</form>
and to access email in index.php you can write code as below
<?php
$emailValue = $_POST["Email"];
//Use variable for further processing
?>
if your form is as below (please check that method is get
<form method='get' action='http://.../index.php'>
<input type="text" value="" name="Email" id="Email">
<input type='submit' value='Post data'>
</form>
and to access email in index.php you can write code as below
<?php
$emailValue = $_GET["Email"];
//Use variable for further processing
?>
You don't need to define the structure of the GET request; that's what the form does.
For instance:
<form action="workerbee.php" method="GET">
<input type="text" name="honey_type" value="sweet" />
</form>
...when submitted, would automatically append the field - honey_type - to the URL for you. It would end up like this:
http://example.com/workerbee.php?honey_type=sweet
You can then access the value via $_GET['honey_type'] in workerbee.php. To pre-fill the form with the existing submitted value - assuming that workerbee.php holds the form - just add a conditional value parameter:
<?php
$honey_type = !empty($_GET['honey_type']) ? $_GET['honey_type'] : null;
?>
<input type="text" name="honey_type" value="'<?php echo htmlspecialchars($honey_type); ?>'" />
If you're trying to use data from your current form, your form tag should read as follows:
<form action="http://.../index.php" method="GET">
If you're trying to pass data your server already has (such as from a previous form), then you should use a hidden field instead:
<input name="email" type="hidden" value="<?php echo $_POST['Email']; ?>">
I have a simple registration form.
I want to pass value entered in one page to other in a text field.
how to pass and access it from next page in php.
this is my first php page.
Thanks in advance.
You can add hidden fields within HTML and access them in PHP:
<input type="hidden" name="myFieldName" value="someValue"/>
Then in PHP:
$val = $_POST['myFieldName'];
If you're going to ouput this again you should use htmlspecialchars or something similar to prevent injection attacks.
<input type="hidden" name="myFieldName" value="<?=htmlspecialchars($_POST['myFieldName']);?>"/>
Suppose this the form input in page A
<form name="" action="" method=post enctype="multipart/form-data">
<input type="text" name="myvalue" value="">
<input type=submit>
</form>
In page B
In the page you want to get values put this code
<?PHP
foreach ($_REQUEST as $key => $value ) {
$$key=(stripslashes($value));
}
?>
<form name="" action="" method=post enctype="multipart/form-data">
<input type="text" name="myvalue" value="<?PHP echo $myvalue" ?>">
<input type=submit>
</form>
So yo can use or attach variable value to another form do what else you want to do
use following code, that should help you.
<form action ="formhandler.php" method ="POST" >
<input name = "inputfield" />
<input type="submit" />
</form>
on formhandler.php file yo need to enter following code to get the value of inputfiled.
$inputfield = isset($_POST['inputfield'])?$_POST['inputfield']:"";
// now you can do what ever you want with $inputfield value
echo($inputfield);