Hello.
I am using MYSQL to store articles. I wanted to present the articles in order by id, then let the user choose one of them and send the id of the article to another php page for processing, using ajax. To do so i have placed a simple form in the articles. like so:
$result = mysql_query("SELECT * FROM articles ORDER BY id DESC")
or die(mysql_error());
while($row = mysql_fetch_array($result)){
$articletid = $row["id"];
$category = $row["category"] . "<br/>";
$title = $row["title"] . "<br/>";
$date = strftime("%d/%m/%y", strtotime($row['date'])) . "<br/>";
$body = $row["body"] . "<br/>";
echo '<div id="connectComplaint"><font size="3">';
echo $complaintid;
echo "<b><font color=red size=3>" . $title . "</b></font>";
echo "<font size=2>Category:" . $category;
echo "<font size=2>An article by:<font color=blue><a href=$self?userprofile=$user>" . $user . " </font></a>from:<font color=green>" . $date . "</font><br>";
echo "<br>" . nl2br($body);
echo '<br><form id="processrequest" action="" method="post">
<input type="hidden" id="complaintid" name="complaintid" value="'. $articletid .'">
<span style="color=blue; text-decoration:underline; cursor:hand;" onclick="javascript:makePOSTReques(this.parentNode);">choose this article!</span>
</form>';
echo "<br><hr><br></div>";
}
So you see, each article is presented in its turn with the submitting form attached to it, and the idea is that the form sends the ID of the article the user chooses.
I used this ajax script which i found on the net:
var http_request = false;
function makePOSTRequest(url, parameters) {
http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
// set type accordingly to anticipated content type
//http_request.overrideMimeType('text/xml');
http_request.overrideMimeType('text/html');
}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
alert('Cannot create XMLHTTP instance');
return false;
}
http_request.onreadystatechange = alertContents;
http_request.open('POST', url, true);
http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded charset=utf-8;" );
http_request.setRequestHeader("Content-length", parameters.length);
http_request.setRequestHeader("Connection", "close");
http_request.send(parameters);
}
function alertContents() {
if (http_request.readyState == 4) {
if (http_request.status == 200) {
document.getElementById('processrequest').innerHTML = http_request.responseText;
} else {
alert('There was a problem with the request.');
}
}
}
function get(obj) {
var poststr = "articleid=" + encodeURI( document.getElementById("articleid").value );
makePOSTRequest('processrequest.php', poststr);
}
this worked great for me in other cases, but not when the form is inside a php while loop. this script only sends the latest article's ID. So if I have 6 articles in the database, the ID "6" is sent no matter which article you choose.
If I remove the ajax and use a regular submit button, it works fine. Obviously my problem is in the ajax, I figured I should "tell" ajax that the data is taken from a loop somehow...
Anyway, if anyone can tell me how to make this work properly using the ajax, i would greatly appreciate it!
Thanks!