You are here: Re: Two Submit buttons on one form « PHP Programming Language « IT news, forums, messages
Re: Two Submit buttons on one form

Posted by thib΄ on 01/13/08 05:53

axlq wrote:
> If you want two buttons in the same form to post to different
> locations, you need javascript to modify the form URL. Not a good
> idea, IMO, to rely on javascript when you don't have to.

I agree, PHP could handle it, just post the form to a single script which
will redirect to your desired location. In order to do this, I think you
could use CURL to re-submit the form data to the desired location, or just
think twice about it and hu.. I don't know.. ask the user to verify the data
he just entered and re-submit it itself =P. It's always worth it anyway !

---form.html---
<form action="post-redirector.php" method="post">
....
<input type="submit" name="button1" value="go to location 1" /><br />
<input type="submit" name="button2" value="go to location 2" />
<input type="hidden" name="form_sent" value="1" />
</form>

---post-redirector.php---
<?php

# regen_form(): regenerates hidden form from array (usually $_POST)
function regen_form($data, $array_name=null) {
foreach( $data as $field => $value ) {
if( is_array($value) ) {
regen_form($value, $field);
} else {
if( isset($array_name) ) $field = sprintf('%s[%s]',
$array_name, $field);
echo ' <input type="hidden" name="' . $field . '" value="' .
$value . '" />' . "\r\n";
}
}
}

if( isset($_POST['form_sent']) ) {
// stripslashes if hellish_quote_gpc on

// it's useless to sanitize your data here, as someone could always
// bypass the redirector to directly submit a custom form to the
// final script.

if ( isset($_POST['button1']) ) $location = 'location1.php';
elseif( isset($_POST['button2']) ) $location = 'location2.php';
else die('internal error #01 / hijack attempt');

ob_start();
ob_implicit_flush(false);
regen_form($_POST);
$form = ob_get_contents();
ob_clean();
}

?>
<!-- xhtml strict dtd, html header, styles, body tag; that stuff.. -->

<p>
please verify your information:<br />
<!-- print the $_POST here, you might want to use a variant of
regen_form(), write it manually or even lazily print_r() it -->
</p>

<p>
<form action="<?php echo $location; ?>" method="post">
<?php echo $form; ?>
<input type="submit" name="verified" value="it's fine" />
</form>
</p>

<!-- body tag end, html footer, etc. -->
<?php ob_end_flush() ?>


The function is recursive and reproduces form-generated arrays so you don't
miss a thing; but you can just rewrite the form manually as well.

The output buffer is there by (good?) habit, not absolutely needed.


Of course, you can also pass the serialized & encoded $_POST via $_GET,
process it and then directly redirect the user or reload the page without
the $_GET (and thus don't process anything after reload) but I think you
might be limited in the size of the form.


Good luck ^^'.

-thibΒ΄

 

Navigation:

[Reply to this message]


УдалСнная Ρ€Π°Π±ΠΎΡ‚Π° для программистов  •  Как Π·Π°Ρ€Π°Π±ΠΎΡ‚Π°Ρ‚ΡŒ Π½Π° Google AdSense  •  England, UK  •  ΡΡ‚Π°Ρ‚ΡŒΠΈ Π½Π° английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Π‘Π°ΠΉΡ‚ ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ Π² Π‘Ρ‚ΡƒΠ΄ΠΈΠΈ Π’Π°Π»Π΅Π½Ρ‚ΠΈΠ½Π° ΠŸΠ΅Ρ‚Ρ€ΡƒΡ‡Π΅ΠΊΠ°
ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ΠΈΠ΅ ΠΈ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠ° Π²Π΅Π±-сайтов, Ρ€Π°Π·Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ½ΠΎΠ³ΠΎ обСспСчСния, поисковая оптимизация