How can I pass a veriable -----

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dilruk83
Forum Newbie
Posts: 13
Joined: Thu Oct 30, 2008 6:44 am

How can I pass a veriable -----

Post by dilruk83 »

I have two buttons named 'go' and 'sub' and one form to do this.At same form i have two click events.

echo "<input type='submit' name='go' id='go' value='Go' />"; // button go

echo "<input type="submit" name="sub" id="button" value="Submit" />"; // button sub

echo "<input type='text' name='type' id='type' value='$router' />"; // This is a texbox. This value-$router pass from a list box

if (isset($_POST['go'])) // when user click on go button
{
$r=$_POST['type'];
echo $r; // this printed $r value.
}

if (isset($_POST['sub']))
{

// problem is...
// How can i get $r value to here...?

echo "Router : ".$r; // I got null value for this

// How can i get $r value to this click event..?


}
pa28
Forum Newbie
Posts: 7
Joined: Mon Oct 05, 2009 9:26 pm

Re: How can I pass a veriable -----

Post by pa28 »

Because your $r variable is created within a set of braces ( {} ), it is only accessible within those braces. You either need to define it outside of your 'if' statement, or define it within each set of braces. The first method is neater IMHO:

Code: Select all

 
if(isset($_POST['go']) || isset($_POST['sub']) {
    $r = $_POST['type'];
    if (isset($_POST['go'])) {
        echo $r; // this printed $r value.
    } elseif(isset($_POST['sub'])) {
        echo "Router : ".$r; // I got null value for this
    }
}
 
On a minor point of efficiency, since only one of the two submit buttons can be pressed for a particular request, it's nicer to use an 'if...elseif' statement than two 'if's.
dilruk83
Forum Newbie
Posts: 13
Joined: Thu Oct 30, 2008 6:44 am

Re: How can I pass a veriable -----

Post by dilruk83 »

Thank you dear friend.....really thank you.....

if i have a listbox instead of textbox how can Listbox remember that user selected Item..?

I have a ListMenu names MyListBox.

<select name="MyListMenu" id="MyListMenu">
<option>a</option>
<option>b</option>
<option>c</option>
</select>

<input type="submit" name="button" id="button" value="Submit" />

If i select Item 'b' from MyListMenu and click on button, I wanna remaining (remember) item 'b' on MyListMenu after click on the Button.

How can i do this..?

Thank you...
Post Reply