homepage Welcome to WebmasterWorld Guest from 54.237.99.131
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
using php in a Javascript
using php in a Javascript
kootje

5+ Year Member



 
Msg#: 3974 posted 9:11 am on Jul 4, 2005 (gmt 0)

Hi all,

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

 

orhor

5+ Year Member



 
Msg#: 3974 posted 9:43 am on Jul 4, 2005 (gmt 0)

First:
All the php is "executed" on the server side through t5he process of building the HTML output for the browser(client), all the javascript is executed on the client side - without the possibility run php. This is the reason you can't combine these two.

But that doesnt mean your problem is unsoluteable.

Maybe try this. Look at the part of loading external data via iframe. [howtocreate.co.uk...]

copongcopong

10+ Year Member



 
Msg#: 3974 posted 9:50 am on Jul 4, 2005 (gmt 0)

from your script, it appears that you have already dump all the questions and answers on the page, anyone checking the source code would see the answers ... isn't it?

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.

kootje

5+ Year Member



 
Msg#: 3974 posted 10:19 am on Jul 4, 2005 (gmt 0)

It's not a problem when the users see the answers in the source code.. it's like a faq system and not a game zo it doesn't matter. I do this because their are a lot of questions and I want them to find the question as fast as possible.. I'm looking now for the hidden iframe but it doesn't work perfectly for me..It is giving me a blank page after loading the koppeling.php

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);
}
?>

copongcopong

10+ Year Member



 
Msg#: 3974 posted 10:32 am on Jul 4, 2005 (gmt 0)

create a page that would be called in an iframe.

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.

kootje

5+ Year Member



 
Msg#: 3974 posted 11:16 am on Jul 4, 2005 (gmt 0)

thanks copongcopong,

but it's going a little bit to quickly for me..

don't know how to create a dynamically created page etc.. Just don't understand it like this.. Can you give me a better explained answer maybe with some sort of example code.

Thanks a lot

orhor

5+ Year Member



 
Msg#: 3974 posted 8:39 am on Jul 7, 2005 (gmt 0)

I don't know if I catched exactly what you need, but what about something like this?

<?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)

orhor

5+ Year Member



 
Msg#: 3974 posted 8:49 am on Jul 7, 2005 (gmt 0)

sorry, those two DIVs should look like this, of course:

<div id='answerDiv'>&nbsp;</div>
<div id='questionsDiv'>&nbsp;</div>

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved