201. How to prevent a form from submitting twice - Part 2?

Problem

Suppose you have set up a form and you want to prevent the user from submitting the form twice as shown below. There are two methods to accomplish this. This article shows the second method.

How to prevent a form from submitting twice - Part 2?


Solution

  • In the second method, when the user clicks on the submit button the first time, we block the signal handler on_click() from being called when the signal 'clicked' is emitted.
  • We block a signal with the method GObject::block($handler_id).

Sample Code

1   
2   
3   
4   
5   
6   
7   
8   
9   
10   
11   
12   
13   
14   
15   
17   
18   
19   
20   
21   
22   
23   
24   
25   
26   
27   
28   
29   
30   
31   
32   
33   
34   
35   
36   
37   
38   
39   
41   
43   
45   
46   
47   
48   
49   
50   
51   
52   
53   
54   
55   
57   
58   
59   
60   
61   
62   
63   
64   
65   
66   
67   
68   
69   
70   
71   
72   
73   
74   
<?php
$window = new GtkWindow();
$window->set_size_request(400, 240);
$window->connect_simple('destroy', array('Gtk','main_quit'));
$window->add($vbox = new GtkVBox());

// display title
$title = new GtkLabel("Prevent a form from submitting twice\n".
"Part 2 - using GObject::block()");
$title->modify_font(new PangoFontDescription("Times New Roman Italic 10"));
$title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff"));
$title->set_size_request(-1, 40);
$title->set_justify(Gtk::JUSTIFY_CENTER);
$alignment = new GtkAlignment(0.5, 0, 0, 0);
$alignment->add($title);
$vbox->pack_start($alignment, 0, 0);
$vbox->pack_start(new GtkLabel(), 0, 0);

$fields = array('Item number', 'Item Description', 'Unit price', 'Quantity');
$field_size = array(120, 200, 80, 80);
$entry = array(); // to store the text entries

$table = new GtkTable();
display_table($table, $fields, $field_size); // display the table
$vbox->pack_start($table);

function display_table($table, $fields, $field_size) {
    global $entry;
    $row = 0;
    foreach ($fields as $field) {
        $label = new GtkLabel("  $field: ");
        $alignment = new GtkAlignment(1, .5, 0, 0);
        $alignment->add($label);
        $table->attach($alignment, 0, 1, $row, $row+1, Gtk::FILL, Gtk::SHRINK, 0, 0);

        $entry[$row] = new GtkEntry();
        $alignment = new GtkAlignment(0, .5, 0, 0);
        $alignment->add($entry[$row]);
        $entry[$row]->set_size_request($field_size[$row], -1);
        $table->attach($alignment, 1, 2, $row, $row+1);
        ++$row;
    }
}

// create a submit button
$button = new GtkButton('Submit');
$button->set_size_request(60, 28);
$handler_id = $button->connect('clicked', 'on_click'); // note 1
$row = count($fields);
$alignment = new GtkAlignment(0, 0.5, 0, 0);
$alignment->add($button);
$table->attach($alignment, 1, 2, $row, $row+1);

$window->show_all();
Gtk::main();

function on_click($button) {
    global $fields, $entry;
    $i=0;
    foreach($fields as $field) {
        echo "$field: ".$entry[$i]->get_text()."\n";
        ++$i;
    }

    global $handler_id;
    $button->block($handler_id); // note 2
}

?>

Output

As shown above.

 

Explanation

The above example is based on How to align GtkEntry fields - Part 2?

What's new here:

  1. Make a copy of the handler id.
  2. Block the signal 'clicked'!

Note

Related Links

Add comment


Security code
Refresh