PHP GET and POST request in signle page what is happning - php

I have PHP code that do the following things
1-get some request parameters from parent page
2-send post request for it self and validate data
3-redirect the combined data to the next page
here is sample snippet
I am having difficult time fixing my messy code... pleas help me!
<?php
//on page load---from parent page
if ($_SERVER["REQUEST_METHOD"] == "GET") {
$room_number=$_GET['room_number'];
}
//on submit
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST["name"];
$lastname = $_POST["lastname"];
//do some stuff... validation
if($valid){
// if valide redirect to ...
header("Location: RegisterUser.php?name=".$name."&lastname=".$lastname."& roomnumber=".$room_number."");
exit();
}
}
?>
<form action='<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>' method='post' >
<input type="text" name="name">
<input type="text" name="lastname">
</form>
the problem i am having is at the redirection $room_number is empty always
how can i fix it??
I know i am doing a lot of stuff in single file that is because i am very beginner.
it would be great if someone can suggest me better way to so such stuff.
Thank you in advanced!

You could use hidden input for room_number input.
<?php
if(isset($_POST['submit'])) {
$name = $_POST['name'];
$lastname = $_POST['lastname'];
$room_number = $_POST['room_number'];
header("Location: RegisterUser.php?name=".$name."&lastname=".$lastname."&roomnumber=".$room_number);
exit();
}
?>
<form action='<?php echo $_SERVER["PHP_SELF"];?>' method='post'>
<input type="text" name="name">
<input type="text" name="lastname">
<input type="hidden" name="room_number" value="<?php echo $room_number; ?>" />
<input type="submit" name="submit" value="Submit" />
</form>
And then use the above code in your file. It may help you. And Why are you redirecting those variables to another register page. Just use them in itself to insert into database or anything else what you want.

Add your $room_number in your form as a hidden input field so that you can pass it through as a post variable.
<input type="hidden" name="room_number" value="<?php echo $room_number; ?>" />

Related

Problem: Part of the PHP exercise does not run correctly

I created a section for editing. When I edit the information and click the save button, the information is not saved and the header section does not display completely.
<?php
if (isset($_POST['submit_btn']))
{
$id = $_POST['id'];
$fn = trim($_POST['name']);
$ln = trim($_POST['lastname']);
$age = trim($_POST['age']);
$q = "UPDATE `users` SET `fn` = '$fn',
`ln` = '$ln',
`age` = '$age'
WHERE id = '$id'";
mysqli_query($dbconnect,$q);
if (mysqli_affected_rows($dbconnect) > 0)
redirect("?msg=ok&id=**$id**");
else
redirect("?msg=error&id=**$id**");
}
else
echo ("Not In If(isset)");
?>
<form action="" method="post">
<label for="name">FirstName:</label>
<input type="text" name="name" id="name" value="<?php echo $row['fn']?>">
<br>
<label for="lastname">LastName:</label>
<input type="text" name="lastname" id="lastname" value="<?php echo $row['ln']?>">
<br>
<label for="age">Age:</label>
<input type="text" name="age" id="age" value="<?php echo $row['age']?>">
<br>
<input type="submit" name="submit_btn" value="Save">
<a href="index2.php">
Back
</a>
</form>
</body>
Bold sections do not work here.
Below is a picture of the result:
In the link that I specified, after clicking on save the ID will not be displayed and all the information filled in the forms will be lost.
Sorry if the result is styleless and boring and I just created this page to practice php😁
Thank you for being responsive🙏🙏🙏
You are mistaking a POST request with a GET request.
Part, which appears in the URL is sent to the webserver in GET request.
Your form is submitting POST request to the webserver, logic in the code does the same, but you are trying to display information from url (GET).
Please check the examples in php.net:
POST variables: https://www.php.net/manual/en/reserved.variables.post.php
GET variables: https://www.php.net/manual/en/reserved.variables.get.php
You can take an example with GET request variable below, however, be careful with trusting the "end client" and always prepare your statements, which you send to your database to execute queries.
if (isset($_GET['submit']))
{
$number = $_GET['number'];
echo $number
? "Number which was submitted: $number <br>"
: 'Number is not set';
} else {
echo 'Form has not been yet submitted';
}
?>
<form action="" method="get">
<input type="number" name="number" placeholder="Number">
<input type="submit" name="submit" value="Save">
</form>

How to hold the form data from one page and show it in after two pages without session and database

i want to display my first page data in third page without session or database.
here is my page1.php form.
<form method="post" action="page1">
name<input type="text" name="name">
password<input type="password" name="pass">
retype password<input type="password">
<input type="submit" name="submit" value="Submit"/>
</form>
it post the value to page two. and my page2.php contain one button. if i click that button means it goes to page2
<form method="post" action="page3">
<?php
if(isset($_POST["submit"]))
{
$name= $_POST["name"];
$pass= $_POST["pass"];
}
?>
<input type="submit" name="submit1" value="submit">
</form>
now i need to display fist page data to here in page3.php
<?php
if(isset($_POST["submit1"]))
{
$name= $_POST["name"];
$pass= $_POST["pass"];
echo $name;
echo $pass;
}
please any one give idea to make this thing to work.
thank you.
We can maintain the data without using session variable or database storage, using the "input type=hidden" property like this
<form method="post" action="page3">
<?php
if(isset($_POST["submit"]))
{
$name= $_POST["name"];
$pass= $_POST["pass"];
}
?>
<input type="hidden" name="name" value="<?php echo $name; ?>">
<input type="hidden" name="pass" value="<?php echo $pass; ?>">
<input type="submit" name="submit1" value="submit">
</form>
And you can retrieve this values in your 3rd form using post variables like this..
<?php
if(isset($_POST["submit1"]))
{
$name= $_POST["name"];
$pass= $_POST["pass"];
echo $name;
echo $pass;
}
?>
Although I personally wouldn't recommend this method for a sensitive data as passwords, as the users can see the values in html form if they need to, using inspect element.

