1

Currently, I am using this jQuery code to put HTML from another file into a content div

$("#list li a").click(function(){
var id = this.id;
$('#content').fadeOut('fast', function(){
    $.ajax({url:"./html/" + id + ".html",success:function(result){
    $("#content").html(result);
    }});
});

$('#content').fadeIn('fast');
});

It is pulling the ID from the anchor tags from this list

<ul id='list'>
<li><a href="#" class="nav" id="home">Home</a></li>
<li><a href="#" class="nav" id="order">Order Form</a></li>
</ul>

The database would have a table named 'content' with column named 'html' which contains the actual HTML code that needs to be loaded into the div.

How do I use the ID in the anchor tag to get the corresponding row with the HTML code, and display it in the content div?

EDIT: So, I figured out that this would be my query:

$result = mysqli_query($con,"SELECT content FROM htmlcontent WHERE id='' ");

My question now is, how to I pass the ID from the ajax call in the html page to the php function, and how do I return the $result and display it in the #content div?

Andrew Brooke
  • 12,073
  • 8
  • 39
  • 55
  • Sorry, but this is basic PHP and HTTP (and some AJAX)... something you'll have to learn on your own. – Marc B Oct 07 '13 at 18:49
  • I think you're suffering from "AJAX is scary magic" syndrome. Write a page in PHP which runs some SQL and outputs the result on the screen. That should be pretty simple to find tutorials for. Then make it look at a query string parameter for the ID (be sure to guard against [SQL injection](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)!) Then... well, that's it really! Use the JS you have to load your new URL. – IMSoP Oct 07 '13 at 22:40

1 Answers1

1

There are multiple ways to pass data to a server. Namely, you can use the HTTP GET method or the HTTP POST method. Right now, you are using the jquery $.ajax method, which, by default, uses GET. In a GET request, you must pass the data in the url with the below format...

$.ajax({url:"./path/to/your/php/script?id=" + id ,success:function(result){
...

So the rendered URL would end up looking like www.somesite.com/path/to/your/php/script?id=24

then on your server, you can grab the id from the url easily with the php $_GET method, like so...

$id = $_GET['id'];

and then you can query your database for the record with that ID...

$result = mysqli_query($con,"SELECT content FROM htmlcontent WHERE id = " . $id);

I recommend you research the basic HTTP request types (GET,POST,PUT,DELETE) and their implementations in php like the $_GET and $_POST methods.

Charlie Martin
  • 8,208
  • 3
  • 35
  • 41
  • 1
    I know it's only a simplified example, but this is a good example of leaving yourself wide open to [SQL injection](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). Assuming the ID is an integer, you could use `"... WHERE id = " . intval($id));` as simple but effective protection. – IMSoP Oct 07 '13 at 22:43