Help - Search - Members - Calendar
Full Version: php and javascript
Invision Power Services > Community Forums > Community Web Design and Coding
Lbruce
Hi there,

i retrieve a data set from a mysql database and i store it in a javascript array. What I cannot figure out is how to provide users with a drop down menu with two options and based on what they select, different records from the data set will be displayed in a columnar table.

I would appreciate any help.

Thanks.
Brendon Koz
Ummm...are you using AJAX, or are you creating the JS dynamically using PHP? Is it imperative to your script to not have a page reload and simply use just PHP, (X)HTML and CSS?

Best tip: Get it working without javascript first, then go back and add it in.
elj
http://www.dhtmlgoodies.com/index.html?page=ajax

Are any of these any good to you?
Lbruce
Guys thanks for the replies. I really appreciate your help.
Actually, i am not using ajax. I am just using PHP to generate javascript.

But, i am not sure if what i am trying to do is feasible. So, i would very much appreciate if u can tell me your opinion.

My database stores information about court cases and related events. I query the database just once and the retrieved data set is stored into a javascript array. My aim is to minimise the client - server transactions.

Now i want the user to view a columnar table with some of the retrieved records and by using a drop down menu to be able to view additional information without having to query the database again. For example, while the initial table will dislpay data such as case number, party name and judge name, by choosing the first option of the drop down menu the user would be able to see information such as case number, charges, sentence imposed etc. (I hope it is a clear explanation)

So, is it possible to do that by just querying the database once and using only one javascript array?

Thanks again.
Brendon Koz
It is possible to do this with just JavaScript and one query to the database (retreiving all records), however...if you ever get a large number of records into the database, the page loading time could eventually reach a very, very large number (in seconds). If you still would want the user to not have to browse away from the single page, then you'd eventually end up having to use some sort of AJAXy type call into the database to reduce server load per call.

You could also do this using pure PHP and (X)HTML and CSS. (X)HTML/CSS could define the dropdown menu, whereas PHP could store a session/cookie that holds all of the information you had originally queried the database for.

...now, since you usually like to write a site without restricting access to certain users (i.e.: those without JavaScript), using this method is probably better. You can always go back and add JavaScript to do the same thing without the page refreshes. But it's best to have something in place FIRST that doesn't require JS.

I guess it all depends on what you're more familiar with though, as well. If you're more comfortable using JavaScript, get that done first (as 90%+ of users will have it enabled), then go back and make sure it works without it, since it might take you a bit longer.

There's more than one way to do it (TMTOWTDI).

P.S. - I'm hoping Veracon will also give out his own suggestion since I saw him reading this topic.
Lbruce
Hello again,

i am trying to create multiple javascript arrays. One for each row of data and then display only the content of the a_caseID array in a drop down list.

I found a handy script on the web and i am trying to follow that. But while it seems that i have created three js arrays it is not working.
I would very much appreciate it if you could comment the following script.

<?
$fields = array();
$fields['caseID']=array();
$fields['casetype']=array();
$fields['casedetails']=array();

...

//perform query
$result = mysql_query("SELECT c_ID, c_type, c_details FROM CASES") or die ("Could Not execute Query");

while($row = mysql_fetch_array($result)){
array_push($fields['caseID'],$row['c_ID']);
array_push($fields['casetype'],$row['c_type']);
array_push($fields['casedetails'],$row['c_details']);
}
?>

<script language="JavaScript">
var js_caseID = new Array();
var js_casetype = new Array();
var js_casedetails = new Array();
<?
for ($i=0; $i<count($fields['caseID']); $i++) {
echo 'js_caseID['.$i.']= "'. $fields['caseID'][$i] .'";';
}
for ($i=0; $i<count($fields['casetype']); $i++) {
echo 'js_casetype ['.$i.']= "'. $fields['casetype '][$i] .'";';
}
for ($i=0; $i<count($fields['casedetails']); $i++) {
echo 'js_casedetails['.$i.']= "'. $fields['casedetails'][$i] .'";';
}
?>
</script>

<form><select name="case" size="1">
document.write(<option selected>Select case</option>);
<script type="text/javascript">
for(i = 0; i < js_caseID.length; i++){
document.write("<option>" + js_caseID[i] + "</option>");
}
</script>
</select></form>

Thanks for your help.
Lbruce
Solved it...
i just had to change the position of the script tags.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.