Retrieve $_POST for two pages

I need to save my submit form data like:Name for two pages...
For some reason the $_POST only saves data for the "action" page, but cannot be retrived after the action page.
Here's my code:
HTML (form):
<html>
<body> <form name="input" action="staff.php" method="post">
Username: <input type="text" name="Name">
<input type="submit" value="Submit">
</form>
</body>
</html>
Here's the next page after submiting and it works... (staff.php)
<html>
<?php
session_start();
echo "You have choosen". $_POST['Name']; // it shows what you've choosen...
?>
<form name="input" action="staff2.php" method="post">
Age: <input type="text" name="Age">
<input type="submit" value="Submit">
</form>
</html>
Ok and after age submiting Name and Age stop working... (staff2.php)
Here's the code:
<?php
session_start();
echo "You have choosen".
$_POST['Name']; //it does't show Name.. Please help!
$_POST['Age']; // it doesnt't show this either..
?>
Obviously, there is nothing wrong on the first page. So don't change anything.
The second page. The post works. Then add a hidden input to preserve it and carry it on the next one:
<?php
echo "You have chosen: ". $_POST['Name']; // it shows what you've choosen...
?>
<form name="input" action="staff2.php" method="post">
Age: <input type="text" name="Age">
<input type="hidden" name="Name" value="<?php echo $_POST['Name']; ?>" /> <!-- this one -->
<input type="submit" value="Submit">
</form>
On the third and final page. Properly concatenate the variables:
echo 'You have chosen: <br/>';
echo $_POST['Name'] . '<br/>'; // this should carry the hidden input you set on the last page
echo $_POST['Age'];
//^^ you forgot the echo
as you have a session running pass them as session variables.
$_SESSION['name'] = $_POST['name'];

Simple PHP: getting variable from a form input

I can't really use PHP and from what I've seen in tutorials this should work, but it doesn't:
<html>
<head></head>
<body>
<form>
<input type='text' name="name" value='myName'>
</form>
<p>
<?php
$name = $_POST['name'];
echo $name
?>
</p>
</body>
</html>
Is there a reason I can't get the value of name?
Sorry for asking such a simple question...
here is the fiddle http://jsfiddle.net/DCmu5/1/, so, please try what you said and send it to me only when it works before answering
PHP is a server-side language, so you'll need to submit the form in order to access its variables.
Since you didn't specify a method, GET is assumed, so you'll need the $_GET super-global:
echo $_GET['name'];
It's probably better though, to use $_POST (since it'll avoid the values being passed in the URL directly. As such, you can add method attribute to your <form> element as follows:
<form method="post">
<input type='text' name="name" value="myName" />
<input type="submit" name="go" value="Submit" />
</form>
Notice I also added a submit button for good measure, but this isn't required. Simply hitting return inside the textbox would submit the form.
Well, you have to POST your form to get the value of $_POST variables. Add this to your <form>
<form action="yourpagename.php" method="post">
<input type='text' name="name" value='myName'>
<button type="submit">Submit</button>
</form>
Click button and whatever is typed in your field will show up.
<html>
<body>
<form method="post" action="1.php">
Name: <input type="text" name="fname">
<input type="submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// collect value of input field
$name = $_POST['fname'];
if (empty($name)) {
echo "Name is empty";
} else {
echo $name;
}
}
?>
</body>
</html>
try this
<?php
if(isset($_REQUEST['name'])){
$name = $_REQUEST['name'];
echo $name;
}
?>

Is it possible to include a form in a wordpress page?

I have the following code in a php template called contact_us. I have created a new page which uses this template, but when you click submit it doesn't post back to the same page and display what the user entered in the form. Any ideas why this doesn't work?
Thanks,
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = $_POST["name"];
$comments = $_POST["comments"];
echo $name;
echo $comments;
}
?>
<form action="<?php echo $PHP_SELF;?>" method="post" >
Name : <br/>
<input type="text" name="name" /><br/>
Comment <br/>
<textarea name="comments" name="comments"></textarea>
<br/><br/>
<input name="submit" type="submit" id="submit" value="Send" />
</form>
Make sure that you don't use "name" as a variable name. I will assume that the same thing goes for comments.
More information here
http://wpquicktips.wordpress.com/2010/02/17/use-an-empty-action-attribute-in-forms/
Does it make any difference removing <?php echo $PHP_SELF;?> from the action and leaving it blank?
I managed to get this to work by adding an id attribute to the form. I think this is something wordpress requires.

Categories