Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is a fully working example. Just copy paste into three files named:</p> <p>whateveryouwant.php<br /> another_php_file.php (to change name, you must also change in ajax code (2 places)<br /> contacts.xml</p> <p><strong>How It Works:</strong></p> <p>The first ajax code block runs as soon as the DOM is ready (note: no event triggers it, it just runs. The 2nd code block is triggered by a click event). The ajax sends this POST over to the PHP file called <strong>another_php_file.php</strong>: <code>req=load</code>. This is a key=>value associative array: "req" is the var name, and "load" is its value.</p> <p>Now, look what happens inside <strong>another_php_file.php</strong>. Upon launch, the file checks what POST variables it received. If <code>$_POST['req'] == 'load'</code> then it reads the file from disk and ECHOes it back out. That's how AJAX works: whatever is echoed from the specified PHP processor file is received inside that AJAX code block's <code>success:</code> function.</p> <p>And how does the xml text get inside the textarea control? Look again at that first AJAX code block. Remember that data echoed from the PHP processor file is received inside the success function? Here's the magic:</p> <pre><code>success: function(result) { $('textarea').val(result); } </code></pre> <p><code>result</code> is a variable (could name it anything) containing what was echoed by the PHP file. Then we use jQuery to stick <code>result</code> into the textarea control.</p> <p>Note that I did not specify an ID for that textarea control. This code assumes there is only one on your page. If you had more than one, then you would reference the desired textarea control by its ID:</p> <pre><code> $('#myText').val(result); </code></pre> <hr> <p>The HTML:</p> <pre><code>&lt;head&gt; &lt;script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; $(document).ready(function() { $.ajax({ type: 'POST', url: 'another_php_file.php', data: 'req=load', success: function(result) { //alert(result); $('textarea').val(result); } }); $('#mybutt').click(function() { var stuff = $('textarea').val(); alert(stuff); $.ajax({ type: 'POST', url: 'another_php_file.php', data: 'req=save&amp;changes=' +stuff, success: function(result) { alert(result); //$('textarea').val(result); } }); }); }); //END $(document).ready() &lt;/script&gt; &lt;/head&gt; &lt;html&gt; &lt;body&gt; &lt;input type="button" value="Save Changes" id="mybutt" /&gt; &lt;textarea id='myText' rows="30" cols="120" value='&lt;?php echo $xml; ?&gt;' /&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <hr> <p><strong>another_php_file.php</strong></p> <pre><code>&lt;?php //This necessary to prevent AJAX from automatically ESCAPING all values (e.g. "Bob" is turned into \"Bob\" ) //See http://stackoverflow.com/questions/4550036/jquery-is-automatically-escaping-ajax if (get_magic_quotes_gpc()) { $process = array(&amp;$_GET, &amp;$_POST, &amp;$_COOKIE, &amp;$_REQUEST); while (list($key, $val) = each($process)) { foreach ($val as $k =&gt; $v) { unset($process[$key][$k]); if (is_array($v)) { $process[$key][stripslashes($k)] = $v; $process[] = &amp;$process[$key][stripslashes($k)]; } else { $process[$key][stripslashes($k)] = stripslashes($v); } } } unset($process); } if ($_POST['req'] == 'load') { $xml = file_get_contents("contacts.xml"); echo $xml; }else if ($_POST['req'] == 'save') { $d = $_POST['changes']; //echo $d; $size = file_put_contents('contacts.xml', $d); echo 'Wrote ' .$size. ' bytes to file. Refresh page with [Ctrl]+[F5] to see your changes.'; } </code></pre> <hr> <p><strong>contacts.xml</strong></p> <pre><code>&lt;?xml version="1.0"?&gt; &lt;phonebooks&gt; &lt;contacts group_name="Dimple" editable="1" id="0"&gt; &lt;contact first_name="Extension" last_name="1000" contact_type="sip"&gt; &lt;numbers&gt; &lt;number dial="1620" dial_prefix="" label="Extension" primary="1"&gt; &lt;/numbers&gt; &lt;/contact&gt; &lt;contact first_name="George" last_name="Smith" contact_type="sip"&gt; &lt;numbers&gt; &lt;number dial="1700" dial_prefix="" label="Extension" primary="1"&gt; &lt;/numbers&gt; &lt;/contact&gt; &lt;/phonebooks&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload