Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript with PHP integration

Status
Not open for further replies.

beirti

Programmer
Joined
Jun 18, 2003
Messages
18
Location
IE
This is a good example for a need for attachments in this forum, its a bit of a long post. Sorry...

I've been mainly using PHP since I started this project and have posted up the odd question here - thanks for all the replies!

This one is a bit strange, maybe I'm just missing a simple syntax error but I can't figure it out. I have 2 files both using a similar script, the functionality is exactly the same. The problem is, one works and the other doesn't.

The idea is to make a dynamic listbox which calls a function in its own file to refresh itself with the new information. The second file(hwSpecs.php) doesn't react when the combobox is changed. Look for the function

Ive pretty much given up on finding an error so good luck out there.

Warning, this is a long post.

Heres the working one:

/*///////////
newHWItem.php
/*///////////

<html>
<head>
<script language=&quot;JAVASCRIPT&quot;>
var lookupWindow = null;

function preload()
{
<?
$strType = $_GET[&quot;strType&quot;];
$strBody = &quot;&quot;;
global $strBody;
global $strType;

$strBody .= &quot;
<table border=\&quot;0\&quot;>
<form action=\&quot;script/hwController.php\&quot; method=\&quot;post\&quot; target=\&quot;Main\&quot; enctype=\&quot;multipart/form-data\&quot;>
<tr>
<td>Type:</td>
<td>
<select name=\&quot;lstType\&quot; onChange='javascript:reloadPage();'>
<option>$strType</option>
<option disabled>------</option>
<option>PC/Mac</option>
<option>Printer</option>
<option>General</option>
</select>
</td>
</tr>
<tr>
<td>Serial Number: </td>
<td><input type=text name=txtSerial></td>
<td>Seperate multiple serials by semicolon(;)</td>
</tr>
<tr>
<td>Status:</td>
<td>
<select name=\&quot;lstStatus\&quot;>
<option>Ok</option>
<option>Faulty</option>
<option>Under Repair</option>
<option>Beyond Repair</option>
</select>
</td>
</tr>
<tr>
<td><a href='javascript:lookup(\&quot;room\&quot;);'>Room: </a></td>
<td><input type=text name=txtRoom></td>
</tr>
<tr>
<td><a href='javascript:lookup(\&quot;make\&quot;);'>Make: </a></td>
<td><input type=text name=txtMake></td>
</tr>
<tr>
<td><a href='javascript:lookup(\&quot;model\&quot;);'>Model: </a></td>
<td><input type=text name=txtModel></td>
</tr>
<tr>
<td>Date Purchased: </td>
<td><input type=text name=txtDate></td>
</tr>
<tr>
<td>Warranty:</td>
<td>
<select name=\&quot;lstWarranty\&quot;>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
</td>
</tr>
&quot;;

if ($strType == &quot;PC/Mac&quot;)
{
$strBody .= &quot;<tr><td>Processor: </td><td><input type=text name=txtSpec1></td></tr>&quot;;
$strBody .= &quot;<tr><td>Ram: </td><td><input type=text name=txtSpec2></td></tr>&quot;;
$strBody .= &quot;<tr><td>Hard Disk: </td><td><input type=text name=txtSpec3></td></tr>&quot;;
$strBody .= &quot;<tr><td>Periph 1: </td><td><input type=text name=txtSpec4></td></tr>&quot;;
$strBody .= &quot;<tr><td>Periph 2: </td><td><input type=text name=txtSpec5></td></tr>&quot;;
$strBody .= &quot;<tr><td>Periph 3: </td><td><input type=text name=txtSpec6></td></tr>&quot;;
$strBody .= &quot;<tr><td>Monitor: </td><td><input type=text name=txtSpec7></td></tr>&quot;;
$strBody .= &quot;<tr><td><input type=submit name=btnNewItem value=Submit></td></tr>&quot;;

}// End of If

else if ($strType == &quot;Printer&quot;)
{
$strBody .= &quot;<tr><td>Type: </td><td><select name=lstPrinterType><option>Colour</option><option>B&W</option></select></td></tr>&quot;;
$strBody .= &quot;<tr><td>Ram: </td><td><input type=text name=txtSpec1></td></tr>&quot;;
$strBody .= &quot;<tr><td>Ink Type: </td><td><input type=text name=txtSpec2></td></tr>&quot;;
$strBody .= &quot;<tr><td><input type=hidden name=txtSpec3 value=''></td></tr>&quot;;
$strBody .= &quot;<tr><td><input type=hidden name=txtSpec4 value=''></td></tr>&quot;;
$strBody .= &quot;<tr><td><input type=hidden name=txtSpec5 value=''></td></tr>&quot;;
$strBody .= &quot;<tr><td><input type=hidden name=txtSpec6 value=''></td></tr>&quot;;
$strBody .= &quot;<tr><td><input type=hidden name=txtSpec7 value=''></td></tr>&quot;;
$strBody .= &quot;<tr><td><input type=submit name=btnNewItem value=Submit></td></tr>&quot;;

}// End of If

else if ($strType == &quot;General&quot;)
{
$strBody .= &quot;<tr><td>Spec 1: </td><td><input type=text name=txtSpec1></td></tr>&quot;;
$strBody .= &quot;<tr><td>Spec 2: </td><td><input type=text name=txtSpec2></td></tr>&quot;;
$strBody .= &quot;<tr><td>Spec 3: </td><td><input type=text name=txtSpec3></td></tr>&quot;;
$strBody .= &quot;<tr><td>Spec 4: </td><td><input type=text name=txtSpec4></td></tr>&quot;;
$strBody .= &quot;<tr><td>Spec 5: </td><td><input type=text name=txtSpec5></td></tr>&quot;;
$strBody .= &quot;<tr><td>Spec 6: </td><td><input type=text name=txtSpec6></td></tr>&quot;;
$strBody .= &quot;<tr><td>Spec 7: </td><td><input type=text name=txtSpec7></td></tr>&quot;;
$strBody .= &quot;<tr><td><input type=submit name=btnNewItem value=Submit></td></tr>&quot;;

}// End of If

$strBody .= &quot;</form></table>&quot;;
?>

}// End of preload()

