Jump to content

Call PHP Function from within another function


reptile

Recommended Posts

I have a dropdown menu created from javascript, so the onchange event should call a function called updateForBodyfittingoptions(). The thing here is that this function updateForBodyfittingoptions() should actually run dependent on a loop that is part of another function called UpdateDivision.

So if I were to put the function UpdateBodyfitting directly within the loop of the other function updateSelectClothByDiv(division) then I get "Object Expected".

 

Remember the onchange calls the UpdateBodyfitting function.

Here is the code which I've shortened for purposes of posting on here, I've also attached the main php file with all the code just in case anyone wants to reference it.
, the main function is "generateUpdateSelectClothByDiv", within this function are "updateForBodyfittingoptions" and "updateSelectClothByDiv(division)".   Really appreciate any help I can get, thanks in advance.

 

function generateUpdateSelectClothByDiv() {

global $connect,$hriconnect,$division_array,$cloth_array,$lining_array,$lining_sleeve_array,$bodyfitting_array,$pantstyle_array,$veststyle_array,$glob_div;
$indexdiv=1; $indexbody=1;
//global $field1_array,$field2_array,$field4_array,$extras_array,$field5_array;

// echo "\t\tdocument.pickDivision.bodyfitting.selectedIndex=-1;\n";
$cloth_numrows = 0;
$clothArray = array();
$div_query = "SELECT distinct DIVISION, CLOTHDB FROM MTM_DIVISIONS_S ORDER BY CLOTHDB";
$div_result = oci_parse($connect,$div_query);
oci_execute($div_result);
while ($div_row = oci_fetch_array($div_result, OCI_ASSOC)) {
$divArray[] = "{$div_row['CLOTHDB']}";
$divDivArray[] = "{$div_row['DIVISION']}";
}

oci_free_statement($div_result);
// print("//TEST\n\n");
echo "\tfunction updateSelectClothByDiv(division)\n";
echo "\t{\n";
$first = 0;

echo "\tClearOptionsFastAlt('cloth');\n";
echo "\t\tdocument.pickDivision.textInput.value='';\n";
echo "var divcomp = division.replace(/^\s+|\s+$/g, '');";
//echo "alert(divcomp)";
for ($i = 0; $i < sizeof($divArray); $i++) {
$cloth_query="SELECT CLOTH, CODE FROM ".$divArray[$i]." where CODE <>'OUT' or CODE is null ORDER BY cloth";

$cloth_result = oci_parse($connect,$cloth_query);
oci_execute($cloth_result);
$cloth_numrows = count_rows($connect,$cloth_query);
$maxclothrows=$cloth_numrows+1;

$bodyfitting_query="SELECT BODYFITTING, BFCODE FROM MTM_STYLES_S WHERE DIVISION= '".$divDivArray[$i]."' AND STYLE_TYPE='BODY' GROUP BY BODYFITTING, BFCODE ORDER BY BODYFITTING";
$bodyfitting_result = oci_parse($connect,$bodyfitting_query);
oci_execute($bodyfitting_result);
$bodyfitting_numrows = count_rows($connect,$bodyfitting_query);
$maxclothrows=$bodyfitting_numrows+1;


if($first == 0) {
$first++;
} else {
echo "\telse\t";
}
//echo "\talert('$divArray[$i]' +' '+ '$divDivArray[$i]'+' :aaa:' + divcomp);";
echo "\tif (divcomp == \"{$divDivArray[$i]}\") {";

$y=1;
echo "var selectObj = document.pickDivision.cloth;\n";
echo "var numShown = selectObj.options.length;\n";
echo "selectObj.selectedIndex = -1;\n";
echo "\t\t\tselectObj.options[numShown] = new Option('- Select Cloth -', '');\n";
echo "\t\t\tnumShown++;\n";
while ($cloth_row = oci_fetch_array($cloth_result, OCI_ASSOC)) {
$newCloth=$cloth_row['CODE'];
$newStyle=$cloth_row['CLOTH'];
echo "\t\t\tselectObj.options[numShown] = new Option('".$newStyle.' '.$newCloth."', '".$newStyle."');\n";
echo "\t\t\tnumShown++;\n";
$y++;
}
echo "\t\t\tdocument.pickDivision.cloth.options[0].selected = true;\n\n";

echo "\tClearOptionsFastAlt('lining');\n";
echo "\t\tdocument.pickDivision.textInput.value='';\n";
echo "var divcomp = division.replace(/^\s+|\s+$/g, '');";
echo "var selectObj = document.pickDivision.lining;\n";
echo "var numShown = selectObj.options.length;\n";
echo "selectObj.selectedIndex = -1;\n";
echo "\t\t\tselectObj.options[numShown] = new Option('- Select Lining -', '');\n";
echo "\t\t\tnumShown++;\n";
while ($lining_row = oci_fetch_array($lining_result, OCI_ASSOC)) {
$newLining=$lining_row['CLOTH'];
echo "\t\t\tselectObj.options[numShown] = new Option('".$newLining."' ,'".$newLining."');\n";
echo "\t\t\tnumShown++;\n";
$y++;
}
echo "\t\t\tdocument.pickDivision.lining.options[0].selected = true;\n\n";


//Setup new dropdown for Style Selection
echo "\tClearOptionsFastAlt('bodyfitting');\n";
echo "\t\tdocument.pickDivision.textInput.value='';\n";
echo "var divcomp = division.replace(/^\s+|\s+$/g, '');";
echo "var selectObj = document.pickDivision.bodyfitting;\n";
echo "var numShown = selectObj.options.length;\n";
echo "selectObj.selectedIndex = -1;\n";
echo "\t\t\tselectObj.options[numShown] = new Option('- Select Style -', '');\n";
echo "\t\t\tnumShown++;\n";
while ($bodyfitting_row = oci_fetch_array($bodyfitting_result, OCI_ASSOC)) {
$newBodyfitting=$bodyfitting_row['BODYFITTING'];
$newBfcode=$bodyfitting_row['BFCODE'];
$bodyfitArray[] = "{$bodyfitting_row['BFCODE']}";
$bodyfitbodyarray[]="{$bodyfitting_row['BODYFITTING']}";
echo "\t\t\tselectObj.options[numShown] = new Option('".$newBfcode.' '.$newBodyfitting."', '".$newBfcode."');\n";
echo "\t\t\tnumShown++;\n";
$y++;

echo "\tfunction updateForBodyfittingoptions()\n";
echo "\t{\n";


echo "\t\tif(document.pickDivision.bodyfitting.options.value == '{$bodyfitting_row['BFCODE']} {$bodyfitting_row['BODYFITTING']}')\n";
echo "\t\t{\n";

//Show selection for front
$field1_query = "SELECT MTM_STYLES_S.CODE,MTM_SUFFEX_S.TEXT FROM MTM_STYLES_S,MTM_SUFFEX_S WHERE MTM_SUFFEX_S.DIVISION='".$divDivArray[$i]."' AND (MTM_STYLES_S.FIELD=MTM_SUFFEX_S.FIELD AND MTM_STYLES_S.CODE=MTM_SUFFEX_S.CODE) AND MTM_STYLES_S.STYLE_TYPE='BODY' AND MTM_STYLES_S.BODYFITTING='".$bodyfitting_row['BODYFITTING']."' AND MTM_STYLES_S.FIELD=1 ORDER BY MTM_STYLES_S.FIELD,MTM_STYLES_S.CODE";
$field1_result = oci_parse($connect,$field1_query);
oci_execute($field1_result);
echo "\t\t\tdocument.pickDivision.field1.options[0].value = '';\n";
echo "\t\t\tdocument.pickDivision.field1.options[0].text = '- Select Frontaaa';\n\n";
$y=1;


while ($field1_row = oci_fetch_array($field1_result, OCI_ASSOC)) {
$newField1=$field1_row['TEXT'];
$newField1Code=$field1_row['CODE'];
$bodyfitArray[] = "{$bodyfitting_row['BFCODE']}";
$bodyfitbodyarray[]="{$bodyfitting_row['BODYFITTING']}";
echo "\t\t\tselectObj.options[numShown] = new Option('".$newField1Code.' '.$newField1."', '".$newField1Code."');\n";
echo "\t\t\tnumShown++;\n";
$y++;
}
echo "\t\t\tdocument.pickDivision.field1.options[0].selected=true;\n\n";
$indexfield1=1;
oci_free_statement($field1_result);



echo "\t\t}\n\n";
echo "\t\t}\n\n";
}
echo "\t\t\tdocument.pickDivision.bodyfitting.options[0].selected=true;\n\n";


echo "\t}\n\n";


oci_free_statement($bodyfitting_result);
$indexbody=1;


oci_free_statement($cloth_result);

}
$indexdiv++;
echo "\t\treturn true;\n";
echo "\t}\n\n";



oci_close($connect);

}

