使用ajax发送特殊字符并将其正确接收到php [英] send special characters with ajax and receive them correctly to php

查看:249
本文介绍了使用ajax发送特殊字符并将其正确接收到php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个javascript函数,它获得一些参数的数据,并尝试将它们保存在postgreSQL数据库。这是javascript ajax函数

i've got a javascript function which get some datas on parameter and try to save them on a postgreSQL database. This is the javascript ajax function

function insertCalendarEvents(calendar_group, event_name, event_datestart, event_datestop, event_timestart, event_timestop, event_info, onfinish) {
    var request;
    if(window.XMLHttpRequest)
        request = new XMLHttpRequest();
    else
        request = new ActiveXObject("Microsoft.XMLHTTP");

    request.onreadystatechange = function() {
        if (request.readyState == 4 && request.status == 200) {alert(request.responseText);
            if(request.responseText.substr(0, 6) == "error ")
                alert(errorName[request.responseText.substr(6)]);
            else {
                var event_id = 7;
                onfinish(event_id);
            }
        }
    }

    var params = "action=insertCalendarEvents&calendar_group=" + calendar_group + "&event_name=" + encodeURIComponent(event_name) + "&event_datestart=" + event_datestart + "&event_datestop=" + event_datestop + "&event_timestart=" + event_timestart + "&event_timestop=" + event_timestop + "&event_info=" + event_info;
    request.open("GET", "php/calendar.php?" + params, true);
    request.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
    request.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
    request.send();
}

这是php函数:

if($action == "insertCalendarEvents") {
$calendar_group = $_GET["calendar_group"];
    $event_name = "'" . htmlspecialchars(urldecode($_GET["event_name"])) . "'";
    $event_datestart = "'" . $_GET["event_datestart"] . "'";
    $event_datestop = "'" . $_GET["event_datestop"] . "'";
    $event_timestart = $_GET["event_timestart"] != "" ? "'" . $_GET["event_timestart"] . "'" : "null";
    $event_timestop = $_GET["event_timestop"] != "" ? "'" . $_GET["event_timestop"] . "'" : "null";
    $event_info = "'" . $_GET["event_info"] . "'";
    echo $event_name;

    require_once("connect.php");
$query = "INSERT INTO calendar_events (calendar_group, event_name, event_datestart, event_datestop, event_timestart, event_timestop, event_info) VALUES (" . $calendar_group . ", " . $event_name . ", " . $event_datestart . ", " . $event_datestop . ", " . $event_timestart . ", " . $event_timestop . ", " . $event_info . ")";
$result = pg_query($connect, $query);
if(!$result)
    die("error 1"); // query error

    $query = "SELECT currval('events_event_id_seq')";
    $result = pg_query($connect, $query);
    if(!$result)
    die("error 1"); // query error

    $row = pg_fetch_row($result);
    echo $row[0];
}

问题是当我尝试添加特殊字符event_name参数)像+或换行符,所以,它不工作,在+它替换空间,换行不做任何事情。

Problem is when i try to add special chars (right now i test only on event_name parameter) like + or newline and so, it doesnt work, on + it replaces it with space, newline doesnt do anything.

推荐答案

p>在您发送到服务器之前编码数据

Encode Data Before You send to server

我也有像你现在的问题,但这个功能我实现解决我的问题

I also had problem like you have now but this function i implemented solved my problem

 function encode(val){
        var eVal;
        if(!encodeURIComponent){
            eVal=escape(val);
            eVal=eVal.replace(/@/g,"%40");
            eVal=eVal.replace(/\//g,"%2F");
            eVal=eVal.replace(/\+/g,"%2B");
            eVal=eVal.replace(/'/g,"%60");
            eVal=eVal.replace(/"/g,"%22");
            eVal=eVal.replace(/`/g,"%27");
            eVal=eVal.replace(/&/g,"%26");
        }else{
            eVal=encodeURIComponent(val);
            eVal=eVal.replace(/~/g,"%7E");
            eVal=eVal.replace(/!/g,"%21");
            eVal=eVal.replace(/\(/g,"%28");
            eVal=eVal.replace(/\)/g,"%29");
            eVal=eVal.replace(/'/g,"%27");
            eVal=eVal.replace(/"/g,"%22");
            eVal=eVal.replace(/`/g,"%27");
            eVal=eVal.replace(/&/g,"%26");
        }
        return eVal.replace(/\%20/g,"+");
    }

这篇关于使用ajax发送特殊字符并将其正确接收到php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