function closeLookupWindow()
{
if(lookupWindow != null)
{
if(lookupWindow.closed != true)
{
lookupWindow.close();

}// End of If

}// End of If

lookupWindow = null;

}// End of closeLookupWindow()

function lookup(strType)
{
closeLookupWindow();

var option;

if(strType == &quot;make&quot;)
{
var option = document.forms[0].lstType.value;

}// End of If

else if(strType == &quot;model&quot;)
{
var option = document.forms[0].txtMake.value;

}// End of If

url = &quot;script/lookup.php?strWindowType=&quot; + strType + &quot;&strOption=&quot; + option;
strOptions = &quot;width=300 ,height=300 ,resizable=no, scrollbars=yes&quot;;
lookupWindow = open(url, null, strOptions);

}// End of lookupRoom()

function reloadPage()
{
parent.Main.location.href=&quot;newHWItem.php?strType=&quot; + document.forms[0].lstType.value;

}// End of reloadPage()

</script>
</head>
<body bgcolor=&quot;#DDDDDD&quot;>
<?
echo $strBody;

?>
</body>
</html>

/*//////////////////
End of newHWItem.php
/*//////////////////

And the one that won't work,

/*/////////
hwSpecs.php
/*/////////

<html>
<head>
<script language=&quot;JAVASCRIPT&quot;>
function preload()
{
<?
$intInfoNo = $_GET[&quot;intInfoNo&quot;];

include(&quot;/vweb/stockcontrol/html/script/hwController.php&quot;);
$serialArray = getSerials($intInfoNo);
$strCurrentSerial = $_GET[&quot;strCurrentSerial&quot;];
$strIsEdit = $_GET[&quot;strIsEdit&quot;];

if ($strCurrentSerial == &quot;&quot;)
{
$strCurrentSerial = $serialArray[0];

}// End of If

$strType = getHWType($intInfoNo);
$specArray = getSpecs($strCurrentSerial);

$strBody = &quot;&quot;;
global $strBody;
global $intInfoNo;
global $serialArray;
global $strCurrentSerial;
global $strType;


$strBody .= &quot;
<table border=\&quot;0\&quot;>
<form method=\&quot;post\&quot; target=\&quot;Foot\&quot; enctype=\&quot;multipart/form-data\&quot;>
<tr>
<td bgcolor='#EFEFEF'>Serial:</td>
&quot;;

if ($strType == &quot;PC/Mac&quot;)
{
$strBody .= &quot;<td bgcolor='#EFEFEF'>Processor: </td>&quot;;
$strBody .= &quot;<td bgcolor='#EFEFEF'>Ram: </td>&quot;;
$strBody .= &quot;<td bgcolor='#EFEFEF'>Hard Disk: </td>&quot;;
$strBody .= &quot;<td bgcolor='#EFEFEF'>Periph 1: </td>&quot;;
$strBody .= &quot;<td bgcolor='#EFEFEF'>Periph 2: </td>&quot;;
$strBody .= &quot;<td bgcolor='#EFEFEF'>Periph 3: </td>&quot;;
$strBody .= &quot;<td bgcolor='#EFEFEF'>Monitor: </td>&quot;;

}// End of If

else if ($strType == &quot;Printer&quot;)
{
$strBody .= &quot;<td bgcolor='#EFEFEF'>Type: </td>&quot;;
$strBody .= &quot;<td bgcolor='#EFEFEF'>Ram: </td>&quot;;
$strBody .= &quot;<td bgcolor='#EFEFEF'>Ink Type: </td>&quot;;
$strBody .= &quot;<td bgcolor='#EFEFEF'>&nbsp</td>&quot;;
$strBody .= &quot;<td bgcolor='#EFEFEF'>&nbsp</td>&quot;;
$strBody .= &quot;<td bgcolor='#EFEFEF'>&nbsp</td>&quot;;
$strBody .= &quot;<td bgcolor='#EFEFEF'>&nbsp</td>&quot;;

}// End of If

else if ($strType == &quot;General&quot;)
{
$strBody .= &quot;<td bgcolor='#EFEFEF'>Spec 1: </td>&quot;;
$strBody .= &quot;<td bgcolor='#EFEFEF'>Spec 2: </td>&quot;;
$strBody .= &quot;<td bgcolor='#EFEFEF'>Spec 3: </td>&quot;;
$strBody .= &quot;<td bgcolor='#EFEFEF'>Spec 4: </td>&quot;;
$strBody .= &quot;<td bgcolor='#EFEFEF'>Spec 5: </td>&quot;;
$strBody .= &quot;<td bgcolor='#EFEFEF'>Spec 6: </td>&quot;;
$strBody .= &quot;<td bgcolor='#EFEFEF'>Spec 7: </td>&quot;;

}// End of If

$strBody .= &quot;<td bgcolor='#EFEFEF'>Status</td>&quot;;
$strBody .= &quot;<td bgcolor='#EFEFEF'>&nbsp</td>&quot;;

if($strIsEdit == &quot;TRUE&quot;)
{
$strBody .= &quot;
</tr>
<tr>
<td bgcolor='#EEEEEE'><input type=text name=txtSerial value=$strCurrentSerial></td>
<td bgcolor='#EEEEEE'><input type=text name=txtSpec0 value=$specArray0></td>
<td bgcolor='#EEEEEE'><input type=text name=txtSpec1 value=$specArray1></td>
<td bgcolor='#EEEEEE'><input type=text name=txtSpec2 value=$specArray2></td>
<td bgcolor='#EEEEEE'><input type=text name=txtSpec3 value=$specArray3></td>
<td bgcolor='#EEEEEE'><input type=text name=txtSpec4 value=$specArray4></td>
<td bgcolor='#EEEEEE'><input type=text name=txtSpec5 value=$specArray5></td>
<td bgcolor='#EEEEEE'><input type=text name=txtSpec6 value=$specArray6></td>
<td bgcolor='#EEEEEE'><input type=text name=txtSpec7 value=$specArray7></td>
<input type=hidden name=strType value=$strType>
<td><input type=submit name=btnEditHW value=Edit></td>
</tr>
&quot;;

}// End of If

else
{
$strBody .= &quot;
</tr>
<tr>
<td>
<select name=\&quot;lstSerial\&quot; onChange='javascript:reloadPage();'>
<option>$strCurrentSerial</option>
<option disabled>-----------</option>
&quot;;

for($i=0;$i<count($serialArray);$i++)
{
$strBody .= &quot;<option>$serialArray[$i]</option>&quot;;

}// End of For

$strBody .= &quot; </select>
</td>
<td bgcolor='#EEEEEE'>$specArray[0]<input type=hidden name=specArray0 value=$specArray[0]></td>
<td bgcolor='#EEEEEE'>$specArray[1]<input type=hidden name=specArray1 value=$specArray[1]></td>
<td bgcolor='#EEEEEE'>$specArray[2]<input type=hidden name=specArray2 value=$specArray[2]></td>
<td bgcolor='#EEEEEE'>$specArray[3]<input type=hidden name=specArray3 value=$specArray[3]></td>
<td bgcolor='#EEEEEE'>$specArray[4]<input type=hidden name=specArray4 value=$specArray[4]></td>
<td bgcolor='#EEEEEE'>$specArray[5]<input type=hidden name=specArray5 value=$specArray[5]></td>
<td bgcolor='#EEEEEE'>$specArray[6]<input type=hidden name=specArray6 value=$specArray[6]></td>
<td bgcolor='#EEEEEE'>$specArray[7]<input type=hidden name=specArray7 value=$specArray[7]></td>
<input type=hidden name=strType value=$strType>
<td><input type=button value=Edit onClick='javascript:loadEditPage();'></td>
</tr>
&quot;;

}// End of Else

$strBody .= &quot;</form></table>&quot;;
?>

}// End of preload()

