How can I keep the URL id on submit? - php

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.

Related

Fetching URL vars into form and submitting to other page

I have this url: torneioJogo.php?equipaleft=24&equiparight=25&torneioid=8
The following code is in this url...
<form method='GET' action='torneioJogoSub.php?'>
//THESE INPUTS DOWN HERE
<input type="hidden" name="equipa1" value="<?php $_GET['equipaleft'] ?>">
<input type="hidden" name="equipa2" value="<?php $_GET['equiparight'] ?>">
<input type="hidden" name="torneioid" value="<?php $_GET['torneioid'] ?>">
<div id="pontos">
<input type="submit" name="win1" value="<=Vencedor">
<input type="submit" name="emp" value="Empate">
<input type="submit" name="win2" value="Vencedor=>">
<br>
<=Pontos=><br>
//AND THESE INPUTS DOWN HERE
<input type="text" name="pontos1" size="3">
<input type="text" name="pontos2" size="3">
<br>
</div>
</form>
When I submit the form, I need all the data from those inputs in the next page where I have this:
echo "equipa1: " . $_GET['equipa1'];
echo "equipa2: " . $_GET['equipa2'];
echo "torneioid: " . $_GET['torneioid'];
echo "pontos1: " . $_GET['pontos1'];
echo "pontos2: " . $_GET['pontos2'];
What happens is $_GET['pontos2'] and $_GET['pontos1'] work but the values from the hidden inputs (which are being pulled from the url variables), echo nothing. What's happening? Is this a problem with GETs and POSTs or am I missing something else?
You need to echo the values out
<input type="hidden" name="equipa1" value="<?php echo $_GET['equipaleft'] ?>">
<input type="hidden" name="equipa2" value="<?php echo $_GET['equiparight'] ?>">
<input type="hidden" name="torneioid" value="<?php echo $_GET['torneioid'] ?>">
You need to actually output the variables into the form fields
<input type="hidden" name="equipa1" value="<?php echo $_GET['equipaleft'] ?>">
^^^^

Passing form inputs from different pages to submit at final page

I have 4 different pages both with one form each.
I want to gather all the entries on each of the pages and submit once.
Here is code.
Page 1
<form action="page2" method="POST">
<input type="text" name="sex">
<input type="submit" value="Submit">
</form>
Page 2
<form action="page3" method="POST">
<input type="text" name="size">
<input type="hidden" name="sex" value="<?php echo $_POST['sex'] ?>" >
<input type="submit" value="Submit">
</form>
Page 3
<form action="page4" method="POST">
<input type="text" name="colors">
<input type="hidden" name="size" value="<?php echo $_POST['size'] ?>" >
<input type="submit" value="Submit">
</form>
Page 4
<form action="verNote.php" method="POST">
<input type="text" name="likes">
<input type="hidden" name="colors" value="<?php echo $_POST['colors'] ?>" > <input type="submit" value="Submit">
</form>
Then i will like to get all the infos on verNote.php
<?php
echo $_POST['sex'];
echo '<br>';
echo $_POST['size'];
echo '<br>';
echo $_POST['color'];
echo '<br>';
echo $_POST['likes'];
?>
This code above dont seem to post entries from both pages 1 and 2, just for 3 and 4 alone gets submitted.
Will appreciate immediate assistance form anyone who understands my question.
Regards!
You need to load the hidden fields again each time
Page 3
<form action="B.php" method="POST">
<input type="text" name="colors">
<input type="hidden" name="size" value="<?php echo $_POST['size'] ?>" >
<input type="hidden" name="sex" value="<?php echo $_POST['sex'] ?>" >
<input type="submit" value="Submit">
</form>
Page 4
<form action="B.php" method="POST">
<input type="text" name="likes">
<input type="hidden" name="colors" value="<?php echo $_POST['colors'] ?>" >
<input type="hidden" name="sex" value="<?php echo $_POST['sex'] ?>" >
<input type="hidden" name="size" value="<?php echo $_POST['size'] ?>" >
<input type="submit" value="Submit">
</form>
I didn't understand 100% what you're trying to achieve, but have you tried using sessions?
Do this in B.php:
<?php
session_start();
if( isset($_POST['sex']))
$_SESSION['sex'] = $_POST['sex'];
if( isset($_POST['size']))
$_SESSION['size'] = $_POST['size'];
if( isset($_POST['color']))
$_SESSION['color'] = $_POST['color'];
if( isset($_POST['likes']))
$_SESSION['likes'] = $_POST['likes'];
?>
Then you can retrieve the values from any other file, just call session_start(); and use the $_SESSION superglobal.
EDIT
Using sessions, you verNote.php file could be something like this:
<?php
session_start();
echo $_SESSION['sex'];
echo '<br />';
echo $_SESSION['size'];
echo '<br />';
echo $_SESSION['color'];
echo '<br />';
echo $_SESSION['likes'];
echo '<br />';
?>

Having issue with my html form in a php page

I have tried to put this code in my webpage but I have this error :
Parse error: syntax error, unexpected '?' in E:\MyServer\htdocs\...(line 1 from this script)
CODE :
<form action='delete.php?name="<?php echo $contact['name']; ?>"' method="post">
<input type="hidden" name="name" value="<?php echo $contact['name']; ?>">
<input type="submit" name="submit" value="Delete">
</form>
this code is in a .php page
Thanks.
Try to format it properly:
<form action="delete.php?name=<?php echo $contact['name']; ?>" method="post">
<input type="hidden" name="name" value="<?php echo $contact['name']; ?>">
<input type="submit" name="submit" value="Delete">
</form>
How to get the value you pass through your form:
Welcome <?php echo $_POST["name"]; ?><br>.
EDIT
Also you should add this in delete.php file:
if(isset($_POST['name'])){ $name = $_POST['name']; }
See : http://www.w3schools.com/php/php_forms.asp
You need to drop the double quotes around the variable name and move them to the actual element property.
<form action='delete.php?name="<?php echo $contact['name']; ?>"'
^ ^
It then becomes
<form action="delete.php?name=<?php echo $contact['name']; ?>"
<form action="delete.php?name=<?php echo $contact['name']; ?>" method="post">
<input type="hidden" name="name" value="<?php echo $contact['name']; ?>">
<input type="submit" name="submit" value="Delete">
</form>
This should work ..
Do you need to send the data via GET and POST to delete.php?
<form action='delete.php' method="post">
<input type="hidden" name="name" value="<?php echo $contact['name']; ?>">
<input type="submit" name="submit" value="Delete">
With this you can then just do $_POST['name'] to get your contact name...
Sorry if I have missed something but cannot see why you would need it passed twice

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/

Issues with passing PHP echo in form through POST method

<form method="POST" name="send"
<input type="hidden" name="title" value="<?php echo ($pro->title;?)">
</form>
I dont want people to see the hidden informations , Is this the best way to pass the variable over to my controller?
This the code in my controller
$this->email->subject('subject '.$_POST['title'].' ' );
Thank you!
<form method="POST" name="send"
<input type="hidden" name="title" value="<?php echo ($pro->title;?)">
should be
<form method="POST" name="send">
<input type="hidden" name="title" value="<?php echo $pro->title; ?>" />
For this i will suggest you to use SESSION because hidden fields can be changed.

Categories