简体   繁体   English

PHP-提交按钮,无需刷新即可获得价值

[英]PHP - Submit button and get value without refresh

Hello I want to get the value of this input and fetch it using ajax no database at all. 您好,我想获取此输入的值,并使用ajax no database来获取它。 thank you. 谢谢。 how can i do it with ajax? 我怎么用ajax做到这一点?

<form method="POST">
   <input type="text" name="input" id="card-code" value='<?php echo $code ?>' class="form-control">

   <input type="text" id="card-pin" value='<?php echo $code2 ?>' class="form-control" maxlength="3">
 </form>

there is my inputs and here is the button. 有我的输入,这里是按钮。

           <form action="top-up.php" method="POST"> 


                    </div>
                </div>

                <div class="col-md-6" style="margin-top: -160px">

                        <div class="caption">

                        <div class="jumbotron">

                        <textarea class="form-control text-center" id="scanned-QR" name="lblQrTxt" onchange = "change()"></textarea><br><br><br>

                           <input class="btn btn-primary btn-lg" type="submit" name="btnSubcode" value="PROCESS"></input>
                        </div>

                        </div>
                </div>
            </div>
             </form>

so the final output sould not refresh the page and the textarea value will be send to the textbox 因此最终输出不会刷新页面,并且textarea值将发送到文本框

The jQuery Form Plugin allows you to easily and unobtrusively upgrade HTML forms to use AJAX. jQuery表单插件允许您轻松,毫不费力地升级HTML表单以使用AJAX。 The main methods, ajaxForm and ajaxSubmit, gather information from the form element to determine how to manage the submit process. 主要方法ajaxForm和ajaxSubmit从form元素收集信息,以确定如何管理提交过程。

http://malsup.com/jquery/form/#getting-started http://malsup.com/jquery/form/#getting-started

 $(document).ready(function() { // bind 'myForm' and provide a simple callback function $('#myForm').ajaxForm(function() { alert("Thank you for your comment!"); }); }); 
 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script> <script src="http://malsup.github.com/jquery.form.js"></script> <form id="myForm" action="comment.php" method="post"> Name: <input type="text" name="name" /> Comment: <textarea name="comment"></textarea> <input type="submit" value="Submit Comment" /> </form> 

// prepare Options Object 
var options = { 
    target:     '#divToUpdate', 
    url:        'comment.php', 
    success:    function() { 
        alert('Thanks for your comment!'); 
    } 
}; 

// pass options to ajaxForm 
$('#myForm').ajaxForm(options);

Firstly, rewrite your html code as below: 首先,如下重写您的html代码:

<form id="form" action="top-up.php" method="POST"> 


                    </div>
                </div>

                <div class="col-md-6" style="margin-top: -160px">

                        <div class="caption">

                        <div class="jumbotron">

                        <textarea class="form-control text-center" id="scanned-QR" name="lblQrTxt"></textarea><br><br><br>

                           <input class="btn btn-primary btn-lg js-form-submit" type="submit"></input>
                        </div>

                        </div>
                </div>
            </div>
             </form>

Then, you can write JS something like this: 然后,您可以像这样编写JS:

$(document).on('click','.js-form-submit', function (e) {
    e.preventDefault();
    var formData = $('#form').serialize();
    var url = $('#form').attr('action');
    $.ajax({
        type: "POST",
        cache: false,
        url: url // Your php url here
        data : formData,
        dataType: "json",
        success: function(response) {
             //var obj = jQuery.parseJSON(response); if the dataType is not specified as json uncomment this
             // do what ever you want with the server response
        },
        error: function() {
          alert('error handling here');
        }
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM