Ciao a tutti, stavo provando un semplice script per inviare, tramite metodo ajax-php, dei dati POST da una textarea al database. Il problema non è tanto il collegamento al db ma la scrittura del dato post nel database, è l'unico dato che non mi salva.
Posto il codice, questo è l'html
codice:
<html>
<head>
<script type="text/javascript" src="textarea.js"></script>
</head>
<body>
<form method="post" action="invia.php" name="MyForm">
<textarea cols="30" rows="4" name="message_text"></textarea>
invia
</form>
</body>
</html>
questo è il js
codice:
function createXmlHttpRequestObject()
{
var xmlHttp;
try
{
xmlHttp = new XMLHttpRequest();
}
catch(e)
{
var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
"MSXML2.XMLHTTP.5.0",
"MSXML2.XMLHTTP.4.0",
"MSXML2.XMLHTTP.3.0",
"MSXML2.XMLHTTP",
"Microsoft.XMLHTTP");
for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++)
{
try
{
xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
}
catch (e) {}
}
}
if (!xmlHttp)
alert("Error creating the XMLHttpRequest object.");
else
return xmlHttp;
}
function process()
{
if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
{
params = document.getElementsByTagName("message_text").value;
xmlHttp.open("POST", "invia.php", true);
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlHttp.onreadystatechange = handleServerResponse;
xmlHttp.send(params);
}
else
setTimeout('process()', 1000);
}
function handleServerResponse()
{
if (xmlHttp.readyState == 4)
{
if (xmlHttp.status == 200)
{
//DO NOTHING
}
else
{
alert("There was a problem accessing the server: " + xmlHttp.statusText);
}
}
}
questo è il php
codice:
<?php
require("db_config.php");
$message = pg_escape_string($_POST["message_text"]);
$dayOfWeek = date("l - d-m-Y - H:i:s");
$query = "INSERT INTO messages (content, created)
VALUES ('" . $message . "', '" . $dayOfWeek . "')";
pg_query($query);
?>
provando mi scrive correttamente il valore $dayOfWeek alla click sul link "invia", segno che il collegamento c'è, il problema sembra proprio come passare il dato POST, poichè sul db mi ritorna un valore nullo. Qualcuno ha qualche idea?
Grazie in anticipo