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>?

12
  • Is categories.php in a sub-directory from getrings.php? Commented May 1, 2014 at 6:52
  • they are in same directory Commented May 1, 2014 at 7:02
  • Then you don't need the ../ in the url. It points to the directory before it. Commented May 1, 2014 at 7:04
  • i have tried removing this.....already Commented May 1, 2014 at 7:07
  • Have you checked the network tab in your browser? There's a lot of things you can find through it. Commented May 1, 2014 at 7:08

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.