簡體   English   中英

jQuery發布PHP發送表單數據

[英]jquery post php send form data

我正在嘗試通過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); }); } }); } 
 $(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; } }); }); 
 <!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> 

要發送數據,您必須執行以下操作:

錯誤代碼:

var datastring ='txtDate ='+ txtDate +'&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                       
    }
});

您不應該將參數中的數據發布為參數。 您應該使用數據來映射要發送的數據字段:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM