douque2016 2012-04-04 13:40
浏览 55
已采纳

PHP表单输入步骤,循环错误发生

I am fairly new to PHP, but have applied my previous knowledge of programming, along with internet searches to get me this far.

I'm trying to make a step by step form entry, where the criteria entered on a page by page basis and only updates the database once the entire form is filled out. I plan on embellishing the form further after I get the basic functionality working on it.

As of the moment the name variable is getting populated, however after submission of the second stage it loops back to the first stage of entry, although the URL updates to stage 3, because of this the information can not get parsed to the database.

I am using PHP v5, I have seen resolutions to this issue using AJAX, but I am not using it in this instance.

Thank you for your time and efforts.

 <?php 

$stage = $_GET['stage'];

//Starting on form 1
$stage = 'one';

//get form details
$name = $_POST['name'];
$surname = $_POST['surname'];

/* ---- Checking if you submitted and if you were on form one THEN set the form the stage2 in order to display the next form ---- */
if (isset($_POST['submitStage1']) && $stage == 'one') {
$stage = 'two';
echo "checking sumbit from stage1";
}
if (isset($_POST['submitStage2']) && $stage == 'two') {
$stage = 'three';
echo "checking submit from stage2 going to 3";
} 

if (isset($_POST['submitStage3']) && $stage == 'three') {
    $stage = 'three';
echo "Adding to database";

}
?>



<?php 
if ($stage == 'one') {
echo $stage;
?>

<div style="width:100%; margin-bottom: 20px;"> Step_001</div>

<form method="post" action="addperson.php?stage=2">
<input name="name" type="text" />
<input name="submitStage1" type="submit" />
</form>

<?php
}
?>





<?php
if ($stage == 'two') {
echo $stage;
?> 

<div style="width:100%; margin-bottom: 20px;"> Step_002</div>

<form method="post" action="addperson.php?stage=3">

<input name="surname" type="text" />
<input name="submitStage2" type="submit" />

</form>

<?php
}
?>




<?php if ($stage == 'three') { echo $stage;

echo "Adding to database";


    $name = mysql_real_escape_string($_POST['name']);   $surname = mysql_real_escape_string($_POST['surname']);     

// Add this product into the database now   
$sql = mysql_query("INSERT INTO testpeople (name, surname) 
        VALUES('$name','$surname')") or die (mysql_error());

    exit(); } ?>
  • 写回答

2条回答 默认 最新

  • dsfhe34889 2012-04-04 13:51
    关注

    Remove

    $stage = 'one' ;
    

    Replace

    $stage = $_GET['stage'];
    

    With

    $stage = isset($_GET['stage']) ? $_GET['stage'] : "one" ;
    

    Thanks

    :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法