Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to pass a php variable to another page using a button (html/php)?

Tags:

html

php

For example, say you have a loop that produces a button every time it loops. So if the loop ran five times, there would be 5 buttons. Now say i have a variable like $num which will change each time the loop loops. Is there a way so that each button will pass its individual $num when the button is clicked?

$num = 0;
while(...){
    //want this to send $num via POST or GET to xyz.php
    echo '<button type="submit" value="3" name="editId">Remove</button>';
    $num++;//$num changes every instance of the loop
}
like image 851
Evan Cathcart Avatar asked Feb 17 '12 03:02

Evan Cathcart


People also ask

How do I pass a variable from one page to another page in PHP?

Open your web browser and type your localhost address followed by '\form1. php'. Output: It will open your form like this, asked information will be passed to the PHP page linked with the form (action=”form2. php”) with the use of the POST method.

How many ways we can pass the variable through the navigation between the pages in PHP?

Three ways can pass the variable through the navigation between the pages: Put the variable into session in the first page, and get it back from session in the next page.

How do you pass a variable in a URL?

To add a URL variable to each link, go to the Advanced tab of the link editor. In the URL Variables field, you will enter a variable and value pair like so: variable=value. For example, let's say we are creating links for each store and manager.


2 Answers

The easiest way is to create a <form> surrounding each of the <button> tags:

$num = 0;
while(...){
    //want this to send $num via POST or GET to xyz.php
    echo '<form method="post" action="xyz.php">';
    // Pass $num into the form value
    echo '<button type="submit" value="' . $num. '" name="editId">Remove</button>';
    echo '</form>'
    $num++;
}

Whichever is clicked will be passed to xyz.php as $_POST['editId'].

While this method creates a different form for each button, there are other ways to do it using JavaScript which would use a single form and assign the value to a hidden input based on the button clicked. If it's only the one input value being sent, the multiple <form>s option is probably easiest to code.

like image 162
Michael Berkowski Avatar answered Sep 24 '22 06:09

Michael Berkowski


The button's name & value should be submitted with the form.

For example:

<?php print_r($_POST); ?>

<form action="" method="post">
    <button type="submit" name="button" value="value">Button</button>
</form>

When the button "Button" is clicked, the form will be submitted and

Array ( [button] => value ) 

Will be output above the form.

Applying this to your situation, one could use something like:

$num = 0;
while(...){
    // Set $num as the value of each button
    echo "<button type='submit' value='{$num}' name='editId'>Remove</button>";
    $num++;//$num changes every instance of the loop
}
like image 34
Michael Robinson Avatar answered Sep 21 '22 06:09

Michael Robinson