Forum Moderators: open
I'm new to this forum but have quite a big problem.. I'm making a question and answer system for about 400 or more question and answers. First I've got topics, then categories and last their are the questions.
The questions comes out of a database with a select query in php.
When the visitor clicks on the questions it shows the answer via a javascript function. On the same moment I want to do a insert query in the database for following what questions are visited.
This is the Javascript code:
<script language="JavaScript">
function toggleAnswer(e) { var eAnswer = e.parentNode.nextSibling;
while (eAnswer.tagName!= 'DD') eAnswer = eAnswer.nextSibling;
if (eAnswer.style.display == 'block')
eAnswer.style.display = 'none';
else
eAnswer.style.display = 'block';
return false;
}
</script>
<?php
if (mysql_num_rows($resultaat) > 0) {
while ($rij = mysql_fetch_array($resultaat))
{
echo "<dl class='faq'>";
echo "<dt id=kopje>
<a href='#' name=test onclick='toggleAnswer(this);'>".$rij["Question"]."</a></dt>";
echo "<dd id=beschrijving><div id=test>".$rij["Onderwerp"]."</div> \n ".$rij["Answer"]."</dd>";
echo "</dl>";
}
}
mysql_close($db);
?>
Is it possible to do a insert query in the Javascript. Or does someone know a solution for this?
Thanks in advance
But that doesnt mean your problem is unsoluteable.
Maybe try this. Look at the part of loading external data via iframe. [howtocreate.co.uk...]
Why not load the answer when the user click on the question? (via pop-up page, refresh the page with the answer, calling an iframe, ajax ...)
From there, you can add php to call insert a value to tag the question that was picked before loading the answer.
This is my code now:
<script language="JavaScript">
function toggleAnswer(e) { var eAnswer = e.parentNode.nextSibling;
while (eAnswer.tagName!= 'DD') eAnswer = eAnswer.nextSibling;
if (eAnswer.style.display == 'block')
eAnswer.style.display = 'none';
else
eAnswer.style.display = 'block';
return false;
}
</script>
<?php
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_array($result))
{
echo "<dl class='faq'>";
echo "<dt>
<a href=faq/terug/koppeling.php?id=".$row["Qnr"]." target='koppeling' name=test onclick='toggleAnswer(this);'>".$row["Question"]."</a></dt>";
echo "<dd id=beschrijving><div id=test>".$row["Subject"]."</div> \n ".$row["Answer"]."</dd>";
echo "</dl>";
echo "<iframe id='koppeling'
name='koppeling'
style='width:100px; height:100px; border: 0px'
src='blank.html'></iframe>";
}
}
and koppeling.php
[code]
<?php
$db = mysql_connect("server", "username", "password") or die("kan niet verbinden:" . mysql_error());
mysql_select_db("database", $db);
$sql="insert into koppeling (Qnr,nawNr) values(1,1)";
$resultaat = mysql_query($sql);
?>
<br>
<div align="center"><b>De gegevens werden correct ingevoerd!</b></a></div>
<br>
<div align="center"><b>Je kan nu dit venster sluiten om terug te keren naar de les.</b>
</div>
<br>
<?php
mysql_close($db);
}
?>
e.g. check_questions.php then let php access the $_GET info.
e.g. check_questions.php?q_id=220
your code,
if(isset($_GET['q_id'])){
do the insert thing here using the $_GET['q_id']
}
on the main page, create a javascript that creates an iframe dynamically or have a hidden iframe on the page, then change the src through javascript
e.g. myiframe.src=check_questions.php?q_id=220;
when the iframe opens the check_questions.php page, it run your php script inserting/updating a table when a parameter is passed via $_GET or the url as a query string.
<?php
// ... make your connection etc...
$sql="SELECT * FROM table"; // I think zou have all Q/A in one table in columns Question and Answer
$result=mysql_query($sql);
echo "<script script language='JavaScript'>\n<!--";
$i=0;
echo "answers = new Array();";
echo "questions = new Array();";
while ($row = mysql_fetch_array($result)){
echo "answers[$i]=\"".$row['Answer']."\";\n"; // writing a javascript array of answers
echo "questions[$i]=\"".$row['Question']."\";\n"; // writing a javascript array of questions
// check both in page source later
$i++;
}
?>
function writeQuestions(){ // function executed by page onLoad, writes all questions in the page
txt="";
for (x=0;x<answers.length;x++){
txt+="<a href='javascript: writeAnswer("+x+");'>"+questions[x]+"</a><br>";
}
document.getElementById("questionsDiv").innerHTML=txt; // puts the prepared html into apropriate div
}
function writeAnswer(num){
document.getElementById("answerDiv").innerHTML=answers[num];
}
//-->
</script>
</head>
<body onLoad='javascript:writeQuestions();'>
<div id='answerDiv'>
<div id='questionsDiv'>
</body>
(I didnt test it and wrote it very quickly - hot needle - before my work, so excuse me for some little mistakes, if any I can correct them later, but I just wanted to show another way)