-1

I am having a problem. i have some code that i need to use ajax to bring up results when a specific link is clicked. it gets the id of the button anduses ajax to search the database for that id. At the moment i am able to post to the next page but i need to load the results at the same time so as it posts to the page say if that page had a confirmation it would show on the index page.

index.php

<script type="text/javascript" src="jquery.min.js"></script>

<button class="Stylebut" style="background-color:yellow;" id="500" onClick="reply_click(this.id)">time</button>

<script type="text/javascript">
function reply_click(clicked_id)
{
    $.ajax({
     url: 'load.php', //This is the current doc
     type: "POST",
     dataType:'json', // add json datatype to get json
     data: ({name: clicked_id}),
     success: function(data){
         console.log(data);
         alert("hello")
     }
});  

}
</script>

load.php

<?php
$userAnswer = $_POST['name']; 
?>
<p>This text needs to show on the page and anything else that loads up</p>

i need to send the data to the page and load that page using the data and print it on the first page using ajax. thanks for your help

Jackbeeby
  • 93
  • 1
  • 8
  • Welcome to SO. Please read [What topics can I ask about](http://stackoverflow.com/help/on-topic) and [How to ask a good question](http://stackoverflow.com/help/how-to-ask) And [the perfect question](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) And how to create a [Minimal, Complete and Verifiable example](http://stackoverflow.com/help/mcve) SO is **not a free Coding or Code Conversion or Debugging or Tutorial or Library Finding service** ___Here at SO we fix your attempts, we do not attempt your fixes___ – RiggsFolly Nov 09 '16 at 01:16

1 Answers1

0

For the given code on load.php you are not returning JSON, so you need to remove this line from your ajax request:

dataType:'json', // add json datatype to get json

Alternatively, you need to serve valid JSON

<?php
echo json_encode($_POST['name']);
Community
  • 1
  • 1
Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167