{"id":17867,"date":"2022-10-27T12:54:55","date_gmt":"2022-10-27T07:24:55","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-php-update-variable-when-link-is-clicked-closed\/"},"modified":"2022-10-27T12:54:55","modified_gmt":"2022-10-27T07:24:55","slug":"solved-php-update-variable-when-link-is-clicked-closed","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-php-update-variable-when-link-is-clicked-closed\/","title":{"rendered":"[Solved] PHP: Update variable when link is clicked [closed]"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-52229499\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"52229499\" data-parentid=\"52229108\" data-score=\"2\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>Yes it is possible.<\/p>\n<p>One thing you could do, as some people touched in the comments, is to use a <code>GET<\/code> method, which is essentially parsing a variable through a URL string.<\/p>\n<p>Example:<\/p>\n<p><code>&lt;a href=\"https:\/\/stackoverflow.com\/questions\/52229108\/example_page_a.php?id=1\"&gt;I'm a link parsing a variable!&lt;\/a&gt;<\/code><\/p>\n<p>Now, by clicking that link, you will see this: <em>&#8220;https:\/\/stackoverflow.com\/questions\/52229108\/example_page_a.php?id=1&#8221;<\/em> in the URL. the part <code>id=1<\/code> is actually a variable that you are able to fetch through the <code>GET<\/code> method like so:<\/p>\n<pre><code>&lt;?php\n$my_get_variable=$_GET['id']; \/\/ This variable will be equal to 1\n?&gt;\n<\/code><\/pre>\n<p>You can then do whatever you wish with this variable in your <em>&#8220;example_page_a.php&#8221;<\/em> file.<\/p>\n<p>i.e. <code>$my_get_variable=$my_get_variable+1;<\/code><\/p>\n<p><code>$my_get_variable<\/code> is now equal to 2.<\/p>\n<p>The below link will then again parse the newly altered variable to another page using the same method, and you&#8217;d have to retrieve the variable the same way as previously shown.<\/p>\n<pre><code>&lt;a href=\"https:\/\/stackoverflow.com\/questions\/52229108\/example_page_b.php?id=&lt;?php echo $my_get_variable; ?&gt;\"&gt;I'm a link parsing the altered variable!&lt;\/a&gt;\n<\/code><\/pre>\n<p>I.e. <code>$my_get_variable=$_GET['id']; \/\/ This is the newly altered variable!<\/code>.<\/p>\n<p>Notice that you don&#8217;t necessarily have to use 2 different pages, but can keep linking to the same page, and the logic still applies.<\/p>\n<p>Another way is to do it by <em>sessions<\/em>.<\/p>\n<p>Example:<\/p>\n<p><code>$_SESSION[\"my_variable\"]=1;<\/code><\/p>\n<p>you now have a session variable that you can access on any page at any time, as long as you remember to do a <code>session_start();<\/code> before doing anything with the session variables.<\/p>\n<p>So let&#8217;s say that you set <code>$_SESSION[\"my_variable\"];<\/code> to be equal to 1, as I did earlier, in a file called <em>&#8220;example_page_a.php&#8221;<\/em> . You will now be able to access this variable in the other file called <em>&#8220;example_page_b.php&#8221;<\/em>, and alter that session variable into anything you want.<\/p>\n<p><code>$_SESSION[\"my_variable\"]=$_SESSION[\"my_variable\"]+1;<\/code>. Your session variable will now be equal to 2 instead of 1, and it will carry over to any page, as long as you remember to do a <code>session_start();<\/code> before doing <em>anything<\/em> that has to do with your sessions.<\/p>\n<p>Note that you don&#8217;t have to work with numbers, or stick with one data type. You can literally make variables with numbers, overwrite them with strings, or vise versa etc. It&#8217;s completely up to you what you do with those variables.<\/p>\n<p>Now, you weren&#8217;t very specific in your question about the use of it, but I trust that the answer was clear enough. You can construct your own logic around the variables, where and when they should change etc.<\/p>\n<p>And on a final note. The <code>GET<\/code> method will have the variables visible in the URL, so make sure that those variables aren&#8217;t meant to be kept unseen by the user. Sessions, however, will not display anywhere, unless you choose to <code>echo<\/code> them out or anything like that, which means they are perfect for situations where you need to keep variables that should be easily accessible on any page at any time, whilst still being outside of the users perspective.<\/p>\n<p>I would recommend that you read up on the different variable parsing methods, such as <code>GET<\/code>, <code>POST<\/code> and <code>SESSION<\/code>.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">1<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved PHP: Update variable when link is clicked [closed] <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Yes it is possible. One thing you could do, as some people touched in the comments, is to use a GET method, which is essentially parsing a variable through a URL string. Example: &lt;a href=&#8221;https:\/\/stackoverflow.com\/questions\/52229108\/example_page_a.php?id=1&#8243;&gt;I&#8217;m a link parsing a variable!&lt;\/a&gt; Now, by clicking that link, you will see this: &#8220;https:\/\/stackoverflow.com\/questions\/52229108\/example_page_a.php?id=1&#8221; in the URL. the &#8230; <a title=\"[Solved] PHP: Update variable when link is clicked [closed]\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-php-update-variable-when-link-is-clicked-closed\/\" aria-label=\"More on [Solved] PHP: Update variable when link is clicked [closed]\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[346,339,597],"class_list":["post-17867","post","type-post","status-publish","format-standard","hentry","category-solved","tag-html","tag-php","tag-wordpress"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] PHP: Update variable when link is clicked [closed] - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-php-update-variable-when-link-is-clicked-closed\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] PHP: Update variable when link is clicked [closed] - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Yes it is possible. One thing you could do, as some people touched in the comments, is to use a GET method, which is essentially parsing a variable through a URL string. Example: &lt;a href=&quot;https:\/\/stackoverflow.com\/questions\/52229108\/example_page_a.php?id=1&quot;&gt;I&#039;m a link parsing a variable!&lt;\/a&gt; Now, by clicking that link, you will see this: &#8220;https:\/\/stackoverflow.com\/questions\/52229108\/example_page_a.php?id=1&#8221; in the URL. the ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-php-update-variable-when-link-is-clicked-closed\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-10-27T07:24:55+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-php-update-variable-when-link-is-clicked-closed\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-php-update-variable-when-link-is-clicked-closed\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] PHP: Update variable when link is clicked [closed]\",\"datePublished\":\"2022-10-27T07:24:55+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-php-update-variable-when-link-is-clicked-closed\/\"},\"wordCount\":474,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"html\",\"php\",\"wordpress\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-php-update-variable-when-link-is-clicked-closed\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-php-update-variable-when-link-is-clicked-closed\/\",\"name\":\"[Solved] PHP: Update variable when link is clicked [closed] - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-10-27T07:24:55+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-php-update-variable-when-link-is-clicked-closed\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-php-update-variable-when-link-is-clicked-closed\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-php-update-variable-when-link-is-clicked-closed\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] PHP: Update variable when link is clicked [closed]\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] PHP: Update variable when link is clicked [closed] - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-php-update-variable-when-link-is-clicked-closed\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] PHP: Update variable when link is clicked [closed] - JassWeb","og_description":"[ad_1] Yes it is possible. One thing you could do, as some people touched in the comments, is to use a GET method, which is essentially parsing a variable through a URL string. Example: &lt;a href=\"https:\/\/stackoverflow.com\/questions\/52229108\/example_page_a.php?id=1\"&gt;I'm a link parsing a variable!&lt;\/a&gt; Now, by clicking that link, you will see this: &#8220;https:\/\/stackoverflow.com\/questions\/52229108\/example_page_a.php?id=1&#8221; in the URL. the ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-php-update-variable-when-link-is-clicked-closed\/","og_site_name":"JassWeb","article_published_time":"2022-10-27T07:24:55+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-php-update-variable-when-link-is-clicked-closed\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-php-update-variable-when-link-is-clicked-closed\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] PHP: Update variable when link is clicked [closed]","datePublished":"2022-10-27T07:24:55+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-php-update-variable-when-link-is-clicked-closed\/"},"wordCount":474,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["html","php","wordpress"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-php-update-variable-when-link-is-clicked-closed\/","url":"https:\/\/jassweb.com\/solved\/solved-php-update-variable-when-link-is-clicked-closed\/","name":"[Solved] PHP: Update variable when link is clicked [closed] - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-10-27T07:24:55+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-php-update-variable-when-link-is-clicked-closed\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-php-update-variable-when-link-is-clicked-closed\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-php-update-variable-when-link-is-clicked-closed\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] PHP: Update variable when link is clicked [closed]"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/17867","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=17867"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/17867\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=17867"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=17867"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=17867"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}