?
Solved

some php/ java i cant work out

Posted on 2010-03-31
6
Medium Priority
?
205 Views
Last Modified: 2013-12-12
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

0
Comment
Question by:helpchrisplz
  • 3
  • 2
5 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 29242170
>>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
 
LVL 82

Expert Comment

by:hielo
ID: 29242651

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
 
LVL 1

Author Comment

by:helpchrisplz
ID: 29296410

what does this bit do. what is p_script_url?

function attach_file( p_script_url ) {
0
 
LVL 82

Accepted Solution

by:
hielo earned 2000 total points
ID: 29343082
>>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
 
LVL 1

Author Closing Comment

by:helpchrisplz
ID: 31710648
sorry about being slow
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

589 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question