0

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>

2 Answers 2

1

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/

Sign up to request clarification or add additional context in comments.

9 Comments

sry don't know why I should hide the buttons. They must be clickable visible for the user...
you don't have to hide the buttons. but if i unterstood you correctly, you want to know which form has been submitted. so add an extra input to each form with submit_X as name. I updated my answer after checking your updated HTML code.
Ok now I see what you did there ;) Really good idea. But it doesn't solve my problem. The JavaScript code always jumps to "else $("#dialog_add_event_error").dialog( "open" );" Remember if I change the click function to submit everything works fine... any explanation? I made this out of a tutorial. I believe reqCode is never set to 1 cause reqCode delivers my whole new Site...
Yeah sorry, I first thought you removed your submit-buttons and replaced them with normal buttons or something. Wasn't really clear without the HTML code. However: Maybe there is an error in your PHP-Script so it doesn't return '1'. Did you check that? But... Then it wouldn't work with normal submits too. Strange...
Yeah I'll take a look at it now. Coded to much for today but yeah why should it be true if there is never a return value of 1 :) Report follows in a few minutes. It works with normal submit but doesn't show the dialog_add_event. Just recognized it now.
|
0

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

1 Comment

This is just what I wish to do. My Problem is that the name from the submit formular is not set in $_POST and I believe this is because of the click function in JavaScript. PS: I have added the html/template file in my first post :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.