Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8421893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:16:39+00:00 2026-06-10T03:16:39+00:00

I have written some JavaScript code that will read from the Google Maps API

  • 0

I have written some JavaScript code that will read from the Google Maps API and get a list of JSON objects. It will then convert each JSON object into an XML object. A co-worker needs this list converted to XML and then appended to an existing XML file and then save it to our server. So I wrote some PHP code to do that.

Now this is my first time working with PHP but I managed to write that PHP code easily enough. The next thing I am trying to do is write some javascript to run the PHP file and also send the XML object over to the PHP where it can append and save the XML. I figured I would use jQuery’s $.ajax function. I started using a simple example trying to echo a string into some <pre> tags. So here is my code:

Javascript (Lies within index.php)

<script>
    var scriptString = 'THIS IS MY STRING';
    $('#clickMe').click(function(){
        $.ajax(
        {
            method:'get',
        url:'index.php',
        data:
        {
            'myString': scriptString
        }
        });
    });
    </script>

PHP & HTML (also lies within index.php)

<button type="button" id="clickMe">CLICK ME TO RUN PHP</button>
<pre>
    <?php
        echo $_GET['myString'];
    ?>
</pre>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T03:16:41+00:00Added an answer on June 10, 2026 at 3:16 am
    • Don’t include = in your data variable names (just say 'myString': scriptString)

    • Always include the form submission method for $.ajax ( method: 'get' )

    • In the PHP code, instead of $myString, use $_GET['myString']

    • Consider using a <button> instead of a <div> to send the data to the server – this is more standard.

    But most importantly:

    • You’re not actually doing anything with the returned data! You also need a success function in the $.ajax options that does something with the data from the server.

    Try this code:

    (JavaScript)

    var scriptString = 'THIS IS MY STRING';
    $('#clickMe').click(function(){
        $.ajax({
          method: 'get',
          url: 'index.php',
          data: {
            'myString': scriptString,
            'ajax': true
          },
          success: function(data) {
            $('#data').text(data);
          }
        });
    });
    

    (PHP and HTML)

    <?php
    if ($_GET['ajax']) {
      echo $_GET['myString'];
    } else {
    ?>
    <button type="button" id="clickMe">CLICK ME TO RUN PHP</button>
    <pre id="data"></pre>
    <?php } ?>
    

    Note that the PHP code will now do different things based on whether or not it is handling an AJAX request. For AJAX requests, you don’t want to return the HTML for the page – just the data you’re interested in. For this reason, it might be better to have two pages: one index.php page, and one ajax.php page which handles AJAX requests.

    Also, in this simple examle, you could have just used a plain HTML form, which wouldn’t require any JavaScript or AJAX.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written some JavaScript and jQuery code that accepts only numeric input in
I have some JavaScript code that is written in one line (no carriage returns),
I have written some code using jQuery to use Ajax to get data from
I am using some pre-written JavaScript from polldaddy. They have a JavaScript option which,
I have written some code that loads an XML document using an XmlDocument object
I have written some code for displaying a drop down list, but the code
I have written some JavaScript that opens an element when an element is clicked.
Ive got a really strange problem with some javascript code that ive written. I
When I read javascript code that is clean and written by people who are
I have written some JavaScript code. It adds new function to NodeList type in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.