Change php variable from jquery value using ajax (same page) -


what i'm trying this:

i've php global variable $currentid , simple jquery script check if there "id" (page) in hash of url , if yes wanted give value $currentid. i've been reading lot , notice shall use ajax i'm not managing it.

my code is:

<script type="text/javascript"> $(document).ready(function(){    if (parent.location.hash != '' ) {      var thecurrentid = parent.location.hash.split('#')[1];       $.ajax({             url: 'my.php', //current page             type: 'post',             data: {theid: thecurrentid },             datatype: 'json',             success: function () {              }         });    }   // code here }); </script> 

could me please? thank in advance.

php server side language, meaning once has sent data browser, php variables , data cannot changed...

however, using ajax, can send request php page , change data in dom based on result.

consider this:

var id = parent.location.hash.split('#')[1];;  $.ajax({     url: 'http://www.example.net/id.php',     datatype: 'json',     data: {id: id},     success: function(data){         // whatever want json object         // following log has been returned         console.log(data);         // if want update id variable         id = data.id;         // can change elements in dom reflect php         // variable set         $('#id').text(id);     } }); 

the following include $currentid sent when page loaded... but, can update text using above script.

<p>my current id is: <span id="id">$currentid</span></p> 

please note: not update php variable once php has sent data browser, script has terminated , cannot modified, nor can varibles accessed. above script update text in html.

server side scripting

you need understand how server side scripting functions before start programing php, pretty basic really:

  1. you send request my.php
  2. this request goes server php generates html, write variables have specified html
  3. the data sent as html, not php, browser, can use javascript make changes dom.

the bottom line is, once php has generated , sent page data browser, nothing php exists on page! pure html, or whatever other type of document may be...

another way explain this:

your server sees php page this:

echo "<p>my current id is: <span id=\"id\">$currentid</span></p>"; 

your browser (where javascript sees it), sees page this

<p>my current id is: <span id="id">1</span></p> 

there no php variable access, not exist!

see following more information:

https://softwareengineering.stackexchange.com/questions/171203/what-are-the-difference-between-server-side-and-client-side-programming


Comments

Popular posts from this blog

How can I fetch data from a web server in an android application? -

android - java.net.UnknownHostException(Unable to resolve host “URL”: No address associated with hostname) -