function reloadPage()
{
alert(&quot;reloadPage&quot;);
parent.Foot.location.href=&quot;hwSpecs.php?intInfoNo=&quot; + $intInfoNo + &quot;&strCurrentSerial=&quot; + document.forms[0].lstSerial.value;

}// End of reloadPage()

function loadEditPage()
{
alert(&quot;loadEditPage&quot;);
parent.Foot.location.href=&quot;hwSpecs.php?intInfoNo=&quot; + $intInfoNo + &quot;&strCurrentSerial=&quot; + document.forms[0].lstSerial.value&strIsEdit=\&quot;TRUE\&quot;;

}// End of loadEditPage()

</script>
</head>
<body bgcolor=&quot;#DDDDDD&quot;>
<?
echo $strBody;

?>
</body>
</html>
 
Ok, I will admit to not having read all of the code you posted. Maybe just post the calls to the functions and the functions themselves? Anyway, I would guess that the issue has to do with the fact that you can't call a php function once the page is loaded without refreshing the entire page. All php is evaluated before a page loads. In order to access the php engine again you have to call it be loading another page or refreshing the current one.
 
The problem is that the function is in javascript and calls a reload on the page - Sorry bout all the code, just posted it so you could see the relevance of what I was doing - heres the relevant function:

<select name=\&quot;lstSerial\&quot; onChange='javascript:reloadPage();'>

