2012-01-27 63 views
0

多次使用此脚本将1或2个值传递给PHP文件,效果很棒。现在使用它来传递7个值,只有前两个值得通过。通过AJAX将表格数据发送到PHP

$(document).ready(function(){ 
$("form#send_note").submit(function() { 
var to_user = $('#to_user').attr('value'); 
var from_user = $('#from_user').attr('value'); 
var defaultpic_from = $('#defaultpic_from').attr('value'); 
var defaultpic_to = $('#defaultpic_to').attr('value'); 
var your_username = $('#your_username').attr('value'); 
var message_title = $('#message_title').attr('value'); 
var message_contents = $('#message_contents').attr('value'); 
    $.ajax({ 
     type: "POST", 
     url: "../inbox/send_note.php", 
     data: "to_user="+ to_user +"& from_user="+ from_user + "& defaultpic_from="+ defaultpic_from + "& defaultpic_to="+ defaultpic_to + "& your_username="+ your_username + "& message_title="+ message_title + "& message_contents=" + message_contents, 
     success: function(){ 
      $('form#send_note').hide(function(){$('div.success2').fadeIn();}); 

     } 
    }); 
return false; 
}); 
}); 

我有双重检查所有的名字的,都是为了之后FROM_USER(defaultpic_from)只是值等不会通过。 我相信这是我列出“数据:”的方式。当谈到JavaScript时,我是一个完整的新手,所以任何关于如何让这些通过的建议都是太棒了!

回答

0

我敢打赌,您的默认图片的值正在终止查询字符串。你可以用你的增值经销商像这样以确保他们正确地转义:

$(document).ready(function(){ 
    $("form#send_note").submit(function() { 
    var to_user = encodeURIComponent($('#to_user').attr('value')); 
    var from_user = encodeURIComponent($('#from_user').attr('value')); 
    var defaultpic_from = encodeURIComponent($('#defaultpic_from').attr('value')); 
    var defaultpic_to = encodeURIComponent($('#defaultpic_to').attr('value')); 
    var your_username = encodeURIComponent($('#your_username').attr('value')); 
    var message_title = encodeURIComponent($('#message_title').attr('value')); 
    var message_contents = encodeURIComponent($('#message_contents').attr('value')); 
     $.ajax({ 
      type: "POST", 
      url: "../inbox/send_note.php", 
      data: {to_user:to_user, from_user:from_user, defaultpic_from: defaultpic_from, defaultpic_to:defaultpic_to, your_username:your_username, message_title:message_title, message_contents:message_contents}, 
      success: function(){ 
       $('form#send_note').hide(function(){$('div.success2').fadeIn();}); 

      } 
     }); 
    return false; 
    }); 
    }); 

您需要urldecode在PHP POST变量,但应该这样做。

+0

尝试这种现在 – user1053263 2012-01-27 01:29:56

+0

也,科学家是正确的约.VAL()。尽管您的方法可行,但图书馆更适合使用预期的功能。如果这仍然失败,请尝试将所有这些&更改为仅在&与变量之间没有空白处。 – 2012-01-27 01:33:16

+0

DARN同样的事情。在我的PHP文件中,我有_POST变量通过这个函数,我应该删除它还是添加一个不同的函数,现在我们添加了urldecode? $ to_user = htmlspecialchars(trim($ _ POST ['to_user'])); – user1053263 2012-01-27 01:34:24

1

1)你知道与jquery你可以做到这一点,对吧?

var defaultpic_from = $('#defaultpic_from').val(); 

2)同时,你不需要打开&成一个实体,但如前所述,你应该在

3)你是否核实了所有的变量实际上有值使用encodeURIComponent方法ajax请求之前的值?当你看POST时会发生什么?你是否在POST中获得成员,但没有值?或没有键和没有值?

4)尝试使用Chrome的网络选项卡中的开发工具来检查请求和响应

这里是我现在使用的例子,其中params为NVP串和同步是真正

var callService = function(sync, params, successCB, errorCB){ 
    console.log('ENTER callService'); 
    $.ajax({ 
    type  : 'POST', 
    url  : 'required/core/Service.php', 
    async  : sync, 
    data  : params, 
    dataType : 'json', 
    success : function(data){ 
     console.log('ENTER POST SUCCESS'); 
     successCB(data); 
    }, 
    error  : function(){ 
     console.log('ENTER POST ERROR'); 
     errorCB(); 
    } 
    }); 
}; 

如果您可以进入请求和响应标题并向我们显示,那么真正有用的是什么。你可以有PHP回声

echo json_encode($_POST); 

,使其更容易得到响应

+0

Unfortunatley我知道javascript很少,只是对它的一个非常基本的理解。我通过没有jQuery的php传递值,并通过正确的回应。我打算玩这个encodeURIComponent函数,看看我能做什么。非常感谢您的回复! – user1053263 2012-01-27 01:41:08

+0

并通过respoonse,我的意思是更容易调试,说在Chrome的网络选项卡 – thescientist 2012-01-27 01:46:11

+0

我已经有一段时间了铬,从来没有注意到网络选项卡。这使生活更轻松。谢谢你,先生。那么数据正在发送。另外,当我通过ajax并进入php文件,值经历很好。 – user1053263 2012-01-27 01:53:53