How to recall something on a separate page

I don't know if this is possible, but on this page I have this code below:

$moduleHTML = ""; while($sqlstmt->fetch()) { $moduleHTML .= "<option value='$dbModuleId'>" . $dbModuleId . " - " . $dbModuleName . "</option>".PHP_EOL; $outputmodule = ""; $outputmodule .= "<p><strong>Module:</strong> " . $dbModuleId . " - " . $dbModuleName . "</p>"; } 

But I want to repeat the $outputmodule variable on another page. So, below is the code for another page, including ajax, which links the script above (module.php) and where it contains the echo and where it contains part of the module drop-down menu:

  <script type="text/javascript"> function getModules() { var course = jQuery("#coursesDrop").val(); jQuery('#modulesDrop').empty(); jQuery('#modulesDrop').html('<option value="">Please Select</option>'); jQuery.ajax({ type: "post", url: "module.php", data: { course:course }, success: function(response){ jQuery('#modulesDrop').append(response); } }); } </script> ... <? $moduleHTML = ""; $moduleHTML .= '<select name="modules" id="modulesDrop">'.PHP_EOL; $moduleHTML .= '<option value="">Please Select</option>'.PHP_EOL; $moduleHTML .= '</select>'; echo $outputmodule; ... 

The problem is that I get an undefined error for the echo above. My question is, how can I echo the $dbModuleId and $dbModuleName from the module.php page to the editsession.php page (script above)?

+4
source share
2 answers

If you don't want to use sessions, you can pass your values ​​as a json array, then decode it and add your values ​​with jquery

  $outputArray = array(); while($sqlstmt->fetch()) { $outputArray['moduleHTML'] = "<option value='$dbModuleId'>" . $dbModuleId . " - " . $dbModuleName . "</option>".PHP_EOL; $outputArray['outputModule'] = "<p><strong>Module:</strong> " . $dbModuleId . " - " . $dbModuleName . "</p>"; } echo json_encode($outputArray); 

Then on your page

 <script type="text/javascript"> function getModules() { var course = jQuery("#coursesDrop").val(); jQuery('#modulesDrop').empty(); jQuery('#modulesDrop').html('<option value="">Please Select</option>'); jQuery.ajax({ type: "post", url: "module.php", data: { course:course }, success: function(response){ var outputjQuery.parseJSON(response) jQuery('#modulesDrop').append(output.moduleHTML); jQuery('#outputModule').html(output.outputModule); }); }); } </script> ... <? $moduleHTML = ""; $moduleHTML .= '<select name="modules" id="modulesDrop">'.PHP_EOL; $moduleHTML .= '<option value="">Please Select</option>'.PHP_EOL; $moduleHTML .= '</select>'; $moduleHTML .= '<div id="outPutModule"></div>; echo $moduleHTML; 
0
source

Are php scripts called during different requests or are they included in each other? If there are different requests, you can save the variable in $ _SESSION and get it between requests. If in the same query the variable will be available either in the same scope, or you can use the global keyword for sharing in scope.

http://php.net/manual/en/features.sessions.php

http://php.net/manual/en/language.variables.scope.php

Globals are usually not recommended. And there is an array of arrays. It just depends on whether you are using the same query.

0
source

Source: https://habr.com/ru/post/1443132/


All Articles