mtm.php

Link to comment
Share on other sites

...okay.

 

1. JavaScript cannot invoke a PHP function unless you use AJAX.  PHP runs first on the server, and is done running once the page is rendered.  Even if you're using PHP to output your JavaScript.

2. You should never, ever, ever use 'global' in your PHP code.

3. When posting code on these forums, please use the code BBCode tags.  You can do it manually, by placing


around your code, or by pressing the <> button in the editor.

4. We don't generally look at attachments here.  So, if you could highlight just the parts that aren't working (your JS function, where you're invoking it, your PHP), that would help.  And, please keep it to just the relevant bits.

Link to comment
Share on other sites

Hi,  I've highlighted the code in red below, it is not showing the output, although it does show the results when I view the source of the actual webpage (so looks to work), it should show a dropdown menu beginning with 'Select Front'  but I am getting an "objected expected" error, also this function "Updateforbodyfittingoptions" is called at the onchange event.

I have a dropdown menu created from javascript, so the onchange event should call a function called updateForBodyfittingoptions(). The thing here is that this function updateForBodyfittingoptions() should actually run dependent on a loop that is part of another function called UpdateDivision.

So if I were to put the function UpdateBodyfitting directly within the loop of the other function updateSelectClothByDiv(division) then I get "Object Expected".

 

Remember the onchange calls the UpdateBodyfitting function.

Here is the code which I've shortened for purposes of posting on here, I've also attached the main php file with all the code just in case anyone wants to reference it.
, the main function is "generateUpdateSelectClothByDiv", within this function are "updateForBodyfittingoptions" and "updateSelectClothByDiv(division)".   Really appreciate any help I can get, thanks in advance.

 

function generateUpdateSelectClothByDiv() {

global $connect,$hriconnect,$division_array,$cloth_array,$lining_array,$lining_sleeve_array,$bodyfitting_array,$pantstyle_array,$veststyle_array,$glob_div;
$indexdiv=1; $indexbody=1;
//global $field1_array,$field2_array,$field4_array,$extras_array,$field5_array;

// echo "\t\tdocument.pickDivision.bodyfitting.selectedIndex=-1;\n";
$cloth_numrows = 0;
$clothArray = array();
$div_query = "SELECT distinct DIVISION, CLOTHDB FROM MTM_DIVISIONS_S ORDER BY CLOTHDB";
$div_result = oci_parse($connect,$div_query);
oci_execute($div_result);
while ($div_row = oci_fetch_array($div_result, OCI_ASSOC)) {
$divArray[] = "{$div_row['CLOTHDB']}";
$divDivArray[] = "{$div_row['DIVISION']}";
}

oci_free_statement($div_result);
// print("//TEST\n\n");
echo "\tfunction updateSelectClothByDiv(division)\n";
echo "\t{\n";
$first = 0;

echo "\tClearOptionsFastAlt('cloth');\n";
echo "\t\tdocument.pickDivision.textInput.value='';\n";
echo "var divcomp = division.replace(/^\s+|\s+$/g, '');";
//echo "alert(divcomp)";
for ($i = 0; $i < sizeof($divArray); $i++) {
$cloth_query="SELECT CLOTH, CODE FROM ".$divArray[$i]." where CODE <>'OUT' or CODE is null ORDER BY cloth";

$cloth_result = oci_parse($connect,$cloth_query);
oci_execute($cloth_result);
$cloth_numrows = count_rows($connect,$cloth_query);
$maxclothrows=$cloth_numrows+1;

$bodyfitting_query="SELECT BODYFITTING, BFCODE FROM MTM_STYLES_S WHERE DIVISION= '".$divDivArray[$i]."' AND STYLE_TYPE='BODY' GROUP BY BODYFITTING, BFCODE ORDER BY BODYFITTING";
$bodyfitting_result = oci_parse($connect,$bodyfitting_query);
oci_execute($bodyfitting_result);
$bodyfitting_numrows = count_rows($connect,$bodyfitting_query);
$maxclothrows=$bodyfitting_numrows+1;


if($first == 0) {
$first++;
} else {
echo "\telse\t";
}
//echo "\talert('$divArray[$i]' +' '+ '$divDivArray[$i]'+' :aaa:' + divcomp);";
echo "\tif (divcomp == \"{$divDivArray[$i]}\") {";

$y=1;
echo "var selectObj = document.pickDivision.cloth;\n";
echo "var numShown = selectObj.options.length;\n";
echo "selectObj.selectedIndex = -1;\n";
echo "\t\t\tselectObj.options[numShown] = new Option('- Select Cloth -', '');\n";
echo "\t\t\tnumShown++;\n";
while ($cloth_row = oci_fetch_array($cloth_result, OCI_ASSOC)) {
$newCloth=$cloth_row['CODE'];
$newStyle=$cloth_row['CLOTH'];
echo "\t\t\tselectObj.options[numShown] = new Option('".$newStyle.' '.$newCloth."', '".$newStyle."');\n";
echo "\t\t\tnumShown++;\n";
$y++;
}
echo "\t\t\tdocument.pickDivision.cloth.options[0].selected = true;\n\n";

echo "\tClearOptionsFastAlt('lining');\n";
echo "\t\tdocument.pickDivision.textInput.value='';\n";
echo "var divcomp = division.replace(/^\s+|\s+$/g, '');";
echo "var selectObj = document.pickDivision.lining;\n";
echo "var numShown = selectObj.options.length;\n";
echo "selectObj.selectedIndex = -1;\n";
echo "\t\t\tselectObj.options[numShown] = new Option('- Select Lining -', '');\n";
echo "\t\t\tnumShown++;\n";
while ($lining_row = oci_fetch_array($lining_result, OCI_ASSOC)) {
$newLining=$lining_row['CLOTH'];
echo "\t\t\tselectObj.options[numShown] = new Option('".$newLining."' ,'".$newLining."');\n";
echo "\t\t\tnumShown++;\n";
$y++;
}
echo "\t\t\tdocument.pickDivision.lining.options[0].selected = true;\n\n";


//Setup new dropdown for Style Selection
echo "\tClearOptionsFastAlt('bodyfitting');\n";
echo "\t\tdocument.pickDivision.textInput.value='';\n";
echo "var divcomp = division.replace(/^\s+|\s+$/g, '');";
echo "var selectObj = document.pickDivision.bodyfitting;\n";
echo "var numShown = selectObj.options.length;\n";
echo "selectObj.selectedIndex = -1;\n";
echo "\t\t\tselectObj.options[numShown] = new Option('- Select Style -', '');\n";
echo "\t\t\tnumShown++;\n";
while ($bodyfitting_row = oci_fetch_array($bodyfitting_result, OCI_ASSOC)) {
$newBodyfitting=$bodyfitting_row['BODYFITTING'];
$newBfcode=$bodyfitting_row['BFCODE'];
$bodyfitArray[] = "{$bodyfitting_row['BFCODE']}";
$bodyfitbodyarray[]="{$bodyfitting_row['BODYFITTING']}";
echo "\t\t\tselectObj.options[numShown] = new Option('".$newBfcode.' '.$newBodyfitting."', '".$newBfcode."');\n";
echo "\t\t\tnumShown++;\n";
$y++;

echo "\tfunction updateForBodyfittingoptions()\n";
echo "\t{\n";



echo "\t\tif(document.pickDivision.bodyfitting.options.value == '{$bodyfitting_row['BFCODE']} {$bodyfitting_row['BODYFITTING']}')\n";
echo "\t\t{\n";

//Show selection for front
$field1_query = "SELECT MTM_STYLES_S.CODE,MTM_SUFFEX_S.TEXT FROM MTM_STYLES_S,MTM_SUFFEX_S WHERE MTM_SUFFEX_S.DIVISION='".$divDivArray[$i]."' AND (MTM_STYLES_S.FIELD=MTM_SUFFEX_S.FIELD AND MTM_STYLES_S.CODE=MTM_SUFFEX_S.CODE) AND MTM_STYLES_S.STYLE_TYPE='BODY' AND MTM_STYLES_S.BODYFITTING='".$bodyfitting_row['BODYFITTING']."' AND MTM_STYLES_S.FIELD=1 ORDER BY MTM_STYLES_S.FIELD,MTM_STYLES_S.CODE";
$field1_result = oci_parse($connect,$field1_query);
oci_execute($field1_result);
echo "\t\t\tdocument.pickDivision.field1.options[0].value = '';\n";
echo "\t\t\tdocument.pickDivision.field1.options[0].text = '- Select Front';\n\n";
$y=1;


while ($field1_row = oci_fetch_array($field1_result, OCI_ASSOC)) {
$newField1=$field1_row['TEXT'];
$newField1Code=$field1_row['CODE'];
$bodyfitArray[] = "{$bodyfitting_row['BFCODE']}";
$bodyfitbodyarray[]="{$bodyfitting_row['BODYFITTING']}";
echo "\t\t\tselectObj.options[numShown] = new Option('".$newField1Code.' '.$newField1."', '".$newField1Code."');\n";
echo "\t\t\tnumShown++;\n";
$y++;

}
echo "\t\t\tdocument.pickDivision.field1.options[0].selected=true;\n\n";
$indexfield1=1;
oci_free_statement($field1_result);



echo "\t\t}\n\n";
echo "\t\t}\n\n";
}
echo "\t\t\tdocument.pickDivision.bodyfitting.options[0].selected=true;\n\n";


echo "\t}\n\n";


oci_free_statement($bodyfitting_result);
$indexbody=1;


oci_free_statement($cloth_result);

}
$indexdiv++;
echo "\t\treturn true;\n";
echo "\t}\n\n";



oci_close($connect);

}

Edited by reptile
Link to comment
Share on other sites

 

also this function "Updateforbodyfittingoptions" is called at the onchange event.

Read Kevins first point.. All what PHP is doing outputting the javascript code for defining the Updateforbodyfittingoptions javascript function, When you call this function the PHP code that defined it will not be called! 

Link to comment
Share on other sites

Read Kevins first point.. All what PHP is doing outputting the javascript code for defining the Updateforbodyfittingoptions javascript function, When you call this function the PHP code that defined it will not be called! 

I see what you are saying, but how come the function "updateSelectClothByDiv(division)" is being called, this is also an onchange event?

Link to comment
Share on other sites

The browser is calling that function. Not PHP.

 

Do you understand the difference between Javascript and PHP? And how/when these two languages are executed?

Maybe I said it incorrectly then but both functions are being called by the browser.  Both at different "Onchange" events. Also yes I know that javascript works on the browser and php on the webserver.

Edited by reptile
Link to comment
Share on other sites

Yes, but what code do you think is being executed when those functions are called using onchange a) Javascript, b) PHP or c) both?

