Wert von hidden-input per JavaScript verändern
am 27.01.2010 - 21:28 Uhr in
Hallo,
ich bastel mir grad ein Drag'n Drop Formular auf dem ich ein Bild per Drag'n Drop auf der Seite verschieben kann.
Die Position des Bildes wird in zwei hidden-felder geschrieben und per klick auf speichern werden die werte dann in die Datenbank geschrieben.
Jetzt habe ich folgendes Problem: In der function tactic_show_form_submit($form, &$form_state)
gebe ich in der ersten Zeile das $form_state per print_r aus und bekomme folgendes:
Array (
[storage] =>
[submitted] => 1
[values] => Array ( <strong>[player1_x] => 100 [player1_y] => 200</strong> [player1_index] => [tactic_id] => 1 [op] => Save Tactic [submit] => Save Tactic [form_build_id] => form-74ed37d3bde81726d0f7c8b2503c0661 [form_token] => 47b286e67e42b2db5ee6306ecad7aab1 [form_id] => tactic_show_form )
[clicked_button] => Array ( [#type] => submit [#value] => Save Tactic [#post] => Array ( <strong>[player1_x] => 131 [player1_y] => 103</strong> [player1_index] => [op] => Save Tactic [form_build_id] => form-cc2e5ff7427d67246c92f43d3224d178 [form_token] => 47b286e67e42b2db5ee6306ecad7aab1 [form_id] => tactic_show_form ) [#programmed] => [#tree] => [#parents] => Array ( [0] => submit ) [#array_parents] => Array ( [0] => submit ) [#weight] => 0.004 [#processed] => [#description] => [#attributes] => Array ( ) [#required] => [#input] => 1 [#name] => op [#button_type] => submit [#executes_submit_callback] => 1 [#process] => Array ( [0] => form_expand_ahah ) [#id] => edit-submit )
[redirect] =>
)
player1_x und player1_y sind die hidden-Felder.
Die unteren Werte sind korrekt, die oberen sind die defaultwerte der hiddenfelder. Wie kommt das, dass die unterschiedlich sind? Sollte man in der _form_submit nicht eigentlich das [values] auslesen um an die Werte zu kommen?
Die relevanten Zeilen aus dem JavaScript:
_posx = $('edit-player1-x');
_posy = $('edit-player1-y');
.
.
.
_posx.value = newx+20;
_posy.value = newy+20;
- Anmelden oder Registrieren um Kommentare zu schreiben
Formular
am 28.01.2010 - 09:10 Uhr
Wie hast Du denn das Formular erzeugt?
PS:
<?php drupal_set_message('<pre>' . var_export($form_state, 1) . '</pre>'); ?>
erzeugt wesentlich lesbarere Debug-Ausgaben.Stefan
--
Sei nett zu Deinem Themer!
danke für den Tipp mit der
am 28.01.2010 - 20:02 Uhr
danke für den Tipp mit der debugausgabe :)
Also die Formularerzeugung:
<?php
function tactic_show_form($form_state,$players,$tactic_id){
foreach($players as $player){
$form['player'.$player->player_id.'_x'] =array('#type' => 'hidden', '#value' => $player->tactic_pos_x);
$form['player'.$player->player_id.'_y'] =array('#type' => 'hidden', '#value' => $player->tactic_pos_y);
$form['player'.$player->player_id.'_index'] =array('#type' => 'hidden', '#value' => $player->tactic_index);
}
$form['tactic_id'] =array('#type' => 'value', '#value' => $tactic_id);
$form['submit'] = array('#type' => 'submit', '#value' => t('Save Tactic'));
return $form;
}
?>
der kmplette html-Quellcode des Forumlars:
<form action="/drupal/?q=tactic/0" accept-charset="UTF-8" method="post" id="tactic-show-form">
<div><input type="hidden" name="player1_x" id="edit-player1-x" value="100" />
<input type="hidden" name="player1_y" id="edit-player1-y" value="200" />
<input type="hidden" name="player1_index" id="edit-player1-index" value="" />
<input type="submit" name="op" id="edit-submit" value="Save Tactic" class="form-submit" />
<input type="hidden" name="form_build_id" id="form-1f627f0058decb32e080ae2be524f262" value="form-1f627f0058decb32e080ae2be524f262" />
<input type="hidden" name="form_token" id="edit-tactic-show-form-form-token" value="47b286e67e42b2db5ee6306ecad7aab1" />
<input type="hidden" name="form_id" id="edit-tactic-show-form" value="tactic_show_form" />
</div></form>
Und die Debugausgabe:
<div class="messages status">
<pre>array (
'storage' => NULL,
'submitted' => true,
'values' =>
array (
'player1_x' => '100',
'player1_y' => '200',
'player1_index' => NULL,
'tactic_id' => '1',
'op' => 'Save Tactic',
'submit' => 'Save Tactic',
'form_build_id' => 'form-0f29dcc65bc0eb064c0a4006a1089ae2',
'form_token' => '47b286e67e42b2db5ee6306ecad7aab1',
'form_id' => 'tactic_show_form',
),
'clicked_button' =>
array (
'#type' => 'submit',
'#value' => 'Save Tactic',
'#post' =>
array (
'player1_x' => '106',
'player1_y' => '32',
'player1_index' => '',
'op' => 'Save Tactic',
'form_build_id' => 'form-2dbe143e2fd3d17ebbe928d69598b624',
'form_token' => '47b286e67e42b2db5ee6306ecad7aab1',
'form_id' => 'tactic_show_form',
),
'#programmed' => false,
'#tree' => false,
'#parents' =>
array (
0 => 'submit',
),
'#array_parents' =>
array (
0 => 'submit',
),
'#weight' => 0.004,
'#processed' => false,
'#description' => NULL,
'#attributes' =>
array (
),
'#required' => false,
'#input' => true,
'#name' => 'op',
'#button_type' => 'submit',
'#executes_submit_callback' => true,
'#process' =>
array (
0 => 'form_expand_ahah',
),
'#id' => 'edit-submit',
),
'redirect' => NULL,
)</pre></div>
Ich verstehs nicht, eigentlich sollte der Wert doch übernommen werden, oder? Bzw er wird ja teilweise übernommen aber halt nciht in dem values-array
Hier liegt der
am 06.02.2010 - 10:36 Uhr
Hier liegt der Fehler:
<?php
function tactic_show_form($form_state,$players,$tactic_id){
foreach($players as $player){
$form['player'.$player->player_id.'_x'] =array('#type' => 'hidden', '#value' => $player->tactic_pos_x);
$form['player'.$player->player_id.'_y'] =array('#type' => 'hidden', '#value' => $player->tactic_pos_y);
$form['player'.$player->player_id.'_index'] =array('#type' => 'hidden', '#value' => $player->tactic_index);
}
$form['tactic_id'] =array('#type' => 'value', '#value' => $tactic_id);
$form['submit'] = array('#type' => 'submit', '#value' => t('Save Tactic'));
return $form;
}
?>
Wenn man den #value der Felder setzt, bleibt der bestehen und ist nicht vom User änderbar.
Man muss den #default_value ändern, dadurch kann der User den Wert des Feldes auch selbst ändern.
Auszug aus der forms_api_reference
#default_value
Used by: checkbox, checkboxes, date, hidden, radio, radios, select, textarea, textfield, token, weight
Description: The value of the form element that will be displayed or selected initially if the form has not been submitted yet. Should NOT be confused with #value, which is a hard-coded value the user cannot change!
#value
Used by: button, hidden, image_button, item, markup, submit, token, value
Description: Used to set values that cannot be edited by the user. Should NOT be confused with #default_value, which is for form inputs where users can override the default value.