user5863306
user5863306

Reputation:

How to redirect to previous page and keep all filled input same using php

I am using php and javascript for my small project. I have a form where there are 10 or more input in one of my page. My question is When user fill the input field like textbox and textarea, and return in same page after clicking submit button if some condition is failed. How to get all input field's values
For example

<form name="form1" method="post" action="">
  <label for="textfield">Name</label>
  <input type="text" name="textfield" id="textfield"><br/>
  <label for="textfield2">Phone</label>
  <input type="text" name="textfield2" id="textfield2"><br/>
  <label for="textarea">Address</label>
  <textarea name="textarea" id="textarea" cols="45" rows="5"></textarea><br />
  <input type="submit" name="button" id="button" value="Submit">
</form>

I like to submit it into next.php, but if any reason condition is failed and I redirect it immediately to same page having this form. I like to have all the fields filled as it is.

Upvotes: 1

Views: 3441

Answers (3)

Yash Kumar Verma
Yash Kumar Verma

Reputation: 9620

Well, others have already answered what i recommended, but if you are not comfortable with displaying through $_POST['element']; , you can do this by cookies too !

When user submits form -> set cookies of data --> if user again visits the same page--> check if cookies settled --> if settled, show , else don't -- > done !!!

Upvotes: 0

Vivek Pipaliya
Vivek Pipaliya

Reputation: 498

You can use jquery or ajax to validation (Check condition).

You need to check all conditions before redirect page to next.php If all condition are true, then page will redirect to next.php otherwise page remain same.

OR

You can use session in php.

<?php
session_start();
?>
<form name="form1" method="post" action="">
  <label for="textfield">Name</label>
  <input type="text" value="<?php echo $_SESSION['textfield_name']; ?>" name="textfield" id="textfield"><br/>
  <label for="textfield2">Phone</label>
  <input type="text" value="<?php echo $_SESSION['textfield_phone']; ?>" name="textfield2" id="textfield2"><br/>
  <label for="textarea">Address</label>
  <textarea name="textarea" id="textarea" cols="45" rows="5"><?php echo $_SESSION['textarea_address']; ?></textarea><br />
  <input type="submit" name="button" id="button" value="Submit">
</form>

In next.php

<?php
session_start();
$_SESSION['textfield_name'] = $_POST['textfield_name'];
$_SESSION['textfield_phone'] = $_POST['textfield_phone'];
$_SESSION['textarea_address'] = $_POST['textarea_address'];

// Your conditions here...

redirect("prev.php"); // Redirect to form page.
?>

OR

If you do not want to use session then you can pass value in url.

<form name="form1" method="post" action="">
      <label for="textfield">Name</label>
      <input type="text" value="<?php echo $_GET['textfield_name']; ?>" name="textfield" id="textfield"><br/>
      <label for="textfield2">Phone</label>
      <input type="text" value="<?php echo $_GET['textfield_phone']; ?>" name="textfield2" id="textfield2"><br/>
      <label for="textarea">Address</label>
      <textarea name="textarea" id="textarea" cols="45" rows="5"><?php echo $_GET['textarea_address']; ?></textarea><br />
      <input type="submit" name="button" id="button" value="Submit">
    </form>

In next.php file

<?php

    $textfield_name = $_POST['textfield_name'];
    $textfield_phone = $_POST['textfield_phone'];
    $textarea_address = $_POST['textarea_address'];

    // Your conditions here...

    redirect("prev.php?textfield_name=$textfield_name&textfield_phone=$textfield_phone&textarea_address=$textarea_address"); // Redirect to form page.
    ?>

Upvotes: 1

Nisar P
Nisar P

Reputation: 316

You don't need sessions for this purpose. You can use $_POST array as shown below:

<form name="form1" method="post" action="">
  <label for="textfield">Name</label>
  <input type="text" name="textfield" id="textfield" value="<?php echo isset($_POST['textfield']) ? $_POST['textfield'] : ''; ?>"><br/>
  <label for="textfield2">Phone</label>
  <input type="text" name="textfield2" id="textfield2" value="<?php echo isset($_POST['textfield2']) ? $_POST['textfield2'] : ''; ?>"><br/>
  <label for="textarea">Address</label>
  <textarea name="textarea" id="textarea" cols="45" rows="5"><?php echo isset($_POST['textarea']) ? $_POST['textarea'] : ''; ?></textarea><br />
  <input type="submit" name="button" id="button" value="Submit">
</form>

Upvotes: 0

Related Questions