function reloadPage()
{
alert(&quot;reloadPage&quot;);
parent.Foot.location.href=&quot;hwSpecs.php?intInfoNo=&quot; + $intInfoNo + &quot;&strCurrentSerial=&quot; + document.forms[0].lstSerial.value;

}// End of reloadPage()
 
It is not clear to me what your question is.

Are the $_GET vars available in the called script?
What is the trouble with the JavaScript?
 
If You look at both files, they are very similar. What is happening is that the second one does not seem to let me call the javascript functions. Javascript code works:

<select name='a' onChange='JAVASCRIPT:alert(&quot;TEST&quot;);'>

however when I do this I get nothing.:

<select name='a' onChange='JAVASCRIPT:reloadPage();'>
function reloadPage()
{
alert(&quot;reloadPage&quot;);
parent.Foot.location.href=&quot;hwSpecs.php?intInfoNo=&quot; + $intInfoNo + &quot;&strCurrentSerial=&quot; + document.forms[0].lstSerial.value;

}// End of reloadPage()
 
please do a view source of the page and post it here...

Known is handfull, Unknown is worldfull
 
Heres the source view:

<html>
<head>
<script language=&quot;JAVASCRIPT&quot;>
function preload()
{

}// End of preload()

function reloadPage()
{
alert(&quot;reloadPage&quot;);
parent.Foot.location.href=&quot;hwSpecs.php?intInfoNo=&quot; + $intInfoNo + &quot;&strCurrentSerial=&quot; + document.forms[0].lstSerial.value;

}// End of reloadPage()

