codingForumsFTW
01-02-2010, 06:19 PM
i would like to look up a database entry by typing in the id and then pulling all the data into a form.
i have this but the problem is that i does not forget the last entry that was put in it. this is what i have so far
and this works pretty much like what i want but when i look up another entry in the database it does not write over the old entry.
additionally, i am not able to put this into a input box (i dont know why)
it will only work in a <p> tag
can anyone look at this code and tell me what i am doing wrong
javascript
<script type="text/JavaScript">
$(document).ready(function(){
timestamp = 0;
$("form#chatform").submit(function(){
$.post("file/test2.php",{
q: $("#msg").val(),
}, function(xml) {
$("#msg").empty();
addMessages(xml);
});
return false;
});
});
function addMessages(xml) {
if($("status",xml).text() == "2") return;
$("message",xml).each(function(id) {
message = $("message",xml).get(id);
$("#1").prepend($("author",message).text());
$("#2").prepend($("text",message).text());
});
}
</script>
html
<form id="chatform">
Message: <input type="text" id="msg" />
<input type="submit" value="ok" /><br />
<table border="1" cellspacing="5" cellpadding="5">
<tr>
<td><input type="text" id="2" name="2" /></td>
<td><p id="1"></p></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
</form>
and my php
// Script
error_reporting(E_ALL);
header("Content-type: text/xml");
header("Cache-Control: no-cache");
$dbconn = mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($dbname,$dbconn);
foreach($_POST as $key => $value)
{
$$key = mysql_real_escape_string($value, $dbconn);
}
$messages = mysql_query("SELECT lastname,firstname,id
FROM p1
WHERE id=$q",$dbconn);
if(mysql_num_rows($messages) == 0) $status_code = 2;
else $status_code = 1;
echo "<?xml version=\"1.0\"?>\n";
echo "<response>\n";
if($status_code == 1)
{
while($message = mysql_fetch_array($messages))
{
$message['msg'] = htmlspecialchars(stripslashes($message['msg']));
echo "\t<message>\n";
echo "\t\t<author>$message[lastname]</author>\n";
echo "\t\t<text>$message[firstname]</text>\n";
echo "\t\t<texts>$message[id]</texts>\n";
echo "\t</message>\n";
}
}
echo "</response>";
?>
i have this but the problem is that i does not forget the last entry that was put in it. this is what i have so far
and this works pretty much like what i want but when i look up another entry in the database it does not write over the old entry.
additionally, i am not able to put this into a input box (i dont know why)
it will only work in a <p> tag
can anyone look at this code and tell me what i am doing wrong
javascript
<script type="text/JavaScript">
$(document).ready(function(){
timestamp = 0;
$("form#chatform").submit(function(){
$.post("file/test2.php",{
q: $("#msg").val(),
}, function(xml) {
$("#msg").empty();
addMessages(xml);
});
return false;
});
});
function addMessages(xml) {
if($("status",xml).text() == "2") return;
$("message",xml).each(function(id) {
message = $("message",xml).get(id);
$("#1").prepend($("author",message).text());
$("#2").prepend($("text",message).text());
});
}
</script>
html
<form id="chatform">
Message: <input type="text" id="msg" />
<input type="submit" value="ok" /><br />
<table border="1" cellspacing="5" cellpadding="5">
<tr>
<td><input type="text" id="2" name="2" /></td>
<td><p id="1"></p></td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
</table>
</form>
and my php
// Script
error_reporting(E_ALL);
header("Content-type: text/xml");
header("Cache-Control: no-cache");
$dbconn = mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($dbname,$dbconn);
foreach($_POST as $key => $value)
{
$$key = mysql_real_escape_string($value, $dbconn);
}
$messages = mysql_query("SELECT lastname,firstname,id
FROM p1
WHERE id=$q",$dbconn);
if(mysql_num_rows($messages) == 0) $status_code = 2;
else $status_code = 1;
echo "<?xml version=\"1.0\"?>\n";
echo "<response>\n";
if($status_code == 1)
{
while($message = mysql_fetch_array($messages))
{
$message['msg'] = htmlspecialchars(stripslashes($message['msg']));
echo "\t<message>\n";
echo "\t\t<author>$message[lastname]</author>\n";
echo "\t\t<text>$message[firstname]</text>\n";
echo "\t\t<texts>$message[id]</texts>\n";
echo "\t</message>\n";
}
}
echo "</response>";
?>