nested form actions ?

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
vertigo
Forum Newbie
Posts: 5
Joined: Thu Nov 13, 2003 10:49 am

nested form actions ?

Post by vertigo »

Hello
I have text input field, and i want to "connect" to that field two actions (submit's), so each of these two scripts can read from that field. How can i do that ?

Thanx again :)
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Like this?

Code: Select all

function a($var) { 
 echo $var; 
}
function b($var) { 
 echo $var; 
}

if (!empty($_POST['foo']) and strlen($_POST['foo']) > 0) {
 a($_POST['foo']);
 b($_POST['foo']);
}
(Coffeebreak, no time to verify that I wrote correctly.)
vertigo
Forum Newbie
Posts: 5
Joined: Thu Nov 13, 2003 10:49 am

two buttons

Post by vertigo »

but i need to have two buttons on my page, and each script do different things but each script need data from the same text input field.
Two buttons needed....


Thanx
vertigo
Forum Newbie
Posts: 5
Joined: Thu Nov 13, 2003 10:49 am

how to get submit value ?

Post by vertigo »

Hello again:)
I tried to make in html:

<form action=script.php method=post>
.....some fields.....
<input type=submit value=val1>
<input type=submit value=val2></form>

How can i in script.php check if i pressed button val1 or val2 ?

Thanx
Paddy
Forum Contributor
Posts: 244
Joined: Wed Jun 11, 2003 8:16 pm
Location: Hobart, Tas, Aussie
Contact:

Post by Paddy »

Ok, so you have a file called file.php

Your form code will look like this

Code: Select all

&lt;form method="post" action="file.php"&gt;
    &lt;input type="text" name="sometext"&gt;
    &lt;input type="submit" name="button1" value="Button 1"&gt;
    &lt;input type="submit" name="button2" value="Button 2"&gt;
&lt;/form&gt;
Then at the top of the page you put your php

Code: Select all

<?php
    $sometext  = (isset($_POST['sometext'])?$_POST['sometext']:"");
    $button1  = (isset($_POST['button1'])?$_POST['button1']:"");
    $button2  = (isset($_POST['button2'])?$_POST['button2']:"");

    if ($button1)
    {
         //do something to $sometext
    }
    else if ($button2)
    {
         //do something else to $sometext
    }
?>
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Paddy wrote:

Code: Select all

<?php
    $sometext  = (isset($_POST['sometext'])?$_POST['sometext']:"");
    $button1  = (isset($_POST['button1'])?$_POST['button1']:"");
    $button2  = (isset($_POST['button2'])?$_POST['button2']:"");

    if ($button1)
    {
         //do something to $sometext
    }
    else if ($button2)
    {
         //do something else to $sometext
    }
?>
Why not just:

Code: Select all

<?php
    if (@$_POST['button1'])
    {
         //do something to @$_POST['sometext']
    }
    else if (@$_POST['button2'])
    {
         //do something else to @$_POST['sometext']
    }
?>
Looks much more clear to me.
Post Reply