PHP scripts will only be activated when a request is made to the webserver and will cause a page to refresh, so from javscript's onchange event.  But I am just annoyed because one function is called while the other isn't, there is nothing different in the way they are being called.

Link to comment
Share on other sites

 

PHP scripts will only be activated when a request is made to the webserver and will cause a page to refresh, so from javscript's onchange event.

onchange doesn't cause the page to refresh, So no request is being sent to the webserver (unless you have some javascript code that is refreshing the page).

Link to comment
Share on other sites

onchange doesn't cause the page to refresh, So no request is being sent to the webserver (unless you have some javascript code that is refreshing the page).

Yeah, there might be more to it.  PHP isn't something that I have much experience with but this is existing code that I have to modify at work.  Basically this code generates dropdown menus with data, so whenever a change is made in the selection then the function is called to display data in the other dropdown menus.  But the updateforBodyfitting isn't working.  So the "Select Front" changes depending on the selection in the "Select Style".

Edited by reptile
Link to comment
Share on other sites

reptile, trust us, there is no way for PHP and JavaScript to directly interact except through AJAX.  If your JavaScript code isn't using XMLHttpRequest (or, even better, library code that makes it a hell of a lot easier to use out of the gate, like jQuery's $.get()), then it's not talking to the PHP on the server.

 

