user1757383
user1757383

Reputation: 91

Submit form if action == something

I would like to submit a <form> automatically if:

if(isset($_POST['action']) && ($_POST['action'] =='confirmado')){

submit form.

I don´t know if I should use a javascript script. I try doing this but it is not working.

<script>

    if(isset($_POST['action']) && ($_POST['action'] =='confirmado')){
    $("#form").submit(function(){
    document.form.submit(); 
    return false;
    }

</script>

Upvotes: 0

Views: 249

Answers (4)

fimas
fimas

Reputation: 568

If i understand your code right, you would like to only submit the form if the field "action" equals "confirmando". Since you are allready using jQuery let's keep at it.

JavaScript:

<script>
        $('document').ready(function() {
            $('input[name=action]').change(function() {
                if($(this).val() == 'confirmando') {
                    $('form').submit(); // If you would like to do a normal sumbit
                    $.post('my_php_file.php', $('form').serialize(), function(data)) { // or use AJAX
                        /* var data now contains the output of the PHP file  */
                    }
                }
            });
        });
    </script>

It still makes sense to check the submited form in PHP when you receive the data from the form.

Upvotes: 0

jpatiaga
jpatiaga

Reputation: 352

First of all, I agree with coder1984: you are mixing PHP with Javascript, and you are trying to access POST data in the client side, which is not possible. If I can guess what you are trying to do, maybe this code will help you:

<script type="text/javascript">
if (<?php echo (isset($_POST['action']) && ($_POST['action'] =='confirmado')) ? '1' : '0'; ?>) {
  document.form.submit();
}
</script>

Upvotes: 0

Šime Vidas
Šime Vidas

Reputation: 185913

Try this:

$( '#form' ).submit(function () {
    if ( this.action !== 'confirmado' ) return false;
});

Upvotes: 1

Teena Thomas
Teena Thomas

Reputation: 5239

You are trying to mix up PHP, which is a server-side language, with Javascript, which is a client-side language. So, that won't work.

Moreover, the submission of a <form> is NOT captured in its $_POST array as $POST['action']. The method of submission is POST and the $_POST array contains data submitted via the form's html elements.

Upvotes: 3

Related Questions