how to refill data after reloading page in php - php

I'm stuck at one point .I have a php form and after filling all details, user have to press submit button . If data does not submitted properly then I am reloading the page .
so My question is .
I want to set all previous values entered by user into respected
fields without request method.
how can i achieve this?
What I have done so far
My user.php
<?php
$name = '';
if(isset($_REQUEST[name]))
{
$name = $_REQUEST[name];
}
if(isset($_POST["submit"]))
{
$name = $_POST["txtname"];
header('Location:user.php?name=<?php echo $name; ?>');
}
?>
<hrml>
<body>
<form name="form1" id="form1" method="POST">
<label>Name:</label>
<input type="text" name="txtname" id="txtname" value="<?php echo $name; ?>">
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
EDIT
apply_now.php
<?php
$CLASS__BL = 'apply_now_bl';
require_once('apply_now_bl.php');
?>
<form name="form1" id="form1" action="" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="input-label">Full Name: *</label>
<input name="txtname" id="txtname" type="text" class="form-control" value="<?= #$page_bl->full_name; ?>">
</div>
</form>
apply_now_bl.php
<?php
require_once('admin/db_lib/candidates.php');
require_once('includes/general_include.php');
$page_bl = apply_now_bl();
page_load();
class apply_now_bl
{
var $full_name = '';
function page_load()
{
$this->obj_candidates = new candidates();
if(isset($_POST["submit"]))
{
$this->submit_data();
}
}
function submit_data()
{
$this->full_name = get_post_val("txtname");
$this->obj_candidates->full_name = $this->full_name;
redirect(apply_now.php); //common function for page redirect
}
}

No need to redirect through php header location . because action is blank and that means it will redirect to user.php(current page) once form submit.
so below is the updated code
<?php
$name = "";
if (isset($_POST["submit"])) {
$name = $_POST["txtname"];
}
?>
<html>
<body>
<form name="form1" id="form1" method="POST">
<label>Name:</label>
<input type="text" name="txtname" id="txtname" value="<?php echo $name; ?>">
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>

Related

the server request method prints GET even though the form method is POST

