0

I have 2 separate pages that display a different set of information. On one page (Page A), I've created a JS function that makes an AJAX query (it pulls in a PHP file that runs a sql) and sorts a table when options on that page are selected. Everything is currently working fine on this page. For the second page (Page B), I would like to be able to create a link that when clicked, will take the user to Page A and run the JS function on click. Since I'm moving over to a new page, I'm not sure how to get that function to carry over.
Any ideas on how I can go about this?

Here's the JS. When a user selects an item on a dropdown from Page B, I run showUser:

function showUser(){
var url = getURLSTring();
console.log(url);
$.ajax({
    type: "GET",
    url: "includes/list-process-availabilities.php?"+url,
    datatype: "jsonp",
    success: function(x){
        $('.list-view-table').html(x);
    }
});

};

The list-process-availabilities.php file is a list of SQL queries that are run based on the options selected in the dropdown menu on the page.

What I want to accomplish on Page B, is have the user taken to Page A and the function showUser is run for a specific SQL query based on the link on Page A.

The PHP is below:

$s=$_GET["s"]; // Get Square Footage
$n=$_GET["n"]; // Get Neighborhood

$sql = "SELECT "; 
$sql .= "* ";
$sql .= "FROM ";
$sql .= "availabilities ";
$sql .= "WHERE ";
$sql .= "1 ";

$availablilityQuery = array("neighborhood" =>$n);
foreach($availablilityQuery as $key => $value){
if(!is_array($value)){
    if($value != ''){
        $sql.="and ".$key."='".$value."' ";
    }
}
}


if($s == "2501")
{
$sql .=" and CONVERT(replace(sq_ft_available, ',',''),UNSIGNED INTEGER)  >2501 ";
}
else if($s != "")
{
$s_array = explode("-", $s);

$sql .=" and CONVERT(replace(sq_ft_available, ',',''),UNSIGNED INTEGER) >=".   $s_array[0]. " and  CONVERT(replace(sq_ft_available, ',',''),UNSIGNED INTEGER) <=". $s_array[1]." ";
}

This is how I run the function on Page A:

        <select name="square-footage" onchange="showUser()" id="selectSquareFootageAjax" class="square-footage-drop">
            <option value="">Square Footage</option>
            <option value="0-1000">0 - 1000 Sq. Ft.</option>
            <option value="1001-2500">1,001 - 2,500 Sq. Ft.</option> 
            <option value="2501">2501 or Greater Sq. Ft.</option>
        </select>
akuzma
  • 1,592
  • 6
  • 22
  • 49
user2989731
  • 1,299
  • 3
  • 17
  • 33

1 Answers1

0

I think that you should create a link in page B with a parameter, sort of:

<a href="pageB.php?runcode=true">Go to page A and run the code</a>

and, in page A, you can detect that the user is coming from page B with:

<?php if ($_GET['runcode']==='true') {    
   ...
   /*** put a link to the Ajax PHP file that is runs the SQL ***/ 
   ...
?>
Reger
  • 474
  • 4
  • 17
  • This looks pretty close to what I want to accomplish, but there's certain information I need to pass over in order to run the sql. On page A, I take options from the values in a form and pass it over (I updated my post above to reflect this). Is there way to associate a value within a link tag to get a similar result? – user2989731 Jan 20 '14 at 19:18
  • I think the only way to do what your asking, is to pass the parameter to page B, capture it in pageB using $_GET['parameter'] and then pass the parameter again in the link pageB.php?runcode=true&parameter=whatevervalue – Reger Jan 20 '14 at 19:33