Re: QF: javascript confirm on submit
| From: | Andrew Kornilov | Date: | Mon, 25 Jul 2005 17:20:34 +0000 |
| Subject: | Re: QF: javascript confirm on submit | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-20129@lists.php.net to get a copy of this message | ||
понеділок, 25-лип-2005 18:26, PitesaJ@peteramayer.com Ви написали:
> I need to put a javascript confirm() on submit. If I use:
>
> $g_buttons[] = & HTML_QuickForm::createElement('submit','btnSubmit',
> 'Submit', array('onclick'=>'goConfirm()'));
>
> function goConfirm() {
> var answer = confirm ("Are you sure it is OK to Delete?")
> if (!answer)
> window.location="menu_redirect.php"
> }
>
> Then no matter if "OK" or "Cancel" the form is submitted.
>
> If I change the input element from submit to button as follows:
>
> $g_buttons[] = & HTML_QuickForm::createElement('button','action',
> 'Submit', array('onclick'=>'goConfirm()'));
>
> and change the javascript to:
>
> function goConfirm() {
> var answer = confirm ("Are you sure it is OK to Delete?")
> if (!answer){
> window.location="menu_redirect.php"
> } else {
> document.frmPubEdit.submit();
> }
> }
>
> It works. However, per the documentation
>
>
> We don't need values from button-type elements (except submit)
>
> Well I do. I have several submit buttons on the page which submits the
> data then directs you to another specific QF and then back. Since there
> is not a value assigned to the button I cannot trap when this button is
> clicked. A nested if statement eliminating the values of the other
> buttons works in most cases but not this particular form (which leads to
> another post).
>
> Is there a better way to get the confirm message to work or can I assign a
> value to a button even if it was not anticipated necessary?
confirmedsubmit.php
<?php
require_once("HTML/QuickForm/submit.php");
/**
* HTML class for a text field
*
* @author Andrew Kornilov <frutik@gmail.com>
* @version 1.0
* @access public
*/
class HTML_QuickForm_confirmed_submit extends HTML_QuickForm_submit
{
// {{{ constructor
/**
* Class constructor
*
* @param string $elementName (optional)Input field name attribute
* @param string $elementLabel (optional)Input field label
* @param mixed $attributes (optional)Either a typical HTML attribute string
* or an associative array
* @param mixed $message (optional)String confirmation question
* @access public
* @return void
*/
function HTML_QuickForm_confirmed_submit($elementName=null, $elementLabel=null,
$attributes=null, $warning=null)
{
HTML_QuickForm_submit::HTML_QuickForm_submit($elementName, $elementLabel, $attributes);
$warning = empty($warning) ? 'are you sure?' : $warning;
$this->setWarning($warning);
} //end constructor
// }}}
// {{{ setWarning()
/**
* Sets warning text for submit button
*
* @param string $warning Warning string
* @since 1.0
* @access public
* @return void
*/
function setWarning($warning)
{
$warn = "if(!confirm('" . $warning . "')){ return false; }";
$this->updateAttributes(array('onclick' => $warn));
} //end func setWarning
// }}}
} //end class HTML_QuickForm_confirmed_submit
?>
test.php
<?
error_reporting(E_ALL);
require_once 'HTML/QuickForm.php';
$form =& new HTML_QuickForm;
$form->registerElementType('confirmedsubmit', 'confirmedsubmit.php',
'HTML_QuickForm_confirmed_submit');
$form->addElement('text', 'login', 'variable');
$submit0 =& $form->addElement('confirmedsubmit', 's0', 'Save');
$submit1 =& $form->addElement('confirmedsubmit', 's1',
'Save',null,'Surewwww?');
$submit2 =& $form->addElement('confirmedsubmit', 's2', 'Save');
$submit2->setWarning('Sure?');
$form->display();
?>
but it is not a solution if user just press enter on any text element of form
--
Andrew Kornilov