简体   繁体   English

如何将php变量传递给同一页面中的其他表单?

[英]How can I pass a php variable to another form in the same page?

I have two forms in the same php page. 我在同一个php页面中有两种形式。 I have to save the values in the first form and after is saved in the database, I can be able to use those values in the next form. 我必须先以第一种形式保存这些值,然后将其保存到数据库中,然后才能以下一种形式使用这些值。

PHP code of the first form: 第一种形式的PHP代码:

<?php
    include 'config.php';
    if(isset($_POST['documentRequest']))
    {
        $date = $_POST['reqDate'];
        $name = $_POST['name'];

        $insertSql = mysqli_query($conn, "INSERT INTO documentrequest(Person_idPerson, docrequest_date) VALUES ('$name', '$date');");

        //$sql = "INSERT into requestitem (DocumentRequest_idDocumentRequest, Document_idDocument) VALUE ((SELECT idDocumentRequest FROM documentrequest WHERE Person_idPerson = 6), 2)";

        header("Refresh: 2; url=doc_request.php");
        mysqli_close($conn);

    }
?>

Here is my first form: 这是我的第一个表格:

<form class="form-horizontal form-label-left" name = "documentRequest" enctype="multipart/form-data" role="form" method="post" novalidate>

    <div class = "first">
    <div class="item form-group">
        <label class="control-label col-md-3 col-sm-3 col-xs-12">ID <span class="required">*</span></label>
        <div class="col-md-6 col-sm-6 col-xs-12">
            <input id="name" class="form-control col-md-7 col-xs-12" placeholder ="Type in ID number " name="name" required="required" type="number">
        </div>
    </div>

    <div class="ln_solid"></div>
    <div class="form-group">
        <div class="col-md-6 col-md-offset-3">
            <button id="requestDiv" type="submit" class="btn btn-success" name="documentRequest" >Proceed</button>
            <button type="submit" class="btn btn-primary">Cancel</button>
        </div>
    </div>
    </div>
</form>

Second form (this is inside another div): 第二种形式(在另一个div内):

<form class="form-horizontal form-label-left" name = "documentRequest" enctype="multipart/form-data" role="form" method="post" novalidate>
    <div class = "second">
        <div class="item form-group">
            <label class="control-label col-md-3 col-sm-3 col-xs-12"> Document Request(s)  <span class="required">*</span></label>
        <div class="col-md-6 col-sm-6 col-xs-12">
            <div class = "clearfix"></div>
            <p>Minimum of 1:<p>
            <input type="checkbox" name="docs[]" id="doc1" value="Certificate of Residency" /> Certificate of Residency
            <br/>
            <div class = "col-xs-3">
    <input name="d1" class="form-control col-md-7 col-xs-12" required="required" type="number">
                </div>
    <div class = "clearfix"></div>

    <input type="checkbox" name="docs[]" id="doc2" value="Barangay Clearance"  /> Barangay Clearance                                                    
    <br />
    <div class = "col-xs-3">
    <input name="d2" class="form-control col-md-7 col-xs-12" required="required" type="number">
    </div>


    <input id="a" class="form-control col-md-7 col-xs-12" name="a" value = "<?php echo isset($_POST['name'])? $_POST['name'] : "";?>" required="required" type="text">
    </div>
    </div>
    <div class="ln_solid"></div>
    <div class="form-group">
        <div class="col-md-6 col-md-offset-3">
            <button id="requestDiv" type="submit" class="btn btn-success" name="documentRequest" >Proceed</button>
            <button type="submit" class="btn btn-primary">Cancel</button>
    </div>
    </div>
</form>

Basically, I need to use the value of ID in the next form so it knows which ID it belongs to . 基本上, 我需要以下一种形式使用ID的值,以便它知道它属于哪个ID The next form is basically about the user's requests (a series of checkboxes). 下一个表单基本上是关于用户的请求的(一系列复选框)。 How can I do that? 我怎样才能做到这一点? Please help me. 请帮我。 Thank you. 谢谢。

I'm pretty sure that when you call header("Refresh: 2; url=doc_request.php"); 我很确定当您调用header("Refresh: 2; url=doc_request.php"); you are refreshing the page and losing the submitted form data. 您正在刷新页面并丢失提交的表单数据。


If you remove that from your logic this would work: 如果您将其从逻辑中删除,这将起作用:

In your second form, where you need the value, do this: 在您需要值的第二种形式中,执行以下操作:

<input id="name2" class="form-control col-md-7 col-xs-12"  name="other-name" required="required" type="number" value="<?php echo isset($_POST['name']) ? $_POST['name'] : '';?>">

This will automatically add the posted value to the second form's input element value if the posted value exists 如果存在过帐的值,这将自动将过帐的值添加到第二个表单的输入元素值中

Note that checking isset() first is necessary.Trying to simply echo the value would result in an error being thrown when the value does not exist (before the first form is submitted) 注意首先检查isset()是必要的,尝试简单地回显该值将导致在该值不存在时(在提交第一个表单之前)抛出错误


Otherwise, you can use sessions to achieve this effect: 否则,您可以使用sessions来实现此效果:

At the very top of your php page add: 在您的php页面的最顶部添加:

session_start();
$_SESSION['name']=isset($_POST['name']) ? $_POST['name'] :'';

Then in your second form do this: 然后以第二种形式执行此操作:

<input id="name2" class="form-control col-md-7 col-xs-12"  name="other-name" required="required" type="number" value="<?php echo $_SESSION['name']">

If your form action is returning to the same page you can try this: 如果您的表单操作返回到同一页面,则可以尝试以下操作:

<form id="form-two">
<input id="name" name="name" value="<?php echo $_POST['name']; ?>">
</form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将变量从 php 页面传递并放入另一个页面(表单)? - How to pass and put variable from a php page into a another page(form)? 如何在一个页面中将变量从一个函数传递给另一个函数? - how to pass variable from one function to another in the same page in php? 如何将php变量从一个div传递到同一页面中的另一个div,而不是传递给另一页面? - How to pass php variable from one div to another div in the same page than pass to another page? 如何使用HTML表单将JavaScript值传递到另一个PHP页面? - How can I pass JavaScript values to another PHP page using an HTML form? 如何使用模态窗口在同一页面的PHP中传递JavaScript变量值 - How can I pass JavaScript variable value in php with same page using modal window 如何将网址变量从一页传递到另一页? - How can I pass on a url variable from 1 page to another? 如何将变量从一页传递到另一页? - How can i pass a variable from one page to another? 如何将PHP变量传递到同一页面中的链接 - How to pass a PHP variable to a link in the same page 如何在同一页面中将javascript变量传递给php - how to pass the javascript variable in to php in same page 如何在php中将变量一页传递给另一页 - How to pass Variable One page to another in php
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM