First fill out an application when submitting a form

I am using the PHP CodeIgniter framework.

The problem is that I want the user to check the fields that they entered and approved by selecting the checkbox and then submitting the form.

Like these fields.

<tr>    
    <td>Remarks </td>
    <td><textarea name="particular" rows="3" class="save_record autosuggestship"></textarea></td>   
    <td>&nbsp;</td>
    <td>&nbsp;</td>                 
</tr>
<tr>
    <td>Custom Reference No.</td>
    <td><input type = 'text' name = 'custom_ref_no' class = 'save_record autosuggestship' id="custom_ref_no" /></td>
    <td>Date</td>
    <td><input name = 'costom_date' type = 'text' class = 'save_record date_class autosuggestship' id="costom_date" /></td>
</tr>
<tr>
    <td>
        <p> Container Information </p>
    </td>
    <td>

And this is a flag to approve.

<tr>
    <td>Approve</td>
    <td><input type="checkbox" id="Approve" /></td>
    <td>&nbsp;</td>
    <td align="left">&nbsp;</td>
</tr>
+5
source share
3 answers

Uses jQuery: try pasting this on your page wrapped in tags <script></script>and including jQuery.

$(document).ready(function() {
    $('form input[type=submit]').on('click', function(e) {
      e.preventDefault();
      if($('#Approve').attr('checked') == 'checked') {
        $('form').submit();
      }
      else {
        alert('You must check the Approve checkbox to continue!');
      }
    });
    // if the user presses enter.
    $('form').on('keyup', function(e) {
      if(e.which == 13) {
       $('form').submit();
      }
    });
});

In PHP (make sure you provide your input name="Approve":

if(isset($_POST['Approve']) {
  // they have ticked the checkbox.
}
+1
source

How to do it with jQuery:

, js, - , ( ), , .

Useless requests HTTP- (form submit), , , .

$(document).ready(function() {

    // Catch submit events:
    $('form').submit(function() {
        // Check if checkbox is checked:
        if ($('#checkme').is(':checked')) {
            // Allow form submit:
            return true; 
        } else {
            // Prevent form submit:
            alert('You must check the Approve checkbox to continue!');
            return false;
        }
    });

});

. <form> , PHP.

, tomhallam, tomhallam, , , , jQuery.

PHP:

, PHP . , , Javascript , .

<?php
/* POST($key) returns value of $_POST($key) if set, 
 *  otherwise empty string is returned. */
function POST( $key ) {
    if ( isset( $_POST[$key] )) return $_POST[$key];
    else return '';
}

if ( isset( $_POST['checkme'] )) {
    // Checkme is checked, do something with form data:
    mySql_insert_formdata( $_POST );
    $_POST['error'] = 'Form processed, thanks for your data';
} elseif ( isset($_POST['submitted'])) {
    // Form submitted but checkme not checked, set error message
    $_POST['error'] = 'Checkme must be cheked first!';
}

echo POST('error');
?>

<form action="forms.php" method="post">
    <!-- Hidden field to check at server if form is submitted -->
    <input type="hidden" name="submitted" value="anything" />
    <!-- Some fields for user to fill, POST() inserts previous value if any -->
    <label for="email">eMail</label>
    <input type="email" id="email" name="email" value="<?php echo POST('email'); ?>" /><br/>
    <!-- Checkbox that must be checked -->
    <input type="checkbox" id="checkme" name="checkme" />
    <label for="checkme">Approve</label><br/>
    <!-- And submit button -->
    <input type="submit" value="Submit" />
</form>

:

Javascript ( ) , javascript.
PHP ( ) , / .

, , , , Javascript, .
JS, , , , "try again".

+1

use the onsubmit form event handler

<form onsubmit="return this.Approve.checked?true:alert('must approve!'),false">
<table>
<tr>
    <td>Approve</td>
    <td><input type="checkbox" id="Approve" /></td>
    <td>&nbsp;</td>
    <td align="left">&nbsp;</td>
</tr>
</table>
<input type="submit"/>
</form>

instead of the built-in code, you can use the function, do not forget to leave the built-in "return myfunc ()", because the presentation of the form will depend on this return, if true, the form is submitted differently, it just waits.

0
source

All Articles