1

Right now I'm using below script for button click which opens in new window

<script>function myFunction()
{
   window.open("ouput.php");
}
</script>

But what I need is when user clicks button the results from output.php must be appended in the same window at the bottom. How to execute PHP file using Ajax? I'm using this code with jQuery but there is no result:

$.get( "out.php", 
   function( data ) {          
      document.write(data.Molecule);                  
      $( "output" ).html("Molecule: "+data.Molecule );           
      alert( "Load was performed." );                 
   }, 
   "json" );
});
});

How to run an external PHP file, get results and display it in the same webpage?

1 Answer 1

1

AJAX calls are regular requests so any PHP file is automatically executed.

$("output") will select all <output> tags. I suspect you meant an object with ID=output (which is at the bottom of the page)

Here's a corrected js:

$.get( "out.php", function( data ) {
    // replace content of #output with the response
    $( "#output" ).html( "Molecule: " + data.Molecule );
    // append the response to #output
    //$( "#output" ).append( "Molecule: " + data.Molecule );
    alert( "Load was performed." );
}, "json" );

If the response is not valid JSON this will cause an error (and not call the alert). If that's the case you can alert and check the response like this:

$.get( "out.php", function( data ) {
    alert( data );
});

This won't expect JSON and therefore not throw an error if it's not valid.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.