helpchrisplz
asked on
some php/ java i cant work out
i found some code online, and i am trying to understand it.
the code is used to create a web page that extracts data from a MySQL database without having to refresh the page.
mysql_insert.html (This is the page that users will see)
mysql_insert.php (This file will do all the hard work and will return results to mysql_insert.html)
this is the code for the data base that i have uploaded to my server runing php:
CREATE TABLE `php_js` (
`srno` INT NOT NULL AUTO_INCREMENT ,
`u_name` VARCHAR( 50 ) NOT NULL ,
`u_email` VARCHAR( 55 ) NOT NULL ,
PRIMARY KEY ( `srno` )
);
in the code bellow i have not edited any of it and i have not put my user pass in to connect the data base.
i am not sure if i need to edit this line: function attach_file( p_script_url ) {
but when i test this code from the server it does work it. just doesnt do anything. just says click under the fileds . here is a screen shot: http://img260.imageshack.us/img260/8833/32192672.jpg
the code is used to create a web page that extracts data from a MySQL database without having to refresh the page.
mysql_insert.html (This is the page that users will see)
mysql_insert.php (This file will do all the hard work and will return results to mysql_insert.html)
this is the code for the data base that i have uploaded to my server runing php:
CREATE TABLE `php_js` (
`srno` INT NOT NULL AUTO_INCREMENT ,
`u_name` VARCHAR( 50 ) NOT NULL ,
`u_email` VARCHAR( 55 ) NOT NULL ,
PRIMARY KEY ( `srno` )
);
in the code bellow i have not edited any of it and i have not put my user pass in to connect the data base.
i am not sure if i need to edit this line: function attach_file( p_script_url ) {
but when i test this code from the server it does work it. just doesnt do anything. just says click under the fileds . here is a screen shot: http://img260.imageshack.us/img260/8833/32192672.jpg
this is code for mysql_insert.html file
<html>
<head>
<script language=\"JavaScript\"><!--
//these function from:
//http://www.devshed.com/c/a/PHP/PHP-and-JavaScript-Pooling-Your-Resources/
//this function calls the child file
function attach_file( p_script_url ) {
// create new script element, set its relative URL, and load it
script = document.createElement( \'script\' );
script.src = p_script_url;
document.getElementsByTagName( \'head\' )[0].appendChild( script );
}
//this function updates the status box
function show_status( status_text ) {
document.getElementById(\'status\').innerHTML = status_text;
}
//-->
</script>
<title>PHP MySQL and Javascripts Working Together </title>
<!-- CSS Details -->
<style type=\"text/css\">
<!--
.table1 {
border: 1px solid #CC6600;
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 10px;
font-style: normal;
text-transform: none;
}
body {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 8px;
background-position: center top;
margin: 0px;
padding: 15px;
}
input {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 10px;
font-style: normal;
font-weight: bold;
font-variant: normal;
color: #0099CC;
background-color: #FFFFCC;
border: 1px solid #FF9900;
}
.row1 {
background-color: #CCFFCC;
}
.style1 {
color: #FFFFFF;
font-weight: bold;
}
.row2 {
background-color: #FFFFFF;
}
-->
</style>
</head>
<body onLoad=\"javascript:attach_file( \'mysql_insert.php\' ) ; show_status(\'Ready...\'); \">
<table width=\"600\" border=\"0\" cellpadding=\"1\" cellspacing=\"3\" class=\"table1\">
<tr>
<td width=\"88\">Status Box: </td>
<td width=\"493\" bgcolor=\"#CCCCCC\"><span id=\"status\" /></span> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Name</td>
<td><input name=\"u_name\" type=\"text\" id=\"u_name\" size=\"40\" maxlength=\"40\"></td>
</tr>
<tr>
<td>Email</td>
<td><input name=\"u_email\" type=\"text\" id=\"u_email\" size=\"40\" maxlength=\"45\">
(email MUST contain the \'@\' and \'.\') </td>
</tr>
<tr>
<td> </td>
<td><input type=\"button\" name=\"Button\" value=\"Click Here to Insert into Mysql...\" onClick=\"javascript:attach_file( \'mysql_insert.php?u_name=\' + u_name.value + \'&u_email=\' + u_email.value) ; show_status(\'Busy\'); \"></td>
</tr>
</table>
<br>
Data from MySQL: <br>
<!-- Do not remove this or the table will not show up -->
<span id=\"from_mysql\">
</span>
<br>
</body>
</html>
==============================================
here is code for mysql_insert.php file
<?php
header( \'Content-Type: text/javascript\' );
//database info: Change password and DB Name
$db = mysql_connect(\"localhost\", \"root\", \"\") or die (mysql_error());
mysql_select_db (\"test\", $db) or die (mysql_error());
//get values if input fields
$u_name = addslashes($_GET[\'u_name\']); //Name
$u_email = addslashes($_GET[\'u_email\']); //Email
//If name and email are NOT empty, insert into mysql
if (strlen($u_name)>1 and strlen($u_email)>1 and strstr($u_email,\"@\") and strstr($u_email,\".\") ) {
$insert = mysql_query(\"INSERT INTO php_js (u_name, u_email) VALUES (\'$u_name\', \'$u_email\')\",$db) or die(mysql_error());
}
//Now Refresh the table at the bottom id=from_mysql
$row_count = 0;
$output = \'<table width=\"600\" border=\"0\" cellpadding=\"3\" cellspacing=\"0\" class=\"table1\"><tr><td width=\"41\" align=\"center\" bgcolor=\"#3366CC\"><span class=\"style1\">Srno</span></td><td width=\"149\" align=\"center\" bgcolor=\"#3366CC\"><span class=\"style1\">Name</span></td><td width=\"384\" align=\"center\" bgcolor=\"#3366CC\"><span class=\"style1\">Email</span></td></tr>\';
$result = mysql_query(\"SELECT srno, u_name, u_email FROM php_js\",$db) or die (mysql_error());
While( $rows = mysql_fetch_array($result)) {
$srno = $rows[\'srno\'];
$u_name = $rows[\'u_name\'];
$u_email = $rows[\'u_email\'];
$row_style = ($row_count % 2) ? \"row1\" : \"row2\";
$output .= \'<tr class=\"\'.$row_style.\'\"><td>\'.$srno.\'</td><td>\'.$u_name.\'</td><td>\'.$u_email.\'</td></tr>\';
$row_count = $row_count + 1;
}
//Free Results
mysql_free_result($result);
$output .= \'</table>\';
?>
from_mysql_obj = document.getElementById( \'from_mysql\' );
from_mysql_obj.innerHTML = \'<? echo $output; ?>\';
//update status box
my_status = document.getElementById( \'status\' );
my_status.innerHTML = \'Ready...\';
//clear values from text fields
document.getElementById(\'u_name\').value = \'\';
document.getElementById(\'u_email\').value = \'\';
?>
this is code for mysql_insert.html file
<html>
<head>
<script language="JavaScript"><!--
//these function from:
//http://www.devshed.com/c/a/PHP/PHP-and-JavaScript-Pooling-Your-Resources/
//this function calls the child file
function attach_file( p_script_url ) {
// create new script element, set its relative URL, and load it
script = document.createElement( 'script' );
script.src = p_script_url;
document.getElementsByTagName( 'head' )[0].appendChild( script );
}
//this function updates the status box
function show_status( status_text ) {
document.getElementById('status').innerHTML = status_text;
}
//-->
</script>
<title>PHP MySQL and Javascripts Working Together </title>
<!-- CSS Details -->
<style type="text/css">
<!--
.table1 {
border: 1px solid #CC6600;
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 10px;
font-style: normal;
text-transform: none;
}
body {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 8px;
background-position: center top;
margin: 0px;
padding: 15px;
}
input {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 10px;
font-style: normal;
font-weight: bold;
font-variant: normal;
color: #0099CC;
background-color: #FFFFCC;
border: 1px solid #FF9900;
}
.row1 {
background-color: #CCFFCC;
}
.style1 {
color: #FFFFFF;
font-weight: bold;
}
.row2 {
background-color: #FFFFFF;
}
-->
</style>
</head>
<body onLoad="javascript:attach_file( 'mysql_insert.php' ) ; show_status('Ready...'); ">
<table width="600" border="0" cellpadding="1" cellspacing="3" class="table1">
<tr>
<td width="88">Status Box: </td>
<td width="493" bgcolor="#CCCCCC"><span id="status" /></span> </td>
</tr>
<tr>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Name</td>
<td><input name="u_name" type="text" id="u_name" size="40" maxlength="40"></td>
</tr>
<tr>
<td>Email</td>
<td><input name="u_email" type="text" id="u_email" size="40" maxlength="45">
(email MUST contain the '@' and '.') </td>
</tr>
<tr>
<td> </td>
<td><input type="button" name="Button" value="Click Here to Insert into Mysql..." onClick="javascript:attach_file( 'mysql_insert.php?u_name=' + u_name.value + '&u_email=' + u_email.value) ; show_status('Busy'); "></td>
</tr>
</table>
<br>
Data from MySQL: <br>
<!-- Do not remove this or the table will not show up -->
<span id="from_mysql">
</span>
<br>
</body>
</html>
==============================================
here is code for mysql_insert.php file
<?php
header( 'Content-Type: text/javascript' );
//database info: Change password and DB Name
$db = mysql_connect("localhost", "root", "") or die (mysql_error());
mysql_select_db ("test", $db) or die (mysql_error());
//get values if input fields
$u_name = addslashes($_GET['u_name']); //Name
$u_email = addslashes($_GET['u_email']); //Email
//If name and email are NOT empty, insert into mysql
if (strlen($u_name)>1 and strlen($u_email)>1 and strstr($u_email,"@") and strstr($u_email,".") ) {
$insert = mysql_query("INSERT INTO php_js (u_name, u_email) VALUES ('$u_name', '$u_email')",$db) or die(mysql_error());
}
//Now Refresh the table at the bottom id=from_mysql
$row_count = 0;
$output = '<table width="600" border="0" cellpadding="3" cellspacing="0" class="table1"><tr><td width="41" align="center" bgcolor="#3366CC"><span class="style1">Srno</span></td><td width="149" align="center" bgcolor="#3366CC"><span class="style1">Name</span></td><td width="384" align="center" bgcolor="#3366CC"><span class="style1">Email</span></td></tr>';
$result = mysql_query("SELECT srno, u_name, u_email FROM php_js",$db) or die (mysql_error());
While( $rows = mysql_fetch_array($result)) {
$srno = $rows['srno'];
$u_name = $rows['u_name'];
$u_email = $rows['u_email'];
$row_style = ($row_count % 2) ? "row1" : "row2";
$output .= '<tr class="'.$row_style.'"><td>'.$srno.'</td><td>'.$u_name.'</td><td>'.$u_email.'</td></tr>';
$row_count = $row_count + 1;
}
//Free Results
mysql_free_result($result);
$output .= '</table>';
?>
from_mysql_obj = document.getElementById( 'from_mysql' );
from_mysql_obj.innerHTML = '<? echo $output; ?>';
//update status box
my_status = document.getElementById( 'status' );
my_status.innerHTML = 'Ready...';
//clear values from text fields
document.getElementById('u_name').value = '';
document.getElementById('u_email').value = '';
ASKER
what does this bit do. what is p_script_url?
function attach_file( p_script_url ) {
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
sorry about being slow
get rid of the slashes preceeding the apostrophes
Open in new window