0

I have a page that lists courses. When a student clicks on one of the course titles, it will use AJAX to pass the parameter (courseId) to a second page which will return the course details. How can the parameter (courseId) be accessed in the second page? If I put a value directly in the source instead of accessing the parameter, it shows the details:

var idInput = 12345;
var regNoInput = 098766;

When I tried getElementById to access the parameter, it didn't show any details:

var idInput = document.getElementById("courseId").value;
var regNoInput = document.getElementById("regNo").value;

I also tried accessing the parameter as a PHP variable, but still no details:

var idInput = "${courseId}";
var regNoInput = "${regNo}";

this is my first page script, the parameter is pass using url:

  success: function(json_results){
    $('#list').append('<ul data-role="listview" data-inset="true"</ul>');
    listItems = $('#list').find('ul');
    $.each(json_results.rows, function(key) {
     html = "<li data-mini='true' id='icon'><a href='MRegisteredClassesDetail.html?
            courseId=" + [json_results.rows[key].courseId] + "&regNo=" +
            [json_results.rows[key].regNo] +"' rel='external'>" + 
            json_results.rows[key].courseName+ "</a>" + "<a 
            href='http://137.57.102.146:8080/Training/MRateCourse.phone?courseId="+ 
            [json_results.rows[key].courseId] + "&regNo=" +   
            [json_results.rows[key].regNo] + "' rel='external'>RATE THIS COURSE</a>
            </li>" ; 
     listItems.append(html); 
    }); 

and this is my second page, it should read the parameter value:

  var idInput = "${courseId}";
  var regNoInput = "${regNo}";

   success: function(json_results){
     $('#list').append('<ul data-role="listview" data-inset="true"</ul>');
     listItems = $('#list').find('ul');
     $.each(json_results.rows, function(courseId) {
     html  ='<h1 align=center>'+json_results.rows[courseId].courseName+'</h1>';
     html +='<li>Registration Number:'+json_results.rows[courseId].regNo+'</li>';
     html +='<li>Registration Date:'+json_results.rows[courseId].regDate+'</li>';
         listItems.append(html);
     });
sone
  • 161
  • 1
  • 5
  • 16
  • How are you passing the information to the next page? – atomSmasher Jul 19 '12 at 01:50
  • yup.. it is in url..here the url.. " + json_results.rows[key].courseName+ " – sone Jul 19 '12 at 02:05
  • This will help you http://stackoverflow.com/questions/1403888/get-url-parameter-with-jquery – James Jul 19 '12 at 02:08
  • @sone: [sample code](http://sscce.org/) should be complete, concise and representative. Currently, the sample code doesn't meet these requirements. – outis Jul 19 '12 at 02:25
  • You should pass data from one page to another using [GET](http://php.net/manual/en/reserved.variables.get.php), [POST](http://php.net/manual/en/reserved.variables.post.php), or [SESSION](http://php.net/manual/en/features.sessions.php). You can't use PHP variables in JavaScript. PHP executes server-side and JavaScript is client-side (usually). In this situation, you may want to use $_GET. – SomeKittens Jul 19 '12 at 02:35
  • @sone: *concise*. Please remove extraneous code. Read the link in my previous comment. Thanks for updating your question, rather than responding with a comment. – outis Jul 19 '12 at 02:52
  • It also doesn't look complete. "pageinit" and "pageshow" aren't standard events, and `listview` isn't a standard jQuery method. – outis Jul 19 '12 at 03:03

2 Answers2

0

if the parameter in the url,try this:

var params=window.location.search.substr(1).split("&"),
    idInput=params[0].split("=")[1],
    regNoInput=params[1].split("=")[1];
artwl
  • 3,502
  • 6
  • 38
  • 53
0

i declare a variable

var idInput = getParameterByName('courseId');

and define the function

function getParameterByName(name) {
        var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
        return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
    }

it works well.

sone
  • 161
  • 1
  • 5
  • 16