-7

I am new here. I want my code to submit form without submit button. My code run like this: Every time when user input data in input text and press enter, the result will be display on the same page just below input text. This code will work perfectly is I use submit button to submit data. Can anyone help me with this ? Thanks in advance.

This is my result.html

$(document).ready(function(){
$("#code").change(function(){
    var st = '';
    $('#Myform input[type=text]').each(function(){

        st = st+ '<td>'+$(this).val()+'</td>';
        $(this).val('');
    });
    $('#result').append('<tr>'+st+'</tr>');
});

});

html

<form id="Myform">
    <table>
        <tr>
            <td>
                <p>Code:</p>
            </td>
            <td><input id="code" type="text" name="code" size="40" autofocus/></td>
        </tr>
    </table>
</form>
<table id="result"></table>

This is my code.php

<?php if( $_REQUEST["code"] ) {$code = $_REQUEST['code'];}?>
guradio
  • 15,524
  • 4
  • 36
  • 57
Jen
  • 1
  • 2
  • 4
    use `$('#formid').submit()` – guradio May 04 '16 at 10:39
  • 1
    Possible duplicate of [Submit a form using jQuery](http://stackoverflow.com/questions/1200266/submit-a-form-using-jquery) – evolutionxbox May 04 '16 at 10:41
  • If you press enter while the input is focused, default behaviour is already to submit the form. It is not clear what is your issue/expected behaviour?... – A. Wolff May 04 '16 at 10:51
  • @A. Wolff Let say i add an input submit button to submit the form. . When user enter "123" in the input text and click on the submit button, it will print "123". Next when the user enter "456" and click the submit button, it will print "456" in the next line below "123". But if the user enter "789" and hit the ENTER key, all the printed result will be gone and the url will add "?code=" behind it. So how can i solve it ? I want the user submit and print each result without submit button. This is the issue that I am facing. Thanks – Jen May 05 '16 at 02:02
  • Anyone know why it behave like this ? I want to develop a barcode scanning system that implement this concept using php. – Jen May 05 '16 at 02:05
  • @guradio I have tried using $('#formid').submit(), but still the same. – Jen May 05 '16 at 02:37
  • @Jen you should use `$('#Myform').submit()` since `Myform` is the ID of your form – guradio May 05 '16 at 02:39
  • @guradio Yes I already change it to $('#Myform').submit() but it is still the same – Jen May 05 '16 at 02:52

2 Answers2

0

A cursory Google search would have brought up that jQuery provides a means of doing this.

You can trigger form submission via either the submit() or trigger() methods, i.e.

$('#Myform').submit()

or

$('#Myform').trigger('submit')

The former is merely a shortcut method to the latter.

Mitya
  • 33,629
  • 9
  • 60
  • 107
0

how are you?

If you need to send to another php page, you can to send AJAX, if don't want to send, you can show data directly to your js, without ajax.

https://jsfiddle.net/5L9Leso8/

UPDATE:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>teste</title>
</head>

<body>
    <div>
        <label for="test">
            <input type="text" name="test" id="test">
            <p id="rest"></p>
        </label>
    </div>
    <script>
    $('#test').on('change', function(e) {
        e.preventDefault();
        var self = $(this);

        /** if you don't need to send to your php code 
        $('#rest').html(self.val());
        **/
        if (self.val().length > 0) {
            $.ajax({
                url: 'yourUrlphp',
                type: 'post',
                data: {
                    yourText: self.val()
                },
                success: function(data) {
                    $('#rest').html(data);
                }
            });
        }
    });

    </script>
</body>

</html>