简体   繁体   English

从jQuery发送数组数据到PHP并获取值

[英]Sending array data to PHP from jQuery and retrieving the value

I am working on a login form. 我正在使用登录表单。 The form fields are validated via jQuery . 表单字段通过jQuery验证。 After jQuery validation the control goes to the php page then it fetches the values from the table in the form of array and send it back to the jQuery. 在jQuery验证之后,控件进入php页面,然后以数组的形式从表中获取值并将其发送回jQuery。

Now the problem is I want to redirect the users to the new page with the values in the array. 现在的问题是我想使用数组中的值将用户重定向到新页面。 How can I do that? 我怎样才能做到这一点?

Here is my jQuery code: 这是我的jQuery代码:

$.ajax({

    type:'POST',
    url:baseurl,

    data:data1,

    success: function (response){

            if (response != false)
            {

             window.location = index.php?u= "+response;

            }
   }

})

Can I send those values via post method and how do I get those values in index.php page? 我可以通过post方法发送这些值吗?如何在index.php页面中获取这些值?

EDIT- Perhaps, its better if you change the approach. 编辑-也许,如果您改变方法会更好。 what you want is a data array in the page in which you get redirect after ajax complete. 您需要的是页面中的数据数组,在完成ajax之后您将在其中重定向。

so- better use php session. 因此-最好使用php session。

Your controller- 您的控制器-

   class myclass extends CI_Controller {
    function myfunction()
    {
    $data=array(
    // set your data array here
    );
    $this->session->set_userdata($data);
    }
    }

Now when ajax runs this function gets executed and data array is stored in session. 现在,当ajax运行时,该函数将被执行并将数据数组存储在会话中。

$.ajax({

    type:'POST',
    url:'<?php echo site_url() ?>myclass/myfunction',

    data:data1,

    success: function (response){

            if (response != false)
            {

             window.location = '<?php echo site_url() ?>newcontroller/newfunction';

            }
   }

})

now, retrieve it in your target view. 现在,在目标视图中检索它。

print_r($this->session->all_userdata());

///////////////////////////////////////////////////////////////////// EDIT ENDS ////////////////////////////////////////////////// //////////////////编辑结束

For what you are asking, you cant acheive it directly, but that does'nt means it cant be done. 对于您要问的问题,您无法直接实现,但这并不意味着它不能完成。

You can acheive it by this- 您可以通过以下方式实现它-

var url = 'http://example.com/vote/';
var form = $('<form action="' + url + '" method="post">' +
  '<input type="text" name="api_url" value="' + Response + '" />' +
  '</form>');
$('body').append(form);
$(form).submit();

Now , on index.php, use $_POST to retrive the value posted by this form. 现在,在index.php上,使用$ _POST检索此表单发布的值。

您应该使用绝对URL,并在引号之间插入:

window.location = "http://domain.com/index.php?u=" + response[0];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM