简体   繁体   English

将单击按钮的值从一个页面传递到另一个页面输入字段

[英]Pass Value of clicked button from one page to another page Input field

Weird question this, going round in circles. 奇怪的问题,围成一圈。

I have 2 pages. 我有2页。

Page 1. Has a button on it. 第1页。上面有一个按钮。 Like 喜欢

<form action="goto_page_2.php"><p class="longdesc"><span class="spanBold">Scrap Collections in North Lakes:</span><br />
      <button class="readViewMoreBtn" value="North Lakes">Book a Collection in North Lakes</button>
      </p></form>

The above is a simple mockup. 以上是一个简单的模型。 But what I want to do is, onclick of the button, parse the Value to ... 但我想要做的是,点击按钮,将值解析为......

Page 2. Which has a form built in. 第2页。内置了一个表单。

One of the fields is Suburb. 其中一个领域是郊区。

<!-- EMAIL SUBURB -->            
                <span class="commonControlLabel">Suburb:</span>&nbsp;
                <span class="commonControlLabelItalic">(required)</span>
                <span id="contactSuburbErrorMsg" class="commonControlErrorMsg"></span><br />
                <input class="commonInput" type="text" id="inputSuburb" value=""/><br />

So what I want to do, is grab the VALUE of the button on PAGE 1, and add it to the Value in the input element on Page 2. 所以我想要做的是获取第1页上按钮的VALUE,并将其添加到第2页上输入元素中的Value。

To complicate matters, Page 1. Has quite a few buttons, with different values, all unique, which we would like to pass, to the input element. 更复杂的是,第1页。有相当多的按钮,具有不同的值,所有唯一的,我们想传递给输入元素。 Obviously onlcick of the button on page 1 we go direct to page 2. 显然,第1页上的按钮是onlcick,我们直接进入第2页。

Have the button post it's value to Page2: 让按钮将它的值发布到Page2:

Page1, I added type="submit" and name="suburb" 第1页,我添加了type="submit"name="suburb"

<button type="submit" name="suburb" class="readViewMoreBtn" value="North Lakes">Book a Collection in North Lakes</button>

Page2: I added the php part in the value attribute 第2页:我在value属性中添加了php部分

<input class="commonInput" type="text" id="inputSuburb" value="<?= $_POST['suburb'] ?>"/>
<!--page1::-->
<html>
    <body>
        <form action="test2.php" method="post">
            <button name="desc" value="1">description!</button>
        </form>
    </body>
</html>

<!--page2::-->

<?php
    echo "hello";
    echo $_POST["desc"];
?>

There are several things you can do. 你可以做几件事。

Eg: on form 2 say: 例如:在表格2上说:

<input type="text" value="<?php if(isset($_POST['inputNameForm1'])){echo htmlentities($_POST['inputNameForm1']);} ?>

Or if that is not an option for you, try something like: 或者,如果这不适合您,请尝试以下方法:

<? php

session_start();

$_SESSION['sessionName'] = $_POST['inputNameForm1']?>

<input type="text" value="<?php if(isset($_SESSION['sessionName']])){echo htmlentities($_SESSION['sessionName']]);} ?> />

Note: didn't test the code 注意:没有测试代码

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM