Javascript & MySQL

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
edoardor
Forum Newbie
Posts: 7
Joined: Sun Aug 11, 2002 7:05 am
Location: Italy

Javascript & MySQL

Post by edoardor »

someone knows like making in order to approach of the MySql tables from a Javascript?

thanks

have a good day
User avatar
haagen
Forum Commoner
Posts: 79
Joined: Thu Jul 11, 2002 3:57 pm
Location: Sweden, Lund

Post by haagen »

You can't communicate directly with a database from a javascript. How ever, you can load values from a database into a javascript varianble or array when you create your page with php (or what ever).

Code: Select all

function myJavaScriptFunc()
myVar = "<? echo $phpvariable; ?>";
alert(myVar);
&#125;
This way you transfer data from PHP to JavaScript.
edoardor
Forum Newbie
Posts: 7
Joined: Sun Aug 11, 2002 7:05 am
Location: Italy

Post by edoardor »

before they are explained me badly:

I have a form like:

<form name="form3" method="post">

<input type="text" name="cod" value="<? print $row[aliquota]; ?>" maxlength="3" size="3" >
<? print $desc;?> // desc is set by a php search function to mysql table where cod is the code and desc is description

<input type="submit" name="Go" value="Confirm">
</form>

This dysplay corret the first time.

I would want that when comes modified the value of "cod" came automatically modified also $desc...

I can call with "onChange=" a Javascript and perform the query ?

Thanks
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

everytime you want php to do something from within a browser you must send a complete request to the server where the script is parsed and executed. All additional data you want to send (like form-values) must be sent with this request via 'get' or 'post' (or 'put'). The respond then is parsed and (hopefully) displayed by the requesting browser)
It's like requesting a whole new page (often it is).

If you refresh the page on every single onChange the user will be delayed after every noticed change and even if you request only a part of the page (inserting it with innerHTML oder HTML-DOM) I'm not quite sure if you want that

but try this one

Code: Select all

<html>
<?php
function doFrameset()
&#123; ?>
	<frameset rows="*,50">
		<frame src="test.php" name="fForm">
		<frame src="about:" name="fDesc">
	</frameset>
<?php
&#125;

function doDescription()
&#123;
	print('<body>description of '.$_POST&#1111;'cod'].' is ...</body>');
&#125;

function doQuery()
&#123;
	print('performing '.$_POST&#1111;'cod'].'...');
&#125;

function doForm()
&#123; ?>
	<head><script language="JavaScript">
	function doSearch()
	&#123;
		form3.mode.value = "search";
		form3.target="fDesc";
		form3.submit();
	&#125;
	
	function doSubmit()
	&#123;
		form3.mode.value = "perform";
		form3.target="fForm";
		form3.submit();
	&#125;
	</script></head>
	<body>
	<form name="form3" method="post"> 
	<input type="hidden" name="mode" value="" />
	<input type="text" name="cod" value="123" maxlength="3" size="3" onChange="doSearch();" />
	<br/>
	<button onClick="doSubmit();">submit</button>
	</form> 
<?php
&#125;

if (isset($_POST&#1111;'mode']))
&#123;
	if ($_POST&#1111;'mode']=='search')
		doDescription();
	else
	&#123;
		doForm();
		doQuery();
		print('</body>');
	&#125;			
&#125;
else
&#123;
	doForm();
	print('</body>');
&#125;

?>
</html>
it's only a quick&dirty hack but maybe it helps you.
edoardor
Forum Newbie
Posts: 7
Joined: Sun Aug 11, 2002 7:05 am
Location: Italy

Post by edoardor »

I solved my problem, after much tiredness, of this way:

file.html

Code: Select all

<html>
<head>
<applet width="0" height="0" name="Query" code="Query" codebase="">
</applet>
<script language="JavaScript">
<!--
function loadField(fname,codname,desname, submit) &#123;
  stat = false;
  f_name = document&#1111;fname]
  sql = "select xxxx from yyyy where zzz = '"+f_name&#1111;codname].value+"'"
  myConn = "jdbc:mysql://localhost/database?user=user&password=passwd"
  field = "desc"
  ret = document.Query.setString(sql, field, myConn)
  if (ret != "N")
  	f_name&#1111;desname].value = ret
  if (ret == "N") &#123;                             //record not found
    alert("Not inserted")
    f_name&#1111;codname].focus()
    stat = true
  &#125;
    f_name&#1111;submit].disabled = stat
&#125;
// -->
</script>

</head>
<body>
<form name="form1" method="post"> 
<input type="text" name="ali" value="" maxlength="3" size="3" onChange="loadField('form1','ali','desc','Invia');"/> 
<input type="text" name="desc" value="" maxlength="100" size="100" onFocus="this.blur();document.form1.Invia.focus();"/> 
<input type="submit" name="Invia" value="Conferma">
</form> 
</body>
</html>
query.java

Code: Select all

import java.applet.Applet;
import java.sql.*;

public class Query extends Applet &#123;
   String myConn;
   String text;
   public void init() &#123;
      text = new String("Ciao");
   &#125;
   public String setString(String sql, String field, String myConn) &#123;
	text = "N";
	try &#123;
	    Class.forName("org.gjt.mm.mysql.Driver");
	&#125;
	catch(Exception ex) &#123;
	    text = "Can't find Database driver class: " + ex;
	    return text;
	&#125;
	try &#123;
	    Connection con = (Connection) DriverManager.getConnection(myConn);
	    Statement stmt = (Statement) con.createStatement();
	    ResultSet rs = (ResultSet) stmt.executeQuery(sql);
	    while (rs.next()) &#123;
		text = rs.getString(field);
	    &#125;
	stmt.close();
	con.close();
	&#125; 
	catch(SQLException ex) &#123;
	    text = "SQLException:   " + ex;
	&#125;
	return text;
   &#125;
&#125;
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

ahh...with an applet ;)

btw: There's a new jdbc-connector version at http://www.mysql.com/
They integrated the old one in their portofolio and made it fully class IV compatible. jippie and halleluja, we are able to transfer 2GB blobs ;)
Post Reply