0

Hi I am new to PHP JSON

My URL

http://xxxx.in/event-view.html?event_id=8

My php code (json-eventview.php)

    include "db.php";
    $data = array();
    $id = $_GET['event_id'];
    $q=mysqli_query($con,"select * from `ca_events` where ca_events.id=$id ");
    while ($row=mysqli_fetch_object($q)){
    $data[]=$row;
    }
    echo json_encode($data);

and Javascript (get Json) used in above url.

    $(document).ready(function() {
    var url = "http://localhost/php/json-eventview.php";
    $.getJSON(url, function(result) {
        console.log(result);
        $.each(result, function(i, field) {
            var id = field.id;
            var general_id = field.general_id;
            var name = field.name;
            var description = field.description;
            var agenda = field.agenda;
            var date = field.date;
            var time = field.time;
            $(".navbar-brand span").append(" " + name + " ");
        });
    });
});

the above gives an empty space.

If, I remove $id = $_GET['event_id']; and WHERE in php file I get the entire table as result.

I need the column values of that particular id in url. can anyone help or give other ideas and examples to get the values based on id from url.

3 Answers3

0

Your issue is that you are passing GET data to an HTML form (Not a PHP or something else)

You need to add some jquery (Since I see Jquery code in your javascript) to pull out the event_id =8 Such as this location How to Read a URL from Jquery

Then Add it to your to the end of your .getJson URL.

Community
  • 1
  • 1
Forbs
  • 1,256
  • 1
  • 7
  • 9
  • as per your message i got this added to javascript var baseUrl = (window.location).href; var event_id = baseUrl.substring(baseUrl.lastIndexOf('=') + 1); alert(event_id) which alerts the id from url but how to get this id in php and get results in mysqli based on this id. Kindly clarify – Subramani N Aug 21 '16 at 09:36
  • store the id in an external variable (such as newid) and then put place it in the URL as "?event_id='.event_id" – Forbs Aug 22 '16 at 04:05
0

Change Javascript Code to this :

$(document).ready(function() {
var url = "http://localhost/php/json-eventview.php?event_id=YOUR_REQUESTED_ID";

and put your id insted of YOUR_REQUESTED_ID

Hossein
  • 1,301
  • 1
  • 12
  • 23
  • I have two pages. events.html which has all the events listed. by clicking a event it should show the column values of that particular event in event-view.html, so how to put event id dynamically – Subramani N Aug 21 '16 at 09:29
0

As I mentioned in my comment above, the problem is that the url parameter is on the page that has your JavaScript, and not on the PHP page that is receiving the AJAX call. One way to resolve your problem would be to pass the event_id to your PHP page that receives the AJAX call.

On your page that has the JavaScript, parse the url to get the event_id parameter.

The function from this site will parse your url for parameters:

$(document).ready(function() {
  var getUrlParameter = function getUrlParameter(sParam) {
    var sPageURL = decodeURIComponent(window.location.search.substring(1)),
        sURLVariables = sPageURL.split('&'),
        sParameterName,
        i;

    for (i = 0; i < sURLVariables.length; i++) {
        sParameterName = sURLVariables[i].split('=');

        if (sParameterName[0] === sParam) {
            return sParameterName[1] === undefined ? true : sParameterName[1];
        }
    }
  };
  var eventId = getUrlParameter('event_id');
});

This will give you the event_id so that you can then use it in your AJAX call to the other PHP page:

var url = 'http://localhost/php/json-eventview.php?event_id="' + id + '"';

Your complete code should look something like this:

$(document).ready(function() {
  var getUrlParameter = function getUrlParameter(sParam) {
    var sPageURL = decodeURIComponent(window.location.search.substring(1)),
        sURLVariables = sPageURL.split('&'),
        sParameterName,
        i;

    for (i = 0; i < sURLVariables.length; i++) {
        sParameterName = sURLVariables[i].split('=');

        if (sParameterName[0] === sParam) {
            return sParameterName[1] === undefined ? true : sParameterName[1];
        }
    }
  };
  var eventId = getUrlParameter('event_id');
  if(eventId !== undefined {
   var url = 'http://localhost/php/json-eventview.php?event_id="' + eventId + '"';
   $.getJSON(url, function(result) {
    console.log(result);
    $.each(result, function(i, field) {
        var id = field.id;
        var general_id = field.general_id;
        var name = field.name;
        var description = field.description;
        var agenda = field.agenda;
        var date = field.date;
        var time = field.time;
        $(".navbar-brand span").append(" " + name + " ");
    });
   });
  }
});
devlin carnate
  • 8,309
  • 7
  • 48
  • 82