草庐IT

php get函数将表单传输到另一个页面,如果编辑回表单

coder 2023-10-18 原文

好的伙计们..

我有两个页面

page1 包含一个表单

 <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>

我使用 php session 将数据发送到 page2

php page1代码

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();
}

现在是第2页

page2 名称是 form1_conf.php & 用于向用户显示表单数据,以便他可以检查表单是否有错误,如果有任何错误,他可以单击编辑并返回主表单(page1)并重新- 输入数据并重新提交表格。

这是page2的代码

这里我使用 php 从 page1 接收数据:

<?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];
}
?>

这是显示这个的html代码

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

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

现在我有两个问题

  1. 人们告诉我,如果在多个选项卡中打开表单, session 可能会成为问题, session 可能会困惑。所以我被建议使用 get。如果此声明有效,应在 php 代码中的何处进行修改以使用 get 或更好的方法来完成它?
  2. 无论此代码是否经过修改以不通过 get 获取工作,如果用户单击 page2 上的编辑按钮,此表单如何重定向到 page1?看到 # 和 EDIT 按钮。如何实现?

在此先感谢您的帮助 friend 。

最佳答案

好吧,这是在一页中执行此操作的解决方案:

表单.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>

希望这对您有所帮助!

关于php get函数将表单传输到另一个页面,如果编辑回表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17917589/

有关php get函数将表单传输到另一个页面,如果编辑回表单的更多相关文章

  1. ruby-on-rails - Rails 编辑表单不显示嵌套项 - 2

    我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib

  2. ruby - 使用 Vim Rails,您可以创建一个新的迁移文件并一次性打开它吗? - 2

    使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta

  3. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  4. ruby-on-rails - 渲染另一个 Controller 的 View - 2

    我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>

  5. ruby-on-rails - 如果为空或不验证数值,则使属性默认为 0 - 2

    我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val

  6. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

  7. ruby-on-rails - 如果 Object::try 被发送到一个 nil 对象,为什么它会起作用? - 2

    如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象

  8. ruby - 为什么 SecureRandom.uuid 创建一个唯一的字符串? - 2

    关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion为什么SecureRandom.uuid创建一个唯一的字符串?SecureRandom.uuid#=>"35cb4e30-54e1-49f9-b5ce-4134799eb2c0"SecureRandom.uuid方法创建的字符串从不重复?

  9. ruby - 如果指定键的值在数组中相同,如何合并哈希 - 2

    我有一个这样的哈希数组:[{:foo=>2,:date=>Sat,01Sep2014},{:foo2=>2,:date=>Sat,02Sep2014},{:foo3=>3,:date=>Sat,01Sep2014},{:foo4=>4,:date=>Sat,03Sep2014},{:foo5=>5,:date=>Sat,02Sep2014}]如果:date相同,我想合并哈希值。我对上面数组的期望是:[{:foo=>2,:foo3=>3,:date=>Sat,01Sep2014},{:foo2=>2,:foo5=>5:date=>Sat,02Sep2014},{:foo4=>4,:dat

  10. ruby - 如何在 Rails 4 中使用表单对象之前的验证回调? - 2

    我有一个服务模型/表及其注册表。在表单中,我几乎拥有服务的所有字段,但我想在验证服务对象之前自动设置其中一些值。示例:--服务Controller#创建Action:defcreate@service=Service.new@service_form=ServiceFormObject.new(@service)@service_form.validate(params[:service_form_object])and@service_form.saverespond_with(@service_form,location:admin_services_path)end在验证@ser

随机推荐