开发者

Form to form with PHP

I am trying to create a multi steps form where user will fill the form on page1.php and by submitting can go to page2.php to the next 'form'. What would be the easiest way?

Here is my code:

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
?>

<form id="pdf" method="post">
New project name:<input type="text" name="pr_name" placeholder="new project name..."><br/>
New project end date:<input id="datepicker" type="text" name="pr_end" placeholder="yyyy-mm-dd..."><br/>

<textarea class="ckeditor" name="pagecontent"  id="pagecontent"></textarea>

<?php    
if ($_POST["pr_name"]!="")
{

  // data collection
  $prname = $_POST["pr_name"];
  $prend = $_POST["pr_end"];
  $prmenu = "pdf";
  $prcontent = $_POST["pagecontent"];

  //SQL INSERT with error checking for test 
  $stmt = $pdo->prepare("INSERT INTO projects (prname, enddate, sel, content) VALUES(?,?,?,?)");

  if (!$stmt) echo "\nPDO::errorInfo():\n";

  $stmt->execute(array($prname,$prend, $prmenu, $prcontent));
 }
// somehow I need to check this
if (data inserted ok) {
  header("Location: pr-pdf2.php");
 }
}

$sbmt_caption = "continue ->";
?>

<input id="submitButton" name="submit_name" type="submit" value="<?php echo $sbmt_caption?>"/>
</form>

I have changed following Marc advise, but I don't know how to check if the SQL INSERT was OK. Could give someone give me some hint on this?

thanks in advance

Andras

the solution as I could not answer to my question (timed out:):

Here is my final code, can be a little bit simple but it works and there are possibilities to check and upgrade later. Thanks to everyone especially Marc.

<form id="pdf" method="post" action="pr-pdf1.php">
New project name:<input type="text" name="pr_name" placeholder="new project name..."><br/>
Email subject:<input type="text" name="pr_subject" placeholder="must be filled..."><br/>
New project end date:<input id="datepicker" type="text" name="pr_end" placeholder="yyyy-mm-dd..."><br/>

<textarea class="ckeditor" name="pagecontent"  id="pagecontent"></textarea>

<?php
include_once "ckeditor/ckeditor.php";
$CKEditor = new CKEditor();
$CKEditor->basePath = 'ckeditor/';
// Set global configuration (will be used by all instances of CKEditor).
$CKEditor->config['width'] = 600;   
// Change default textarea attributes
$CKEditor->textareaAttributes = array(“cols” => 80, “rows” => 10);
$CKEditor->replace("pagecontent");


if ($_SERVER['REQUEST_METHOD'] == 'POST')
 {
  // data collection
  $prname = $_POST["pr_name"];
  $prsubject = $_POST["pr_subject"];
  $prend = $_POST["pr_end"];
  $prmenu = "pdf";
  $prcontent = $_POST["pagecontent"];

  //SQL INSERT with error checking for test 
  $stmt = $pdo->prepare("INSERT INTO projects (prname, subject, enddate, sel, content) VALUES(?,?,?,?,?)");
  // error checking
  if (!$stmt) echo "\nPDO::errorInfo():\n";
  // SQL command check...
  if ($stmt->execute(array($prname, $prsubject, $prend, $prmenu, $prcontent))){
   header("Location: pr-pdf2.php");
     }
      else{
       ech开发者_如何学Co"Try again because of the SQL INSERT failing...";
      };
   }

$sbmt_caption = "continue ->";
?>

<input id="submitButton" name="submit_name" type="submit" value="<?php echo $sbmt_caption?>"/>
</form>


Add the attribute action with the url you'd like to go to. In this case it'd be

<form id="pdf" method="post" action="page2.php">

EDIT: i missed you saying this method doesn't work. What part of it doesn't work?


You should keep the action to the same script, so the POST action is still performed and then redirect with header("Location: page2.php"); when the processing is done.


A basic structure like this will do it:

form1.php:

<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
   ... process form data here ...
   if (form data ok) {
       ... insert into database ...
   }
   if (data inserted ok) {
      header("Location: form2.php");
   }
}
?>

... display page #1 form here ...

And then the same basic structure for each subsequent page. Always submit the form back to the page it came from, and redirect to the next page if everything's ok.


You're probably better off separating the php code from the form. Put the php code in a file called submit.php, set the form action equal to submit.php, and then add the line header('Location: whateverurl.com'); to your code.


The easiest way is to post it to form2.php by giving the form the attribute action="page2.php". But there's a risk in that. It means that form2 must parse the posted data of form1. Also, if the data is wrong (verification) form1 must be shown instead of form2. This will make your code over complicated and creates dependencies between the two forms.

So the better solution (and quite easy as well) is to implement the post-redirect-get pattern.

You post to form1, verify all data and store it. If the data is ok, you redirect to form2. If the data is wrong, you just show form1 again.

Redirecting is done by a header:

// Officially you'll need a full url in this header, but relative paths 
// are accepted by all browsers.
header('Location: form2.php');


Save already posted fields in hidden input fields, but don't forget to validate them every time user submits another step of the form as the user may change hidden inputs in source code.

<input type="hidden" name"some_name" value="submitted_value"/>


There are several ways handling the submitted data while jumping between steps. You will find your reasons for /against writing data to session, database, whatever... after each step or not.

I did following approach: The form includes always a complete set of input elements, but on page #1 the step-2-elements are hidden ... and other way round.

I built a 6-step-wizard this way. One large template, some JS /Ajax for validating input, additional hidden inputs that hold current step-ID and PHP deciding, which fields to show or hide.

The benfit in my opinion: Data can easily be saved completely, as soon as input is alright and complete. No garbage handling, if users abort after step 1.


I would store it all in a session array (or sub array)

a really rough example where I'm saving all the form names to an array (to be checked later of course):

<?
foreach($_POST as $k => $v){
$session['register'][$k]=$v;}
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