在html中单击div后,使用jquery将数组传递给url

时间:2022-08-26 20:27:42

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

我有主文件index.php和索引我要去另一个文件categories.php

Here is a div in categories.php

这是在categories.php中的div

<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我这样做

 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

在getRings.php我有功能

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

现在我面临的问题是,jquery代码运行,但是我不确定是否正在调用getRings代码,因为当我点击div时我不会朝向任何URL。如果我必须将$ response传递给$ URL我该怎么做

What could be wrong>?

可能是什么错误>?

3 个解决方案

#1


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.

由于您必须将AJAX的响应值传递给另一个文件,我建议您使用会话来简化这一过程。

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

所以在你的getRings.php中,你需要有session_start();在文件的开头而不是这个:

$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:

在您的AJAX成功函数中,重定向为:

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.

在重定向页面中,您还需要拥有session_start();再次访问值为echo $ _SESSION ['error'];等等

#2


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.

您正在调用getrings.php。如果您使用的是Lunux操作系统,则会出现404错误,因为您的实际文件名是getRings.php,其中R位于Upercase中。

#3


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.
  • 你正在做的是通过javascript ajax向另一个php文件发送请求,其中包含一些代码,然后重定向到其他页面。记住这个javascript ajax只是发送请求,这意味着你不能通过getRings.php文件重定向它,你必须通过javascript ajax重定向它或为什么不在同一个文件中执行它?不错的选择。

#1


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.

由于您必须将AJAX的响应值传递给另一个文件,我建议您使用会话来简化这一过程。

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

所以在你的getRings.php中,你需要有session_start();在文件的开头而不是这个:

$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:

在您的AJAX成功函数中,重定向为:

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.

在重定向页面中,您还需要拥有session_start();再次访问值为echo $ _SESSION ['error'];等等

#2


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.

您正在调用getrings.php。如果您使用的是Lunux操作系统,则会出现404错误,因为您的实际文件名是getRings.php,其中R位于Upercase中。

#3


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.
  • 你正在做的是通过javascript ajax向另一个php文件发送请求,其中包含一些代码,然后重定向到其他页面。记住这个javascript ajax只是发送请求,这意味着你不能通过getRings.php文件重定向它,你必须通过javascript ajax重定向它或为什么不在同一个文件中执行它?不错的选择。