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
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 = \'\';
?>

Open in new window

LVL 1
helpchrisplzAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hieloCommented:
>>script = document.createElement( \'script\' );
get rid of the slashes preceeding the apostrophes
<script type="text/javascript">

//this function calls the child file
function attach_file( p_script_url ) {
// create new script element, set its relative URL, and load it
var 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>

Open in new window

0
hieloCommented:

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 = '';

Open in new window

0
helpchrisplzAuthor Commented:

what does this bit do. what is p_script_url?

function attach_file( p_script_url ) {
0
hieloCommented:
>>what is p_script_url?
Whenever you call the function attach_file(), you need to provide the url of the file you want to "import". In your case that is being done on the following line:

<body onLoad="javascript:attach_file( 'mysql_insert.php' ) ; show_status('Ready...'); ">

So, when attach_file is executed, p_script_url "inherits" the value mysql_insert.php because that is the value passed to the function when it was called. To clarify further, if you were to call the function like this:

attach_file('file2.html'); then p_script_url is set to file2.html

attach_file('someotherfile.asp'); then p_script_url is set to someotherfile.asp
etc.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
helpchrisplzAuthor Commented:
sorry about being slow
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.