access $_GET value after form submission - php

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

Related

How can I keep the URL id on submit?

When I click submit, the data is submitted to the database but the URL id of the book disappears to ----book.php
I want the URL to go back to the id of the page e.g. ----book.php?id=3
Is it possible to keep the first line as action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" and add the value="<?php echo $book_id ?>"?
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="hidden" value="<?php echo $book_id ?>" name="book_id" />
<p>Author: <input type="text" value="<?php echo $_SESSION['author']; ?>" name="author" id="author" readonly /></p>
<p>Summary: <input type="text" name="summary" value="<?php echo $summary;?>" /></p>
<p><input type="submit" name="submit" value="Submit" /></p>
<input type="hidden" name="submitted" value="TRUE" />
</form>
PHP code:
if (isset($_GET['id'])) {
$book_id = $_GET['id'];
}
Please try this code:
// save values in DB
header('Location: book.php?id=' . $book_id);
exit;
It is because $_SERVER["PHP_SELF"] contains only URL . It does not contain get queries . To solve the problem leave your action empty
e.g
<form method="post" action="">.....
You send a POST form but are trying to retrieve a GET value. Additinally, the parameter is named book_id, not id.
Use $book_id = $_POST['book_id'];
if (isset($_GET['id'])) { also won't work, for the same reasons.
There is no name with "id"
If you want to get the book id:
if (isset($_POST['book_id'])) {
$book_id = $_POST['book_id'];
}
OR modify the HTML like this:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input type="hidden" value="<?php echo $book_id ?>" name="id" />
<p>Author: <input type="text" value="<?php echo $_SESSION['author']; ?>" name="author" id="author" readonly />
</p>
<p>Summary: <input type="text" name="summary" value="<?php echo $summary;?>" /></p>
<p><input type="submit" name="submit" value="Submit" /></p>
<input type="hidden" name="submitted" value="TRUE" />
</form>
On the other hand, if you have problems with POST, just modify the first line of HTML for this one:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]) . "?id=" . echo $book_id; ?>">
You can use anyone of these:
PHP_SELF returns just URL. Use REQUEST_URI that returns URL with query string:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["REQUEST_URI"]);?>">
[you can also omit action values - that will have same behavior]
or if you want just id, then use:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]) . "?id=" . $_GET['id'];?>">
Note: Use validation wherever necessary. I just gave you an idea.

How to properly escape tags in input text

looks like ordinary question but could not find correct solution. lets say I have this form:
<form name="form" action="nextpage.php">
<input type="text" name="input_name" value="<?php echo $_POST['input_name'];?>" />
<input type="submit" name="submit" value="Next"/>
</form>
For example if I add "My input text >" as value of the input the nextpage.php is broken.
I tried to use for:value="<?php echo strip_tags($_POST['input_name']);?>" or
:value="<?php echo htmlspecialchars($_POST['input_name']);?>" but none of them works.. Why is that and how to avoid it? Thanks
First, you should sanitize your posted value for security, but that aside, php provides a function that automatically escapes special characters called addslahes http://php.net/manual/en/function.addslashes.php, save your post value to a variable using the addslashes function, then use the variable as your value.
try this
<?php
$_POST['input_name']="ddd"; ?>
<form name="form" method="POST" action="nextpage.php">
<input type="text" name="input_name" value="<?php echo $_POST['input_name'];?>" />
<input type="submit" name="submit" value="Next"/>
</form>
and in nextpage.php
<?php
$name=$_POST['input_name'];
echo $name;
Try this:
<form name="form" action="nextpage.php" method="POST">
<input type="text" name="input_name" value="<?php echo $_REQUEST['input_name'];?>" />
<input type="submit" value="Next"/>
</form>

why php input blank value or duplicate values in one form if submit another form

