ashis
ashis

Reputation: 381

php get function to transfer form to another page and if edited back to form

okay guys..

i have two pages

page1 contains a form

 <form id="Form1" name="Form1" method="post" action="form1.php"> //form1.php is the page1 where this form is..
<input type="text" class="input" name="txtName" value="<?php if(isset($name)){echo $name;} ?>" <?php if(isset($flag) && $flag == 1){echo "div style = 'border:2px solid red;'". "/div";}?>> //HERE EVERYTHING IS GOOD

<input type="submit" name="submit" value="Submit" class="button3">
</form>

I send the data to page2 using php sessions

php page1 code

session_start();
if(NO ERRORS)) // in the form there is actual code
{
//insert into database
$result = mysql_query($insert);
if($result)
{
echo("<br>Input data is succeed");
$lastInsertedId =  mysql_insert_id();
$_SESSION['last_id'] = $lastInsertedId;
header('Location: form1_conf.php?id');
}
else
{
$message = "The data cannot be inserted.";
$message .= "<br />" . mysql_error();
}

now comes page2

the page2 name is form1_conf.php & is used to display the form data to user so that he may check the form for error and if anything is wrong he can click on edit and go back to main form (page1) and re-enter the data and resubmit the form.

here is the page2 code

here i use php to receive the data from page1 as:

<?php 
session_start();
$id = $_SESSION['last_id'];
$query  = "SELECT * FROM db_form1 WHERE id=$id";
$result = mysql_query($query);
while($row = mysql_fetch_row($result))
{
    $name = $row[3];
}
?>

and here is the html code to display this

 <div id="DisplayForm">
 <div class="dispText">
 <?php echo $name; ?>
 </div>
 </div>

 <a href="#" class="button">EDIT</a>

NOW i have two questions

  1. People told me that sessions can be an issue if the form is opened in multiple tabs, sessions can mess up. So instead i was suggested to use get. If this statement is valid, where shall be the modifications in php code to get it accomplished with get, or maybe better method?
  2. whether or not this code is modified to get work dont via get, how can this form will be redirected to page1, if the user clicks on edit button on page2? see the # with EDIT button. How can it be achieved?

Thanks a lot in advance for your help friends.

Upvotes: 3

Views: 2418

Answers (2)

Expedito
Expedito

Reputation: 7795

You can send data with a query string to send an id or whatever else in your header function:

header('Location: form1_conf.php?id='.$id);

Or anywhere else such as in links:

www.example.com/page2.php?id=445

Then on the receiving end, you can get the data with the $_GET array:

if (isset($_GET['id'])){
    $id = $_GET['id'];
}

You can change your edit link with the URL as well:

<a href="http://www.example.com/page1.php?id=<?php echo $id; ?>" class="button">EDIT</a>

Upvotes: 2

Tobias Golbs
Tobias Golbs

Reputation: 4616

Well here is a solution to do this in one page:

form.php

<?php

if(isset($_POST['txtName'])) {
    //the form was submitted, validate the data and insert to the database
    if(NO ERRORS)) // in the form there is actual code
    {
        //insert into database
        $result = mysql_query($insert);
        if($result)
        {
            $lastInsertedId = mysql_insert_id();
            $_SESSION['last_id'] = $lastInsertedId;
            //do not echo anything before you try to redirect. There must not be any output before header
            header("Location: the_page_that_displays_your_data.php?id=$lastInsertedId");
        } else
        {
            $errorMessage = "The data cannot be inserted.";
            $errorMessage .= "<br />" . mysql_error();
        }
    } else {
        //check your errors and display them in the form below
    }
}

?>

<!--set action to "" so the page will submit to itself-->
<form id="Form1" name="Form1" method="post" action="">
    <!--if $errorMessage is defined you can echo it here (for example)-->
    <!--if the txtName in $_POST is set display it after escaping it-->
    <input type="text" class="input" name="txtName" value="<?php echo isset($_POST['txtName']) ? htmlspecialchars(filter_input(INPUT_POST, 'txtName'), ENT_COMPAT, 'UTF-8') : ''; ?>">
    <input type="submit" name="submit" value="Submit" class="button3">
</form>

Hope this helps!

Upvotes: 3

Related Questions