简体   繁体   中英

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

Weird question this, going round in circles.

I have 2 pages.

Page 1. Has a button on it. 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.

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.

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. Obviously onlcick of the button on page 1 we go direct to page 2.

Have the button post it's value to Page2:

Page1, I added type="submit" and 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

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

<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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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