Can't get form value with PHP - php

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 .

Related

$_POST doesn't work with my form

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);
}

How to send a get form wthout losing get parameters

I have a form located at a url containing get parameters,my form is also using this method.When the form is submitted it rewrites the previos get parameters.
Is there a simple way to rewrite only my form parameters?
I have in mind a Javascript solution ,however I want to know if there is a simpler way?Using HTML/PHP perhaps?
As far as I know, u u are not interested in using JS, then using form's hidden element is only way u have like this-
<form action="demo_form.asp">
First name: <input type="text" name="fname"><br>
<input type="hidden" name="country" value="Norway">
<input type="submit" value="Submit">
</form>
<p>Notice that the hidden field above is not shown to a user.</p>
The question is how u can use it with PHP, right?
The solution is here-
//In PHP
if( isset($_GET['fromPerson']) )
{
echo $fromPerson;
}
So combined HTML and PHP code will be like this (assuming a get element from prevous page is named fromPerson)-
<form action="demo_form.asp">
First name: <input type="text" name="fname"><br>
<?php
if( isset($_GET['fromPerson']) )
{
echo '<input type="hidden" name="country" value=".$_POST['fromPerson'].">';
}
?>
<input type="submit" value="Submit">
</form>
Lets say you get a parameter p1 from a get request, it should look like this:
http://server.com/?p1=123
In your form, you can add hidden fields that would have the same effect when you submit, like this:
<form method="GET">
<input type="hidden" value="<?php echo $_GET["p1"]; ?>" name="p1">
</form>
That way you can resend the variables as many times as you need.
I'm not sure I understand your question... Can you post your code?
I assume you mean something like this?
in index.php
<input type="hidden" name="id" value="<?php echo $id; ?>" />
in return.php
Edit

How to call php variable input text from another Php page

My question is how do I call input value of user selected which has show by php code from another php.
Normal simple way we get the input this way
$calendar_id_val = $_GET['calendar_id'];
and now it is not working:
For example Show.php, I have one form which show the values from Database and show the result with php variable with While Loop.
<input name="calendar_id" value="<?php echo $calendar_id;?>">
and when user is submit that form I will carry these user selected value and perform insert.php
While you are doing echo in the input use echo $calendar_id_val
You should use form like,
<form action="insert.php" method="get">
<input type="text" name="calendar_id" value="<?php echo $calendar_id;?>"/>
<input type="submit" name="submit_id" value="Insert"/>
</form>
Insert.php
echo $calendar_id_val = $_GET['calendar_id'];
Does this answer your question?
<form action="insert.php" method="GET">
<input name="calendar_id" value="<?php echo $calendar_id;?>">
</form>
If you want to redirect the user back to show.php, add this to the end of your insert.php script
header('Location: show.php');
exit();
I suggest $_POST var like this:
<form action="insert.php" method="POST">
<input type="text" name="calendar_id" value="<?php echo $calendar_id;?>" />
<input type="submit" name="submit" value="Submit" />
</form>
insert.php file:
echo $calendar_id = $_POST['calendar_id'];

A second button to start php script, how?

I have read the answer to this question, to execute PHP scripts with the click of a button. But what if I have a "nested button", like this :
<?php
if(!empty($_POST['act'])) {
echo "Ready to rock!";
$someVar = "Rock n Roll";
if(!empty($_POST['act2'])) {
echo $someVar;
} else {
?>
<form method="POST" action="">
<input type="hidden" name="act2" value="run">
<input type="submit" value="Rock It!">
</form>
<?php
}
} else {
?>
<form method="POST" action="">
<input type="hidden" name="act" value="run">
<input type="submit" value="Show It!">
</form>
<?php } ?>
I heard my problem can be solved with jQuery, but I no idea.
anyone please.
To execute a script on the server you use the action property of your form:
<form method="POST" action="myscript.php">
When clicking a input type="submit" the browser will go to to action of the form surrounding the input type="submit"
Nesting is not a issue, as the browser always will look for the 'surrounding' form.
Problem is in second form, so it will never calls in this code, because it fails in first $_POST variable IF statement, because in second form you do not POST variable "act". so you need to add it
<form method="POST" action="">
<input type="hidden" name="act" value="run">
<input type="hidden" name="act2" value="run">
<input type="submit" value="Rock It!">
</form>
with this form you should see echo $someVar;
p.s. if form action property is emtpy, by default it submits form to the same php script
Just like #DTukans said here, you need the hidden field. If you would post the second form, the value of act will be lost if you are not having a hidden field with the value of act from the first form.
In php you can also check which submit button you submitted by giving the input[type="submit"] a name, such as <input type="submit" name="form2">, then you could check if you submitted that form by:
if (isset($_POST['form2'])) {}, but this is not the case here.
Use the hidden input and you will be good to go.

passing value in hidden field from one page to another in php

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);

Categories