function loadEditPage()
{
alert(&quot;loadEditPage&quot;);
parent.Foot.location.href=&quot;hwSpecs.php?intInfoNo=&quot; + $intInfoNo + &quot;&strCurrentSerial=&quot; + document.forms[0].lstSerial.value&strIsEdit=\&quot;TRUE\&quot;;

}// End of loadEditPage()

</script>
</head>
<body bgcolor=&quot;#DDDDDD&quot;>

<table border=&quot;0&quot;>
<form method=&quot;post&quot; target=&quot;Foot&quot; enctype=&quot;multipart/form-data&quot;>
<tr>
<td bgcolor='#EFEFEF'>Serial:</td>

<td bgcolor='#EFEFEF'>Processor: </td><td bgcolor='#EFEFEF'>Ram: </td><td bgcolor='#EFEFEF'>Hard Disk: </td><td bgcolor='#EFEFEF'>Periph 1: </td><td bgcolor='#EFEFEF'>Periph 2: </td><td bgcolor='#EFEFEF'>Periph 3: </td><td bgcolor='#EFEFEF'>Monitor: </td><td bgcolor='#EFEFEF'>Status</td><td bgcolor='#EFEFEF'>&nbsp</td>
</tr>
<tr>
<td>
<select name=&quot;lstSerial&quot; onChange='javascript:reloadPage();'>

<option>a</option>
<option disabled>-----------</option>
<option>a</option><option>b</option><option>c</option><option>d</option><option>e</option> </select>
</td>
<td bgcolor='#EEEEEE'>P4 2.5GHz<input type=hidden name=specArray0 value=P4 2.5GHz></td>

<td bgcolor='#EEEEEE'>512<input type=hidden name=specArray1 value=512></td>
<td bgcolor='#EEEEEE'>40 Gb<input type=hidden name=specArray2 value=40 Gb></td>
<td bgcolor='#EEEEEE'>DVD/CD-RW<input type=hidden name=specArray3 value=DVD/CD-RW></td>
<td bgcolor='#EEEEEE'>ZIP 750<input type=hidden name=specArray4 value=ZIP 750></td>
<td bgcolor='#EEEEEE'><input type=hidden name=specArray5 value=></td>
<td bgcolor='#EEEEEE'>19<input type=hidden name=specArray6 value=19></td>

<td bgcolor='#EEEEEE'>FAULTY<input type=hidden name=specArray7 value=FAULTY></td>
<input type=hidden name=strType value=PC/Mac>
<td><input type=button value=Edit onClick='javascript:loadEditPage();'></td>
</tr>
</form></table> </body>
</html>
 
try this:
function reloadPage()
{
alert(&quot;reloadPage&quot;);
parent.Foot.location.href=&quot;hwSpecs.php?intInfoNo=&quot; + <?=$intInfoNo?> + &quot;&strCurrentSerial=&quot; + document.forms[0].lstSerial.value;

}// End of reloadPage()


function loadEditPage()
{
alert(&quot;loadEditPage&quot;);
parent.Foot.location.href=&quot;hwSpecs.php?intInfoNo=&quot; + <?=$intInfoNo?> + &quot;&strCurrentSerial=&quot; + document.forms[0].lstSerial.value&strIsEdit=TRUE&quot;;

}// End of loadEditPage()


Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top