i wrote this code below and when i submit the form the $_SERVER["REQUEST_METHOD"] prints GET instead of POST even though the form method = "POST"
<?php
include "init.php";
include $temp . "navbar.php";
echo $_SERVER["REQUEST_METHOD"];
if($_SERVER["REQUEST_METHOD"]=='POST'){
echo $_POST["name"];
}
?>
<input type="hidden" value="admin" class="title">
<form action="index.php" method="POST">
<input type="text" name="name">
<input type="submit" value="ok">
</form>
<?php
include $temp . "foot.php";
?>
You can simply check if $_POST['name'] is set:
if(isset($_POST['name'])) {
// ...
}
And since you are posting to the same page you don't need to include the action attribute in your form.
<?php
include "init.php";
include $temp . "navbar.php";
if(isset($_POST['name']) {
echo $_POST['name'];
}
?>
<input type="hidden" value="admin" class="title">
<form method="POST">
<input type="text" name="name" value="" />
<input type="submit" value="ok">
</form>
<?php
include $temp . "foot.php";
?>

Cannot find the page/object after click submit the form

I followed a online tutorial to connect my php code with MySQL. After I click the submit button, it said it cannot find the object (page). I did not see any code in my code? Any idea for me how to debug this code?
<?php
$user = 'root';
$pass = 'xxxxxx';
$db = 'testDB';
$db = new mysqli('localhost', $user, $pass, $db) or die("Unable to connect");
echo"Great work!";
$id ="";
$fname="";
$midname="";
$lname="";
function getPosts()
{
$posts =array();
$posts[0]=$_POST['Contact_ID'];
$posts[1]=$_POST['first_name'];
$posts[2]=$_POST['middle_name'];
$posts[3]=$_POST['last_name'];
return $posts;
}
//search
if(isset($_POST['search']))
{
$data = getPosts();
$search_Query="select * from Contact where contact_ID =$data[0]";
$search_Result=mysql_query($db, $search_Query);
if($search_Result)
{
if(mysql_num_rows($search_Result))
{
while($row=mysql_fetch_array($search_Result))
{
$id =$row['contact_ID'];
$fname =$row['first_name'];
$midname =$row['middle_name'];
$lname =$row['last_name_name'];
}
}
else
{
echo"No data for this Id";
}
}
else
echo"Result error";
}
?>
<!DOCTYPE Html>
<html>
<head>
<title>Contact Us</title>
</head>
<body>
<form action="php_insert_update_delete_search.php" method="post">
<input type="number", name="contact_ID" placeholder="Id" value="<?php echo $id;?>"><br><br>
<input type="text", name="first_name" placeholder="First Name" value="<?php echo $fname;?>"><br><br>
<input type="text", name="middle_name" placeholder="Middle Name" value="<?php echo $midname;?>"><br><br>
<input type="text", name="last_namename" placeholder="Last Name" value="<?php echo $lname;?>"><br><br>
<div>
<input type="submit" name="insert" value="Add">
<input type="submit" name="update" value="Update">
<input type="submit" name="search" value="Find">
</div>
</form>
</body>
</html>
remove php_insert_update_delete_search.php from the action of your form. The form is being redirected to php_insert_update_delete_search.php page when you click search button
<form action="" method="post">
<input type="number" name="contact_ID" placeholder="Id" value="<?php echo $id;?>"><br><br>
<input type="text" name="first_name" placeholder="First Name" value="<?php echo $fname;?>"><br><br>
<input type="text" name="middle_name" placeholder="Middle Name" value="<?php echo $midname;?>"><br><br>
<input type="text" name="last_namename" placeholder="Last Name" value="<?php echo $lname;?>"><br><br>
<div>
<input type="submit" name="insert" value="Add">
<input type="submit" name="update" value="Update">
<input type="submit" name="search" value="Find">
</div>
</form>
See You Have applied an action in your form tag . This action means that when ever a user will click on any button related to that form he or she will be redirected to that page (mentioned in action ) . So u need to make a page php_insert_update_delete_search.php and has to apply logic there for inserting the data .
If your wanting the form to go to another page to run code to process what you need
<form action="pageyouneed.php" method="post">
if you want to use the current page to parse your code:
<form action="<?php echo $_SERVER["PHP_SELF"];?>" method="post">
$_SERVER["PHP_SELF"]; // Will return current document to parse code.

calling php function from submit button

I want to pass the value of input field address to php. So that when i click on submit button it will be store in php.
<input type="text" name="address">
<input type="submit" name="go" method="post">
<?php
if($_POST){
if(isset($_POST['go'])){
call();
}else{
echo "Error";
}
}
function call(){
echo "hi";
print(document.address.value);
}
?>
However when i click on button it response nothing.
You are mixing PHP and JavaScript. If you want a pure PHP solution you could do something like:
<?php
if(isset($_POST['go'])){
$address = $_POST['address'];
echo $address;
}
?>
<form method="POST">
<input type="text" name="address">
<input type="submit" name="go" method="post">
</form>
With PHP once the form is submitted you can access form values with $_POST so use $_POST['address'] instead of document.address.value
what you intend to do can be done as below method.
<form method="post" >
<input type="text" name="address">
<input type="submit" name="go">
</form>
<?php
if($_POST){
if(isset($_POST['go'])){
call();
}else{
echo "Error";
}
}
function call(){
echo "hi";
echo $_POST['address'];
}
?>
Try like this,this is working fine as per your requirement:
<html>
<body>
<form action="" method="post">
Address: <input type="text" name="name"><br>
<input type="submit" name="go" value="call" />
</form>
<?php
if (isset($_REQUEST['name'])) {
call();
}
?>
<?php
function call() {
$address= $_POST['name'];
echo "Address:".$address;
exit;
}
Output:-
For output Click here
use this code
<input type="text" name="address">
<input type="submit" name="go" method="post">
<?php
if($_POST){
if(isset($_POST['go'])){
call();
}else{
echo "Error";
}
function call(){
echo "hi";
echo $_POST['address'];
}
?>

How process two forms in 1 PHP file?

I've got 1 single PHP file, which contains 2 variables... what I want to achieve is this, first get the first name from the user, and store in in variable $name, then show a form to ask the user for his lastname, THEN.. print both variables, the thing is, when the second form is submitted the first variable disappears, is there a way to keep it in memory?
<?php
$title = rand(100, 300);
?>
<!doctype html>
<html>
<head>
<?php
//functions to call
function nameform() {
?>
<center><form action="<?php
echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" autofocus name="name"><br>
<input type="submit" value="name">
<input type="hidden" name="val1">
</form></center>
<?php
}
function lastname() {
?>
<center><form action="<?php
echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" autofocus name="lastname"><br>
<input type="submit" value="lastname">
<input type="hidden" name="val2">
</form></center>
<?php
}
?>
<meta charset="utf-8">
<title><?php
echo ("$title"); ?></title>
</head>
<body>
<?php
if(!isset($_POST['name'])){
nameform();
}
if (isset($_POST['name'])) {
$name = $_POST['name'];
lastname();
}
if (isset($_POST['lastname'])) {
$lastname = $_POST['lastname'];
echo "Your name is $name and your last name is $lastname";
}
?>
</form>
</body>
</html>
How about waiting until the form is complete to submit it? I would use a little javascript to take care of this.
<?php
$title = rand(100, 300);
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title><?php
echo ("$title"); ?></title>
</head>
<body>
<center>
<?php
if (isset($_POST)) {
$name = $_POST['name'];
$lastname = $_POST['lastname'];
echo "Your name is $name and your last name is $lastname";
} else {
?>
<form action="<?php
echo $_SERVER['PHP_SELF']; ?>" method="post">
<div id="firstname">
<input type="text" autofocus name="name"><br>
<a href="javascript:;"
onclick="document.getElementById('firstname').style.visibility='hidden';document.getElementById('lastname').style.visibility='visible';">Next</a>
</div>
<div id="lastname" style="display:none;">
<input type="text" name="lastname"><br>
<input type="submit" value="lastname">
</div>
</form>
<?php } ?>
</center>
</body>
</html>
Just set the correct info in your input type hidden, when your second form is send you can get the name value in $_POST['val2'].
See the example:
<?php
$title = rand(100, 300);
?>
<!doctype html>
<html>
<head>
<?php
//functions to call
function nameform() {
?>
<center><form action="<?php
echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" autofocus name="name"><br>
<input type="submit" value="name">
<input type="hidden" name="val1">
</form></center>
<?php
}
function lastname() {
?>
<center><form action="<?php
echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" autofocus name="lastname"><br>
<input type="submit" value="lastname">
<input type="hidden" name="val2" value="<?php echo $_POST['name']; ?>">
</form></center>
<?php
}
?>
<meta charset="utf-8">
<title><?php
echo ("$title"); ?></title>
</head>
<body>
<?php
if(!isset($_POST['name'])){
nameform();
}
if (isset($_POST['name'])) {
$name = $_POST['name'];
lastname();
}
if (isset($_POST['lastname'])) {
$lastname = $_POST['lastname'];
/*
Here you can get the name that come from the input type hidden named "val2"
*/
$name = $_POST['val2'];
echo "Your name is $name and your last name is $lastname";
}
?>
</form>
</body>
</html>
Or as sjagr says you can use SESSIONS, take a look to this example:
How preserve my inputs values each time that I refresh my page?

value preserving across multiple pages

i am trying to post the some data on another page at this i have a back button onclick of back button i wanna reload the original page with posted data.
use $_SESSION:
Page1.php
<?php
session_start();
$name = isset($_SESSION['name']) ? $_SESSION['name'] : '';
?>
<html>
<form method="post" action="Page2.php">
<input type="text" name="name" value="<?php echo $name; ?>" />
<input type="submit" value="Submit" />
</form>
</html>
Page2.php
<?php
session_start();
if (isset($_POST['name']))
{
$_SESSION['name'] = $_POST['name'];
}
$name = $_SESSION['name'];
?>
<html>
<form>
<span><?php echo $name; ?>" </span>
Back
</form>
</html>
you can use $_SESSION
<?php
if (!isset($_SESSION)) session_start();
if ($_POST['btnsubmit']){
$_SESSION['data1'] = $_POST['data1'];
$_SESSION['data2'] = $_POST['data2'];
$_SESSION['data3'] = $_POST['data3'];
//... and so on
}
?>
<form>
<input type="text" id="data1" value="<? echo $_SESSION['data1'];?>"/>
<input type="text" id="data2" value="<? echo $_SESSION['data2'];?>"/>
<input type="text" id="data3" value="<? echo $_SESSION['data3'];?>"/>
<input type="submit" id="btnsubmit" value="Submit"/>
</form>
<!-- and so on -->
Hope this gives you an idea

Categories