{"id":1208,"date":"2022-08-30T15:14:11","date_gmt":"2022-08-30T15:14:11","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/10\/sending-a-global-variable-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:14:11","modified_gmt":"2022-08-30T15:14:11","slug":"sending-a-global-variable-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/sending-a-global-variable-collection-of-common-programming-errors\/","title":{"rendered":"Sending a global variable-Collection of common programming errors"},"content":{"rendered":"<p>I have a piece of Javascript that forwards a users&#8217; selected information to an external PHP file, and returns information. In the code below, you can see it sends {&#8216;report&#8217; : report} via POST to that file. That works fine.<\/p>\n<p>Essentially I need to add another variable to be sent. It&#8217;s called &#8216;id&#8217;, but it&#8217;s in another function. Is there a way to make that variable global and then incorporate it so it&#8217;s sent in my code snippet? (and when will the global variable be cleared?)I can also send it via the &#8216;url&#8217; attribute, and use GET in my PHP&#8230;just not sure how to implement.<\/p>\n<pre><code>$('#adminSelectReport a').live(\"click\", function () {\n    \/\/Get id from clicked link:\n    var report = $(this).attr('id');\n\n    $.ajax({\n        type: 'POST',\n        url: 'getReportInfo.php',\n        data: {\n            'report': report\n        },\n        success: function (msg) {\n            \/\/everything echoed in your PHP-File will be in the 'msg' variable:\n            $('#adminReportInfo').html(msg);\n            $('#adminReportInfo').fadeIn(400);\n        }\n    });\n});\n<\/code><\/pre>\n<p><strong>UPDATE<\/strong>: Here is the other snippet that sends &#8216;id&#8217; to another page, getting information. I need to retain this ID, however, and use it on my original code.<\/p>\n<pre><code>$('#adminSelectCompany a').click(function() {\n    \/\/Get id from clicked link:\n    var id = $(this).attr('id');\n\n    $.ajax({\n        type: 'POST',\n        url: 'getReports.php',\n        data: {'id': id},\n        success: function(msg){\n            \/\/everything echoed in your PHP-File will be in the 'msg' variable:\n            $('#adminSelectReport').html(msg);\n            $('#adminSelectReport').fadeIn(400);\n           $('#adminReportInfo').fadeOut(300);\n\n        }\n});\n    });\n<\/code><\/pre>\n<ol>\n<li>\n<p>So it sounds like they select a Company via a link, then they select a Report via another link, and you need to remember which Company was selected.<\/p>\n<p>In order to avoid global variables, I&#8217;d probably just add a class to the selected Company link, and then fetch that element by the selected class, and grab its ID. You could use the class for styling as well if that&#8217;s needed.<\/p>\n<pre><code>var companies = $('#adminSelectCompany a');\n\ncompanies.click(function () {\n\n      \/\/ remove class from previously selected, and add to new one\n    companies.filter('.selected').removeClass('selected');\n    $(this).addClass('selected');\n\n    $.ajax({\n        type: 'POST',\n        url: 'getReports.php',\n        data: {\n            'id': this.id\n        },\n        success: function (msg) {\n            \/\/everything echoed in your PHP-File will be in the 'msg' variable:\n            $('#adminSelectReport').html(msg)\n                                   .fadeIn(400);\n            $('#adminReportInfo').fadeOut(300);\n\n        }\n    });\n});\n<\/code><\/pre>\n<pre><code>$('#adminSelectReport a').live(\"click\", function () {\n\n    $.ajax({\n        type: 'POST',\n        url: 'getReportInfo.php',\n        data: {\n            'report': this.id,\n            'company': $('.selected')[0].id\n        },\n        success: function (msg) {\n            \/\/everything echoed in your PHP-File will be in the 'msg' variable:\n            $('#adminReportInfo').html(msg);\n            $('#adminReportInfo').fadeIn(400);\n        }\n    });\n});\n<\/code><\/pre>\n<\/li>\n<li>\n<p>You can achieve this by assigning the value as a property of JavaScripts &#8220;global&#8221; namespace called window. Simply assign the id you want to make global to <code>window.my_id<\/code>, then refer to it in the click callback.<\/p>\n<p>Note: If you&#8217;re setting the global variable in another function, remember to check for its existance in the function that will use the variable, ie: <code>var my_id = null; if (window.my_id != undefined) { my_id = window.my_id; }<\/code><\/p>\n<p>Here&#8217;s an implementation:<\/p>\n<pre><code>$('#adminSelectReport a').live(\"click\", function () {\n    \/\/Get id from clicked link:\n    var report = $(this).attr('id');\n    var company = window.report_company_id != undefined ? window.report_company_id : null;\n\n    $.ajax({\n        type: 'POST',\n        url: 'getReportInfo.php',\n        data: {\n            'report': report,\n            'company': company\n        },\n        success: function (msg) {\n            \/\/everything echoed in your PHP-File will be in the 'msg' variable:\n            $('#adminReportInfo').html(msg);\n            $('#adminReportInfo').fadeIn(400);\n        }\n    });\n});\n<\/code><\/pre>\n<p>.<\/p>\n<pre><code>$('#adminSelectCompany a').click(function() {\n    \/\/Get id from clicked link:\n    var id = $(this).attr('id');\n\n    window.report_company_id = id;\n\n    $.ajax({\n        type: 'POST',\n        url: 'getReports.php',\n        data: {'id': id},\n        success: function(msg){\n            \/\/everything echoed in your PHP-File will be in the 'msg' variable:\n            $('#adminSelectReport').html(msg);\n            $('#adminSelectReport').fadeIn(400);\n           $('#adminReportInfo').fadeOut(300);\n\n        }\n});\n    });\n<\/code><\/pre>\n<p>Lastly I would advise against global variables if possible, or at least minimize the usage by wrapping common function\/purposes in objects and prefixing the names with the project name or something.<\/p>\n<\/li>\n<li>\n<p>Change<\/p>\n<pre><code>data: { 'report': report },\n<\/code><\/pre>\n<p>To<\/p>\n<pre><code>data{ 'report': report, 'id': YOUR ID },\n<\/code><\/pre>\n<\/li>\n<li>\n<p>Why don&#8217;t you send the second variable like that:<\/p>\n<pre><code> data: {'report': report, 'id': your_id },\n<\/code><\/pre>\n<p>edit: arf too slow!<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-10 00:09:54. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I have a piece of Javascript that forwards a users&#8217; selected information to an external PHP file, and returns information. In the code below, you can see it sends {&#8216;report&#8217; : report} via POST to that file. That works fine. Essentially I need to add another variable to be sent. It&#8217;s called &#8216;id&#8217;, but it&#8217;s [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1208","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1208","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1208"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1208\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1208"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1208"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1208"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}