I am encountering a strange phenomena: When I submit a form in my code, then beside doing what is there in that form, it looks like it also trigger the comment form. So when the page is reloading, I get some blank comments or duplicate comments. In my code I have several forms with submit, and one of that is the form for input comment:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="hidden" name="a" value="<?php echo $_GET['id'] ?>">
<input type="hidden" name="b" value="<?php echo $_SERVER['REMOTE_ADDR'] ?>">
<input type="text" name="c" value="Name"><br>
<textarea name="d">
</textarea>
<input type="submit" />
</form>
Could any of you point out for me where it gets wrong?
As requested, I post the whole (updated) code here:
<h3>Comments</h3>
<!-- <p>Put your comments here: </p> -->
<?php
//a: commenters
$i = addslashes($_POST['a']);
$ip = addslashes($_POST['b']);
$a = addslashes($_POST['c']);
$b = addslashes($_POST['d']);
if(isset($_POST['form1'])){
if(isset($i) & isset($ip) & isset($a) & isset($b))
{
$connector= new DbConnector();
$r = mysql_query("SELECT COUNT(*) FROM `databasename`.`ban` WHERE ip=$ip"); //Check if banned
$r = mysql_fetch_array($r);
if(!$r[0]) //Phew, not banned
{ // echo "a: ".$a." b: ".$b." ip: ".$ip." i: ".$i."";
$Date4=date('Y-m-d H:i:s');
if(mysql_query("INSERT INTO `databasename`.`Comments` VALUES ('$a', '$b', '$ip', '$Date4')"))
{
?>
<script type="text/javascript">
window.location="/index.php?id=".<?php echo $i; ?>;
</script>
<?php
}
else echo "Error, in mysql query";
}
else echo "Error, You are banned.";
}
}
$x = mysql_query("SELECT * FROM `databasename`.`Comments` ORDER BY i DESC ");
while($r = mysql_fetch_object($x)) echo "<div class='c'>".$r->a."<p>".$r->b."</p> </div>";
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="hidden" name="a" value="<?php echo $_GET['id'] ?>">
<input type="hidden" name="b" value="<?php echo $_SERVER['REMOTE_ADDR'] ?>">
<input type="text" name="c" value="Name"><br>
<textarea name="d">
</textarea>
<input type="submit" name="form1" />
</form>
You need use isset()
in HTML submit button you need to use name attribute for each and every form,
<input type="submit" name="form1" />
IN PHP,
<?php
if(isset($_POST['form1']){
echo $_POST['a'];
}
?>
Your code,
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="hidden" name="a" value="<?php echo $_GET['id']; ?>">
<input type="hidden" name="b" value="<?php echo $_SERVER['REMOTE_ADDR']; ?>">
<input type="text" name="c" value="Name"><br>
<textarea name="d"></textarea>
<input type="submit" name="form1" />
</form>
I would use:
if(!empty($_POST['form1']){
echo $_POST['a'];
}
empty() will return true if the variable is an empty string, false, array(), NULL, “0?, 0, and an unset variable.
Read more: http://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/

More than one html form in a php file

I have a php and html based tool that has a form that, when submitted, outputs the data reformatted using echo commands.
I'd like to add a 2nd form to the same page that will also output using echo.
My issue is, when I submit the 2nd form the first forms output disappears. I'd like to make it so the echo output from the first form does not go away when the 2nd form is submitted so they will both be on the screen at the same time.
Is there a way I can do this?
Only one <form> block in a page can be submitted at a single time. <input> fields defined in one form will not be submitted when the other form is submitted.
e.g.
<form>
<input type="text" name="foo" />
<input type="submit" />
</form>
<form>
<input type="text" name="bar" />
<input type="submit" />
</form>
Clicking on submit will submit either a foo field, OR a bar field. Not both. If you want both fields to be submitted, then you have to either build them into a SINGLE form:
<form>
<input type="text" name="foo" />
<input type="text" name="bar" />
<input type="submit" />
</form>
or use Javascript to copy the data from one form to another.
<form method="post"> <div>Module1</div> <input type="text"
value="module1" name="module_id"> <input type="text" value="title 1"
name="title"> <input type="text" value="some text 1" name="text">
<input type="submit" name="form_1" value="submit"> </form>
<form method="post"> <div >Module2</div> <input type="text"
value="module2" name="module_id"> <input type="text" value="title 2"
name="title"> <input type="text" value="some text 2" name="text">
<input type="submit" name="form_2" value="submit"> </form>
<?php
if(isset($_POST['form_1'])){
echo '<pre>';
print_r($_POST); }
if(isset($_POST['form_2'])){
echo '<pre>';
print_r($_POST); } ?>
Yes,you can do it.
Eg :
// form1 on page a.php
<form method="post" action="a.php" name="form_one" >
<input type="text" name="form_1" value="if(isset($_POST['form_1'])) echo $_POST['form_1']; ?>" >
<input type="submit" name="submit_1" >
</form>
<?php
if(isset($_POST['submit']))
{
?>
<form method="post" action="a.php" name="form_two" >
<input type="text" name="form_2" value="if(isset($_POST['form_2'])) echo $_POST['form_2']; ?>" >
<input type="submit" name="submit_2" >
</form>
<?php
}
?>
Now when you will submit form_one you will see form_two appear and the value in form one will stay intact in form_one and one the submitting form two the value will remain.
Hope it helped :)

Form submission on php custom templated system

We are using very simple php custom template system using oop approach, but when we submit form, its not getting $_POST[] results. Please have a look at main code...
switch($act) { //$act= $_GET['Act'];
case 'Add':
$add_product = new Product();
print_r($_POST);
if(!empty($_POST)) {
echo $_POST['name']; //to check if its getting value
}
include('templates/edit_product.tpl');
break;
}
and here is edit_product.tpl...
<form action="<?php echo BASE_URL; ?>products.php?Act=Add" method="post">
<input type="text" size="50" name="name" />
<button type="submit" name="submit">submit</button>
thanks for your support.
<form action="<?php echo BASE_URL; ?>products.php?Act=Add" method="post">
<input type="text" size="50" name="name" />
<input type="submit" name="submit" value="submit" />
</form>
we got the problem, Thanks. We were using if(!empty($errors)) instead of if(empty($errors))

Categories