The thing that looks like it's working is likely being pre-populated with data at the time of page request, with the JavaScript merely manipulating the DOM after it's been rendered.  In other words, it's an illusion, and not working the way you think it's working.

 

So, again, just to drive the point home: no XMLHttpRequest object in your JS code/no using a library that wraps that object (like jQuery) = no direct communication between JS and PHP.

Link to comment
Share on other sites

reptile, trust us, there is no way for PHP and JavaScript to directly interact except through AJAX.  If your JavaScript code isn't using XMLHttpRequest (or, even better, library code that makes it a hell of a lot easier to use out of the gate, like jQuery's $.get()), then it's not talking to the PHP on the server.

 

The thing that looks like it's working is likely being pre-populated with data at the time of page request, with the JavaScript merely manipulating the DOM after it's been rendered.  In other words, it's an illusion, and not working the way you think it's working.

 

So, again, just to drive the point home: no XMLHttpRequest object in your JS code/no using a library that wraps that object (like jQuery) = no direct communication between JS and PHP.

Hmm... I see, I do know that once the page loads and then you select different selections in the dropdown (eg. Select distinct Division...) then the other dropdowns change because they depend on that select statement from the database.  I just can't get the one to work where 'Select Style' selects a style and then it should change the 'Select Front' selections or at least display them which it's not.

Edited by reptile
Link to comment
Share on other sites

There are two possible things happening:

 

1. The other dropdowns' onchange use the XMLHttpRequest object, either directly or indirectly (JavaScript library, like jQuery or any of the others, including those created by other people at your job).  You should be able to determine if that's the case by looking at the generated source and checking for the existence of either a JavaScript library or XMLHttpRequest in the raw itself.

 

OR

 

2. All of the other dropdown data is retrieved at the first page request, and JavaScript is being used to simply hide/display the correct options.  You should be able to determine that's the case by looking at the generated source and checking for all the other dropdown data.

 

So, if it's case 1, you'll have to figure out how to adopt code that's already working and accessing the PHP code for your new dropdown/function.  If it's case 2, you have a decision to make: pre-load all of the data you're concerned with, like the other dropdowns have done, or use AJAX to dynamically retrieve just the set you want.  Either way, your first step is to actually examine what's going on when the dropdowns are played with, and that requires a trip to the generated source.  Simple enough to do with your browser of choice and its web developer tools.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.