doucitan2544 2016-10-07 13:46
浏览 11

发布标志和另一个PHP文件

How do i post a string with sign '&' to a php file.

I have a jscript:

function saveRow(oTable, nRow) {
    var jqInputs = $('input', nRow);
    oTable.fnUpdate(jqInputs[0].value, nRow, 0, false);
    oTable.fnUpdate(jqInputs[1].value, nRow, 1, false);
    oTable.fnUpdate(jqInputs[2].value, nRow, 2, false);
    oTable.fnUpdate(jqInputs[3].value, nRow, 3, false);
    oTable.fnUpdate('<a class="edit" href="">Edit</a>', nRow, 4, false);
    oTable.fnUpdate('<a class="delete" href="">Delete</a>', nRow, 5, false);
    oTable.fnDraw();
    var val1 = jqInputs[0].value;
    var val2 = jqInputs[1].value;
    var val3 = jqInputs[2].value;
    var val4 = jqInputs[3].value;

    var dataString = 'pairchannels=1' + '&eventname=' + val1 + '&datetime=' + val2 + '&pairedchannel=' + val3 + '&realchannel=' + val4;
    if (val1 == '' || val2 == '' || val3 == '' || val4 == '') {
        alert(jqInputs[1].value);
    } else {
        alert(val1);

        $.ajax({
            type: "POST",
            url: "process.php?",
            data: dataString,
            cache: false,
            success: function(result) {
                alert(dataString);
            }
        });
    }
    return false;
}

Which sends a POST to process.php:

 ...
 else if(isset($_POST['pairchannels'])){
     $this->procPairChannels();
  }
...

function procPairChannels(){
  global $session, $form;
  /* Account edit attempt */
  $retval = $session->procPairChannels($_POST['eventname'], $_POST['datetime'], $_POST['pairedchannel'], $_POST['realchannel']);   

}

But instead of whole string in $_POST['eventname'] which is "Bosnia & Herzegovina" php splits the string at "&". This is the first time error like this occurred because i have never had a "&" in any of my strings until now.

What should i do to handle this kind of strings?

  • 写回答

1条回答 默认 最新

  • doulanyan6455 2016-10-07 13:48
    关注

    You need to encode the values you are sending in your datastring correctly so that they won't be interpreted as special characters in a url (a & separates key-value pairs...):

    The easiest way to do that, is to let jQuery handle that by sending an object instead of a string:

    // Maybe a name change to dataObject...
    var dataString = {
      pairchannels: 1,
      eventname: val1,
      // etc.
    };
    

    If you want to build the query-string yourself you need to use encodeURIComponent() on each individual value.

    评论

报告相同问题?

悬赏问题

  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果