jquery发布php发送表单数据

时间:2014-09-18 13:27:45

标签: javascript php jquery

我试图通过jquery将变量从2个表单元素发送到另一个PHP脚本。 不确定我做错了什么,只是无法将变量发送到其他PHP脚本。 尝试使用jquery序列化,也没有得到它。 尝试使用serialize()命令。 任何建议都超过欢迎。

谢谢!



When putting the data in url manually, the return is correct. Then in data I have +page, as:
		    $.ajax
                ({
                    type: "GET",
                    url: "agent_talktime_pag.load.php?txtDate=2007-03-04&zone=Hongkong",
                    data: "page="+page,
                    success: function(msg)

If I use " var datastring  = $('#idForm').serialize(); " the return doesn't work.
The variables get passed in the url, but the issue seems that the page="+page isn't passed. Which makes the load script not work. I tried to add a hidden field in the form like:
<input type="hidden" name="page" value="+page">
But, seems it's not passed as expected by the load script. The +page becomes just a string, think in the $.ajax it functions as a counter?

Any ideas?


 $(document).ready(function(){
		    $("#driver").click(function(){
		  var datastring  = $('#testform').serialize(); 
		    
                function loading_show(){
                    $('#loading').html("<img src='../images/loading.gif'/>").fadeIn('fast');
                }
                function loading_hide(){
                    $('#loading').fadeOut('fast');
                }             
		

		   
                function loadData(page){
                    loading_show();                   
		    $.ajax
                    ({
                        type: "GET",
                        url: "agent_talktime_pag.load.php?txtDate=2007-03-04&zone=Hongkong",
                        data: "page="+page,
                        success: function(msg)
                        {
                            $("#container").ajaxComplete(function(event, request, settings)
                            {
                                loading_hide();
                                $("#container").html(msg);
                            });
                        }
                    });
                }
		
&#13;
$(document).ready(function(){
		    $("#driver").click(function(){
		    var txtDate=$("#txtDate").val();
		    var zone=$("#zone").val();
    var dataString = 'txtDate='+ txtDate + '&zone='zone;
    });
		    
                function loading_show(){
                    $('#loading').html("<img src='../images/loading.gif'/>").fadeIn('fast');
                }
                function loading_hide(){
                    $('#loading').fadeOut('fast');
                }             
		

		   
                function loadData(page){
                    loading_show();                   
		    $.ajax ({
         type: "GET",
                        url: "agent_talktime_pag.load.php",
                        data: dataString,
                        success: function(msg)
                        {
                            $("#container").ajaxComplete(function(event, request, settings)
                            {
                                loading_hide();
                                $("#container").html(msg);
                            });
                        }
                    });
                }
		
		
		
                loadData(1);  // For first time page load default results
                $('#container .pagination li.active').live('click',function(){
                    var page = $(this).attr('p');
                    loadData(page);
                    
                });           
                $('#go_btn').live('click',function(){
                    var page = parseInt($('.goto').val());
                    var no_of_pages = parseInt($('.total').attr('a'));
                    if(page != 0 && page <= no_of_pages){
                        loadData(page);
                    }else{
                        alert('Enter a PAGE between 1 and '+no_of_pages);
                        $('.goto').val("").focus();
                        return false;
                    }
                    
                });
            });
&#13;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <!-- This is a pagination script using Jquery, Ajax and PHP
     The enhancements done in this script pagination with first,last, previous, next buttons -->

    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>Pagination with Jquery, Ajax, PHP</title>
        <script type="text/javascript" src="jquery/external/jquery/jquery.js"></script>
        <script type="text/javascript" src="jquery/jquery-ui.min.js"></script>
        <link rel="stylesheet" href="includes/jquery/jquery-ui.css">
        <link type="text/css" rel="stylesheet" href="style.css">
        <script type="text/javascript"  src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
            
    </head>
    <body>

    <div align="center" style="font-size:24px;color:#cc0000;font-weight:bold">Pagination with jquery, Ajax and PHP</div>

    <form id="testform">
    <input type="text" name="txtDate" id="txtDate">
    <select id="zone" name="zone">
        <option value="Hongkong">Hongkong</option>
        <option value="EST">EST</option>
    </select>
    <input type="button" id="driver" name="submit" value="Search"/>
    </form>

        <div id="loading"></div>
        <div id="container">
            <div class="data"></div>
            <div class="pagination"></div>
        </div>
		
    </body>
    </html>
&#13;
&#13;
&#13;  

2 个答案:

答案 0 :(得分:0)

要发送您必须执行的数据,请执行以下操作:

错误代码:

  

var datastring ='txtDate ='+ txtDate +'&amp; zone ='zone;

代码:     //手动代码

 var datastring  = {"name field" txtDate, "fieldname": zone} // zone = field values

否则:

 var datastring  = $('#idForm').serialize(); 

代码ajax:

$.ajax({
    data:  datastring, //date form 
    url:   'url',
    type:  'get', //type
    dataType: "html",
    beforeSend: function () {
      //code load
    },
    success:  function (response) 
    {
      //code                       
    }
});

答案 1 :(得分:0)

您不应该在网址中将数据发布为params。您应该使用数据来绘制您将要发送的数据字段:

type: "method",
data: {field1: field1, field2: field2}