パラメータでいくつかのデータを取得し、それらをpostgreSQLデータベースに保存しようとするjavascript関数があります。これはJavaScript ajax関数です
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];
}
問題は、+ または newline のような特殊な文字 (現在は event_name パラメーターでのみテスト) を追加しようとすると、機能しません。+ ではスペースに置き換えられ、改行は何もしません。