Getting input value and put it in a variable PHP - php

I have a problem of getting the value of an input element. Thanks in advance for help! This is the sample code:
<form action="#" method="POST">
<input name="X" value="3" />
<?php
//question: how can I put the value of input in a variable
//$_POST['X']; isn't applicable since I am in the same form.
?>
</form>

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
<input name="X" value="3" />
<input type="submit" value="Submit">
</form>
<?php
if(isset($_POST['X']) == true){
echo $_POST['X'];
}
?>

First of all submitting a form on its own page is a bad practice because whenever you reload the page the form will submit everytime.
Best approach is that you set the action attribute of form to another file or link.
fileOne.php (where your form is located):
<form action="anotherfile.php" method="POST">
<input name="X" value="3" />
<button type="submit" name="submitForm">Submit</button>
</form>
Then in anotherfile.php you will do this:
$check = $_POST["submitForm"];
If(isset($check))
{
$myInputValue = $_POST["X"];
header("Location: fileOne.php/?val".$myInputValue);
}
Then in fileOne.php you can get the variable and its value which is sent via link in the browser like this:
$getInputValue = $_GET["val"];
Now you can echo out the $getInputValue variable wherever you want.
If the the information is not sensitive this is the best approach you got. But if it is, try saving that value in the session and retrieving in the file where you want. I hope it helps. Typed code via app so it might throw an error. But i think this code will work fine. Good luck!

Related

PHP form is not posting data

I have a form on an HTML/PHP page.
I have the same exact code on other pages on the site, and it works fine. I cannot figure it out.
Form:
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input name="prize_id" type="hidden" value="<?php echo $prize_id; ?>" />
<input name="ContestEntry" type="submit" class="submit" value="Enter me in the Raffle!" />
</form>
I looked at the HTML source code, and the action populates the correct page, and $prize_id populates the correct info in the value.
PHP:
if(isset($_POST['ContestEntry'])){
//...code to enter data into form, irrelevent since it won't post anything anyway.
}
else {
echo 'Nothing posted from form';
}
"Nothing posted from form" always shows, and no data is being entered into the database. I've tried changing the form name to 5 different things, thinking maybe there was a conflicting name somewhere, but nothing works.
Any ideas?
If all you need to achieve is check whether the form is submitted or not, it's better to check that the request type is a POST:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Process form
} else {
// Nothing posted!
}
Also change your submit button to:
<button type="submit">Submit</button>
and see what happens
The code you posted actually works for me.
But, if you still can't get it working, I would try checking to see if prize_id is set rather than the button. The PHP script is executed when the form is submitted.
Also, I would recommend that you don't use $_SERVER['PHP_SELF'] as the form action. According to https://stackoverflow.com/a/14093363/3593228, that can make it easy for attackers to insert malicious data. Instead, leave the action empty.
The if Condition is not working because you put it on Button, instead of this just make an hidden field in the form to check form is submit or not like as you already have one. Or make new for this form.
<input name="prize_id" type="hidden" value="<?php echo $prize_id; ?>" />
Consider adding what you're looking for as another hidden field:
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input name="prize_id" type="hidden" value="<?php echo $prize_id; ?>" />
<input name="ContestEntry" type="hidden" value="Enter me in the Raffle!" />
<input type="submit" class="submit" />
</form>

Form submit to another page and then review or write to a file

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.

HTML form does not submit data

I have one page with a simple post form
<form name="click" action="UserOrders.php" method="post">
<input type="hidden" name="amount" value="<?php echo $total ?>">
<input type="image" src="/Templates/images/UpdateButton.png" name="submit">
</form>
and the only thing I want to check is if it submits.. In the other page "UserOrders.php" I just wrote
<?php
if ($_POST['submit']){
echo $_SESSION['ID'];
}
It seems to me irregular that it doesn't work and I would like another set of eyes to check it out.
(if i put the echo $_SESSION['ID'] outside the brackets, it works.)
An input type="image" only defines that image as the submit button and not as an input that can carry over a value to the server. source
An alternative way to see if the form was submitted is to check $_SERVER['REQUEST_METHOD']
if ('POST' === $_SERVER['REQUEST_METHOD']) {
// submitted
}
I agree with the previous answer. You are not actually putting any information to the form and therefore there is nothing for the new form to react to. $_POST['submit"] is looking for data that was sent and not for the method of sending.
There's no actual submit button. You need a
<form action="UsersOrders.php" method="POST">
<input type="whatever you want" name="input">
<input type="submit" name="submit">
</form>
Now if you go into UsersOrders.php and do what you did before:
if($_POST['submit']) {
echo $_POST['input'];
}
You will actually get some input.

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