dongxinm279890 2014-10-07 11:53
浏览 51
已采纳

在php中创建一个多步骤表单

I am trying to create a form in multi steps. what i am trying to do is in the first page user will enter two field i.e title and description which will get saved in the database and then they are redirected to the second page where they need to few more details, but here i want to put a condition that these values should be added in front of the row that has the value of title which was saved in the previous page.

here is what i have done so far

<?php
include('creator_session.php');
?>
<!DOCTYPE html>
<html lang="en">
  <head>
     //necessary data
  </head>
  <body>
  <form class="form-horizontal" role="form" action="insert_page_one.php" enctype="multipart/form-data" method="post">
    <div class="row">
        <div class="col-md-4 col-md-offset-4"><input class="form-control" value="Project Name" type="text" name="title"></div>
    </div>

    <div class="row">
        <div class="col-md-4 col-md-offset-4"><input class="form-control" value="Project Name" type="text" name="title"></div>
    </div>

    <div class="row">
        <div class="col-md-4 col-md-offset-4"><input class="btn btn-primary" value="Get Started" type="submit" name="submit"></div>
    </div>
</form>
<body>
</html>

insert_page_one.php

<?php
include('creator_session.php');
$con=mysqli_connect("xyz.com","xyz","xyz","xyz");
if (mysqli_connect_errno()) 
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
// escape variables for security
$title = mysqli_real_escape_string($con, $_POST['title']);
$category = mysqli_real_escape_string($con, $_POST['category']);

$sql="INSERT INTO creatorproject(title,category) VALUES ('$title','$category')";

if (!mysqli_query($con,$sql)) 
    {
        die('Error: ' . mysqli_error($con));
    }

header("Location: page_two.php");
mysqli_close($con);
?>

page_two.php

<?php
include('creator_session.php');
?>
<!DOCTYPE html>
<html lang="en">
  <head>
     //necessary data
  </head>
<body>
<form class="form-horizontal" role="form" action="creator_page_two.php" enctype="multipart/form-data" method="post">
    <div class="form-group">
        <label class="col-md-3 control-label">Description</label>
            <div class="col-md-8">
                <input class="form-control" name="description" value="" type="">
            </div>
    </div>

    <div class="form-group">
        <label class="col-md-3 control-label">Project Location:</label>
            <div class="col-md-8">
                <input class="form-control" name="projectlocation" value="" type="">
            </div>
    </div>

    <div class="form-group">
        <label class="col-md-3 control-label">Funding Goal:</label>
            <div class="col-md-8">
                <input class="form-control" name="funding" value="" type="">
            </div>
    </div>              

    <div class="submit">
        <input type="submit" value="SAVE">
    </div>
</form>
</body>
</html>

creator_page_two.php

<?php
include('creator_session.php');

$con=mysqli_connect("xyz","xyz","xyz","xyz");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// escape variables for security

$description = mysqli_real_escape_string($con, $_POST['description']);
$projectlocation = mysqli_real_escape_string($con, $_POST['projectlocation']);
$funding = mysqli_real_escape_string($con, $_POST['funding']);

$sql = "UPDATE creatorproject SET description='".$description."', projectlocation='".$projectlocation."', funding='".$funding."'  WHERE title ='".$title."'";

if (!mysqli_query($con,$sql)) {
  die('Error: ' . mysqli_error($con));
}
header("Location: creator_add_project.php");
exit;

mysqli_close($con);
?>

When i run this code it is not inserting values from the second page.. Would appreciate some help and would be grateful if someone could explain the procedure as i am new to this field and would love to learn about it.

P.S- i have also used creator_session that stores the login details of the user as the user have to fill these forms after signing in, therefore the entire login process has been done with the session

  • 写回答

1条回答 默认 最新

  • duanan5940 2014-10-07 11:57
    关注

    funding='".$funding."', WHERE should look like funding='".$funding."' WHERE in creator_page_two.php

    Delete the comma (,) before WHERE.

    EDIT:

    In insert_page_one.php change header("Location: page_two.php"); to header("Location: page_two.php?title=".urlencode($title));;

    And in page_two.php change

    <form class="form-horizontal" role="form" action="creator_page_two.php" enctype="multipart/form-data" method="post">
    

    to

    <form class="form-horizontal" role="form" action="creator_page_two.php?title=<?php echo $_GET['title'] ?>" enctype="multipart/form-data" method="post">
    

    And in creator_page_two.php add $title= urldecode($_GET['title']);

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?