English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I have a Javascript function that creates a tabbed browsing section, for example, one tab is the most recently added news. How do I get the results from my sql query (recent news) in the Javascript. Here's how I would be able to do it in a perfect world... You should see what I am looking for...

var center_tabs = [
//first tab
[
'images/tabs/news.gif',
'images/tabs/news_selected.gif',
' for($i=0; $i < $news_query; $i++)
{
$tag = $news_query[$i][2];
if($class != "tab_row1")
$class = "tab_row1";
else
$class = "tab_row2";
echo "

$tag
\n";
}
?>'],
//second tab
[



Thanks.

2007-03-26 10:05:17 · 4 answers · asked by Duds331 5 in Computers & Internet Programming & Design

4 answers

that actually looks close! I wouldn't code it that way though!

that code gives you a nice array... then you could just javascript to write the center_tabs into the html.

Why not just write the html in the php code, or use JSON to drop the code from php to javascript.

JSON is JavaScript Object Notation. a quickie bridge from php to javascript. You can use it with or without AJAX.

2007-03-26 10:55:12 · answer #1 · answered by jake cigar™ is retired 7 · 1 0

So where is the javascript function?
What is the $class var?
What is the problem?

This is not enough information to understand what the problem is or what you are asking.

Javascript will only be able to negotiate the values you echo out onto the screen. Only the echo stuff makes it back to the browser which is where the javascript gets run.

You wanna know how to get the results from mySql query?
That's specifically what is asked....

okay here is how to do that:

mysql_connect("localhost", "mysql_user", "mysql_password") or
die("Could not connect: " . mysql_error());
mysql_select_db("mydb");

$result = mysql_query("SELECT id, name FROM mytable");

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
printf("ID: %s Name: %s", $row[0], $row[1]);
}

mysql_free_result($result);
?>


Also - i disagree with the guy who says you will need to structure your query results in JSON or XML.
No you don't. That's only useful when manipulating huge sets of data like reports that could return thousands of records with unique columns...

Maybe what you are asking is this - how can I place an action on a link or button (tab) that fires a request to the server.....
Each tab could have a link like -
www.mysite.com/home.php? openTab=2 or whatever.
Then your script is just waiting there for the $_GET.....

Where I do agree with the last poster and what he likely meant was - to make a request to the server (and therefore php) you can ONLY use POST or GET....(and possibly httprequest object...)

Hope that helps~

2007-03-26 10:13:15 · answer #2 · answered by Nicholas J 7 · 0 1

You can't put php code in side javascript, you have to make a script on the server that pulls the data out of the database and sends it back to the javascript in XML or JSON format, then use XmlHttpRequest from javascript to call that script. This is basically how AJAX works.

2007-03-26 10:11:40 · answer #3 · answered by undercoloteal 3 · 0 1

regrettably, you do now no longer come suitable right here 24hrs till eventually now the due date to do your homework: that's in complication-free words too overdue! Now, keep in mind one needed ingredient: - js code is on the guy's gadget. - personal residing residing house web page is on the server. they're MILES appart. - personal residing residing house web page runs first, and many cases generate the js code, then furnish it to the guy. in a number of words, personal residing residing house web page variables are frequently happening to personal residing residing house web page, and could be despatched to js, yet now no longer any different way round!!! As you're using ajax, that is the purely thanks to do it: furnish an HTTPrequest to a personal residing residing house web page function. That request will contain the end results of your js script. your individual residing residing house web page function can then use the concepts received to do in spite of you want, mutually with sending an replace on your individual reveal. now no longer expertise how your AJAX is determined-up, i visit't manage to fairly help you, yet you get the basics. js establishes the variable. ajax takes that variable and furnish it to personal residing residing house web page. (The "how" relies upon on your setup) personal residing residing house web page receives the variable and writes it to a record.

2016-12-02 20:48:12 · answer #4 · answered by ? 4 · 0 0

fedest.com, questions and answers