Reputation: 213
I made a form in php. I am using 3 different forms in the document. So I have to distinguish these forms. Therefore I am using the submit "name-tag".
if ('POST' == $_SERVER['REQUEST_METHOD']) {
if (isset($_POST['submit_1'])) {
....
}
if (isset($_POST['submit_2'])) {
....
}
if (isset($_POST['submit_3'])) {
....
}
}
This is how I check these different submit-buttons.
But I noticed that I need to use the click function in JS instead of the submit function (cause the form shouldn't be submitted with a page-reload)
$('.erzaehlcafe_submit').click(function(){
if ($('#year_from').val() == 'none' || $('#month_from').val() == 'none'
|| $('#day_from').val() == 'none' || $('#topic').val() == '' || $('#contributer').val() == ''
|| $('#begin').val() == '' || $('#place').val() == '' || $('#entrance').val() == '') {
$("#dialog_empty").dialog( "open" );
return false;
}
var form = $('#erzaehlcafe_add');
var data = form.serialize();
$.ajax({
url: "index.php?section=events",
type: "POST",
data: data,
success: function (reqCode) {
alert(reqCode);
if (reqCode == 1) {
//Date inserted into DB
$("#dialog_add_event").dialog( "open" );
} else {
$("#dialog_add_event_error").dialog( "open" );
}
}
});
return false; //dont submit form
});
So how to make a difference between these forms? (All of them are using same names for same input).
The only way to solve the problem is to define other name indexes for all input typed. Is there any other way?
Edit: Here is my template ( remember I used submit_1, submit_2 and submit_3 instead of erzaehlcafe_submit and so on to make it more precise)
<div id="event_accordion">
<h3><a href="#">Erzählcafé hinzufügen</a></h3>
<div>
<form id="erzaehlcafe_add" action="index.php?section=events" method="post" accept-charset="utf-8">
<table>
<tr>
<td>Datum:</td>
<td>
<select name="day_from" id="day_from">
<option value="none" class="bold italic">Tag</option>
<?php
for($i=1; $i<=31; $i++){
echo "<option value=\"".$i."\">".$i."</option>\n";
}
?>
</select>
<select name="month_from" id="month_from">
<option value="none" class="bold italic">Monat</option>
<?php
for($i=1; $i<=12; $i++){
echo "<option value=\"".$i."\">".$month_name[$i]."</option>\n";
}
?>
</select>
<select name="year_from" id="year_from">
<option value="none" class="bold italic">Jahr</option>
<?php
for($i=2008; $i<=$year; $i++){
echo "<option value=\"".$i."\">".$i."</option>\n";
}
?>
</select>
</td>
</tr>
<tr>
<td>Thema:</td>
<td><input type="text" name="topic" id="topic" /></td>
</tr>
<tr>
<td>Referent:</td>
<td><input type="text" name="contributer" id="contributer" /></td>
</tr>
<tr>
<td>Beginn:</td>
<td><input type="text" name="begin" id="begin" /> Uhr</td>
</tr>
<tr>
<td>Ort:</td>
<td><input type="text" name="place" id="place" /></td>
</tr>
<tr>
<td>Eintritt:</td>
<td><input type="text" name="entrance" id="entrance" /> €</td>
</tr>
</table>
<br />
<div id="add_erzaehlcafe">
<input type="submit" id="small" class="erzaehlcafe_submit" name="erzaehlcafe_submit" value="speichern">
</div>
</form>
</div>
<h3><a href="#">Vortrag hinzufügen</a></h3>
<div>
<form name="vortrag_add" action="index.php?section=events" method="post" accept-charset="utf-8">
<table>
<tr>
<td>Datum:</td>
<td>
<select name="day_from">
<option value="0" class="bold italic">Tag</option>
<?php
for($i=1; $i<=31; $i++){
echo "<option value=\"".$i."\">".$i."</option>\n";
}
?>
</select>
<select name="month_from">
<option value="0" class="bold italic">Monat</option>
<?php
for($i=1; $i<=12; $i++){
echo "<option value=\"".$i."\">".$month_name[$i]."</option>\n";
}
?>
</select>
<select name="year_from">
<option value="0" class="bold italic">Jahr</option>
<?php
for($i=2008; $i<=$year; $i++){
echo "<option value=\"".$i."\">".$i."</option>\n";
}
?>
</select>
</td>
</tr>
<tr>
<td>Thema:</td>
<td><input type="text" name="name_topic" id="name_topic" /></td>
</tr>
<tr>
<td>Referent:</td>
<td><input type="text" name="name_contributer" id="name_contributer" /></td>
</tr>
<tr>
<td>Beginn:</td>
<td><input type="text" name="name_begin" id="name_begin" /> Uhr</td>
</tr>
<tr>
<td>Ort:</td>
<td><input type="text" name="name_place" id="name_place" /></td>
</tr>
<tr>
<td>Eintritt:</td>
<td><input type="text" name="name_entrance" id="name_entrance" /> €</td>
</tr>
</table>
<br />
<div id="add_vortrag">
<input type="submit" id="small" class="vortrag_submit" name="vortrag_submit" value="speichern">
</div>
</form>
</div>
<h3><a href="#">Ausstellung hinzufügen</a></h3>
<div>
<form name="ausstellung_add" action="index.php?section=events" method="post" accept-charset="utf-8">
<table>
<tr>
<td>Von:</td>
<td>
<select name="day_from">
<option value="0" class="bold italic">Tag</option>
<?php
for($i=1; $i<=31; $i++){
echo "<option value=\"".$i."\">".$i."</option>\n";
}
?>
</select>
<select name="month_from">
<option value="0" class="bold italic">Monat</option>
<?php
for($i=1; $i<=12; $i++){
echo "<option value=\"".$i."\">".$month_name[$i]."</option>\n";
}
?>
</select>
<select name="year_from">
<option value="0" class="bold italic">Jahr</option>
<?php
for($i=2008; $i<=$year; $i++){
echo "<option value=\"".$i."\">".$i."</option>\n";
}
?>
</select>
</td>
</tr>
<tr>
<td>Bis:</td>
<td>
<select name="day_till">
<option value="0" class="bold italic">Tag</option>
<?php
for($i=1; $i<=31; $i++){
echo "<option value=\"".$i."\">".$i."</option>\n";
}
?>
</select>
<select name="month_till">
<option value="0" class="bold italic">Monat</option>
<?php
for($i=1; $i<=12; $i++){
echo "<option value=\"".$i."\">".$month_name[$i]."</option>\n";
}
?>
</select>
<select name="year_till">
<option value="0" class="bold italic">Jahr</option>
<?php
for($i=2008; $i<=$year; $i++){
echo "<option value=\"".$i."\">".$i."</option>\n";
}
?>
</select>
</td>
</tr>
<tr>
<td>Thema:</td>
<td><input type="text" name="name_topic" id="name_topic" /></td>
</tr>
<tr>
<td>Aussteller:</td>
<td><input type="text" name="name_contributer" id="name_contributer" /></td>
</tr>
<tr>
<td>Eintritt:</td>
<td><input type="text" name="name_entrance" id="name_entrance" /></td>
</tr>
</table>
<br />
<div id="add_ausstellung">
<input type="submit" id="small" class="ausstellung_submit" name="ausstellung_submit" value="speichern">
</div>
</form>
</div>
<h3><a href="#">Termine bearbeiten/löschen</a></h3>
<div>
<button id="edit_event">Termin bearbeiten</button>
</div>
Upvotes: 0
Views: 553
Reputation: 6406
Add an <input type="hidden">
to each form
with name="submit_X"
. The input type="submit"
element seems to be ignored by serialize();
.
Example:
<input type="text" id="year_from" name="year_from" />
<!-- More inputs.... -->
<input type="hidden" name="submit_1" value="true" />
You could also use JavaScript to add another key to the POST data.
Take a look at this fiddle: http://jsfiddle.net/cd7Yy/
Upvotes: 1
Reputation: 4511
You need to show your HTML too.
But from what I see, if you use OnClick event on buttons in your forms, you can create buttons in your HTML with different names for different forms, and then, in your PHP cheack which name you recieve in $_POST
Upvotes: 0