1

I'm trying to submit a form with jquery from plugin With multiple submits, but i can't get the value from the $_POST['aggInf'] in php, i can get it in js.

My form:

<form action="#" method="post" id="gestioneProfilo">

My inputs:

<input name="aggInf" type="submit" value="Upload" class="upload"> <input name="aggInfAnn" type="submit" value="Annulla" id="cancel">

If i try onsubmit='alert($(this).serialize()); return false;' on the from i always get this in return:

alert error

On the js side i used this solution and it works fine, if i try a console.log() command i get the correct value. If i try a var_dump() command i only get all the other input and textboxes values. How do i fix this?

Full form html:

    <form action="#" method="post" id="gestioneProfilo">

        <!-- Scelta avatar -->
        <div id="sceltaAvatar">

            <!-- Profilo -->
            <div id="perAvatar">
                <h1>Scelta dell'Avatar</h1>
                <p>Carica dal tuo pc o dal web una tua immagine di profilo</p>
                <!-- <input type="hidden" name="immP"> -->
            </div>

            <!-- Copertina -->
            <div id="perCopertina">
                <h1>Scelta della copertina</h1>
                <p>Carica dal tuo pc o dal web una tua immagine di copertina</p>
                <!-- <input type="hidden" name="immC"> -->
            </div>

            <!-- Info generali -->
            <div id="altreInfo">
                <div>

                    <!-- informazioni -->
                    <h1>Informazioni</h1>
                    <textarea placeholder="Cosa fai nella vita? Scrivi una breve descrizione su di te" name="bio" id="bio"><?php get_stuff('bio', TRUE, TRUE) ?></textarea>
                </div>
                <div id="due">
                    <div>

                        <!-- Sito -->
                        <h1>Sito</h1>
                        <textarea placeholder="Il tuo sito" name="sito" id="sito"><?php get_stuff('sito', TRUE, TRUE) ?></textarea>
                    </div>
                    <div>

                        <!-- Posizione -->
                        <h1>Posizione</h1>
                        <textarea placeholder="Da dove vieni?" name="pos" id="pos"><?php get_stuff('pos', TRUE, TRUE) ?></textarea>
                    </div>
                </div>
            </div>

            <!-- Torna indietro -->
            <input name="aggInf" type="submit" value="Upload" class="upload">
            <input name="aggInfAnn" type="submit" value="Annulla" id="cancel">
        </div>
    </form>
    <?php include_once 'lightbox.php' ?>

js:

$('#gestioneProfilo').submit(function(e) {
    e.preventDefault();

    var
        val = $("input[type=submit][clicked=true]").val(),
        pop = $(this);

    console.log(val);
    // it works, if i press upload it prints upload, if i press annulla it prints annulla

    if (val == "Upload") {
        $(this).ajaxSubmit({
            type: 'POST',
            url: "lib/ajax.php",
            dataType: "json",
            data: pop.serialize(),
            success: function(data){
                //stuff
            }
        });
    }else{
        cambiaA("#perCommentare", "#gestioneProfilo", "ritorna");
    };
});

$(document).on('click', '#gestioneProfilo input[type=submit]', function() {
    $("input[type=submit]",
    $(this).parents("#gestioneProfilo")).removeAttr("clicked");
    $(this).attr("clicked", "true");
});

php:

if (isset($_POST['aggInf'])) {

    update_info();

    $return_data['immP'] = get_stuff('immP');
    $return_data['immC'] = get_stuff('immC');
    $return_data['bio']  = get_stuff('bio');
    $return_data['sito'] = get_stuff('sito', FALSE, TRUE);
    $return_data['pos']  = get_stuff('pos');
    echo json_encode($return_data);
    exit;
}
9
  • 1
    Show your full HTML please. Especially the full <form> tag. Jan 5, 2016 at 15:24
  • 2
    probably because the submit doesn't hold the name attribute for it. Error reporting php.net/manual/en/function.error-reporting.php will tell you if it is or not. <input name="aggInfAnn" type="submit" value="Annulla" id="cancel"> should read as <input name="aggInfAnn" type="submit" value="Annulla" id="cancel" name="submit"> or whatever input it's supposed to be (only you know that). In a nutshell, there is no name="submit" anywhere in your "posted" code. Jan 5, 2016 at 15:25
  • And also, it is better to use <input type="hidden"> instead. Jan 5, 2016 at 15:26
  • That's not how $_POST works in PHP - ideally you need to supply a PHP script to the forms action event and the elements need to have a name value for the post method to pick up the values. Jan 5, 2016 at 15:27
  • "but i can't get the value from the $_POST['submit'] in php" - there is nothing in your (now edited) question to support your question. Jan 5, 2016 at 15:34

0

Your Answer

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

Browse other questions tagged or ask your own question.