someone knows like making in order to approach of the MySql tables from a Javascript?
thanks
have a good day
Javascript & MySQL
Moderator: General Moderators
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).
This way you transfer data from PHP to JavaScript.
Code: Select all
function myJavaScriptFunc()
myVar = "<? echo $phpvariable; ?>";
alert(myVar);
}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
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
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 oneit's only a quick&dirty hack but maybe it helps you.
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()
{ ?>
<frameset rows="*,50">
<frame src="test.php" name="fForm">
<frame src="about:" name="fDesc">
</frameset>
<?php
}
function doDescription()
{
print('<body>description of '.$_POSTї'cod'].' is ...</body>');
}
function doQuery()
{
print('performing '.$_POSTї'cod'].'...');
}
function doForm()
{ ?>
<head><script language="JavaScript">
function doSearch()
{
form3.mode.value = "search";
form3.target="fDesc";
form3.submit();
}
function doSubmit()
{
form3.mode.value = "perform";
form3.target="fForm";
form3.submit();
}
</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
}
if (isset($_POSTї'mode']))
{
if ($_POSTї'mode']=='search')
doDescription();
else
{
doForm();
doQuery();
print('</body>');
}
}
else
{
doForm();
print('</body>');
}
?>
</html>I solved my problem, after much tiredness, of this way:
file.html
query.java
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) {
stat = false;
f_name = documentїfname]
sql = "select xxxx from yyyy where zzz = '"+f_nameї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їdesname].value = ret
if (ret == "N") { //record not found
alert("Not inserted")
f_nameїcodname].focus()
stat = true
}
f_nameїsubmit].disabled = stat
}
// -->
</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>Code: Select all
import java.applet.Applet;
import java.sql.*;
public class Query extends Applet {
String myConn;
String text;
public void init() {
text = new String("Ciao");
}
public String setString(String sql, String field, String myConn) {
text = "N";
try {
Class.forName("org.gjt.mm.mysql.Driver");
}
catch(Exception ex) {
text = "Can't find Database driver class: " + ex;
return text;
}
try {
Connection con = (Connection) DriverManager.getConnection(myConn);
Statement stmt = (Statement) con.createStatement();
ResultSet rs = (ResultSet) stmt.executeQuery(sql);
while (rs.next()) {
text = rs.getString(field);
}
stmt.close();
con.close();
}
catch(SQLException ex) {
text = "SQLException: " + ex;
}
return text;
}
}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
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