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


February 2019


20.7k time


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>

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.


3 answers


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'] ?>"/>
        <form action="test2.php" method="post">
            <button name="desc" value="1">description!</button>


    echo "hello";
    echo $_POST["desc"];

There are several things you can do.

E.g: 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['sessionName'] = $_POST['inputNameForm1']?>

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

Note: didn't test the code