0

I have main file index.php and from index i am going to another file categories.php

Here is a div in categories.php

<div  id="allRings" class="categories">

          <img style="margin-top: 10px;" src="images/allrings~iPad.png" alt="" width="100" height="68" /><br /><br />
          <label class="catText" >All Rings</label>

    </div>

In jquery i am doing this

 jQuery('#allRings').click(function()
   {
       $.ajax({
                type: "POST",
        url: "getrings.php?ringType=all", // what ever exact url is
        data: "",
        success: function(result) 
                {
                  //alert(result);
                   // CALL index.php here with result data
                }
     });
   });

In getRings.php i have function

if(!empty( $_REQUEST['ringType'] )) 
{

    $db = new db();
    $ringType = $_REQUEST['ringType'];
    if ($ringType == "all")
       $rings   = $db->query("SELECT * FROM rings");
    else
       $rings   = $db->query("SELECT * FROM rings WHERE ringSetCategories LIKE '".$ringType."'");

    $response = array();
    $response['error'] = '';
    $response['status'] = '10';    
    $response['data'] = $rings;
    //echo json_encode( $response);
    header('location: http://www.xxx.com');

    exit;
}

Now problem i am facing is, the jquery code runs , however i am not sure if getRings code is being called or not because i am not headed towards any url when i click on div.Also if i have to pass $response to the $URL How would i do it

What could be wrong>?

Muhammad Umar
  • 11,391
  • 21
  • 91
  • 193
  • Is _categories.php_ in a sub-directory from _getrings.php_? – AyB May 01 '14 at 06:52
  • they are in same directory – Muhammad Umar May 01 '14 at 07:02
  • Then you don't need the `../` in the `url`. It points to the directory before it. – AyB May 01 '14 at 07:04
  • i have tried removing this.....already – Muhammad Umar May 01 '14 at 07:07
  • Have you checked the network tab in your browser? There's a lot of things you can find through it. – AyB May 01 '14 at 07:08
  • Can you be more specific? not sure what is network tab and its usage. I am fairly new to server side – Muhammad Umar May 01 '14 at 07:29
  • It's a code inspector tool built-in your browser, in this case, it would tell you about the url accessed by ajax and it's output. Have a look at [this](http://cs.brown.edu/courses/cs132/resources/inspector/) if you use chrome and [this](http://dannguyen.github.io/NICAR/2012/02/25/nicar-2012-inspect-the-web-with-your-browsers-web-inspector/) for firefox. – AyB May 01 '14 at 07:34
  • @ICanHasCheezburger i did a little modification, i have removed the location and it seems i am getting response in success: function(result) { }. How can I load a new page www.xxxx.com from success? – Muhammad Umar May 01 '14 at 07:36
  • You can use `window.location.href = "url";`. If you need to pass the ajax file's data to this url, I recommend you use sessions. – AyB May 01 '14 at 07:41
  • @ICanHasCheezburger make an ansewr maybe? check my edited question – Muhammad Umar May 01 '14 at 07:43
  • One thing I missed, are you redirecting to a different domain? If so, I guess it's not possible to use session. – AyB May 01 '14 at 07:52
  • no i am not , its the same domain, the files are in same directory. Can you make an answer and tell me how to load new url in my ajax function. I'll accept it – Muhammad Umar May 01 '14 at 07:53

3 Answers3

1

Here's the answer in detail:

Since you have to pass the AJAX's response value to another file, I recommend you use sessions to make this easy.

So in your getRings.php, you need to have session_start(); at the beginning of the file and instead of this:

$response = array();
$response['error'] = '';
$response['status'] = '10';    
$response['data'] = $rings;

you do:

$_SESSION['error'] = '';
$_SESSION['status'] = '10';    
$_SESSION['data'] = $rings;

And in your AJAX success function, redirect as:

window.location.href = "http://www.xxx.com";

In the redirected page, you again need to have session_start(); again to access the values as echo $_SESSION['error']; etc.

AyB
  • 11,609
  • 4
  • 32
  • 47
0

You are calling getrings.php. If you are on Lunux OS then it gives 404 error because your actual file name is getRings.php where R is in Upercase.

martian
  • 20
  • 5
0
  • What you are doing is sending a request through javascript ajax to another php file which include some code and then redirecting to other page. Remember this javascript ajax is just to send request, it means that you can't redirect it through getRings.php file for this you have to redirect it through the javascript ajax or why don't you do it in the same file ? good option.