PHP表单使用AJAX传递post数据

时间:2021-06-03 21:31:52

I'm sorry if the question has been answered before but I have I have populated with data from PHP ie. dropdowns with options. I am using CodeIgniter and wish for the php variables to be sent back to my controller.

我很抱歉,如果问题已经得到解答,但我已经填充了来自PHP的数据,即。带有选项的下拉菜单。我正在使用CodeIgniter并希望将php变量发送回我的控制器。

I have found the following Ajax post function using JQuery:

我使用JQuery找到了以下Ajax post函数:

$.ajax({
  type: "POST",
  url: url,
  data: data,
  success: success,
  dataType: dataType
});

In my case I have a controller called main and a method called postBack() so I'm assuming I would enter the following:

在我的情况下,我有一个名为main的控制器和一个名为postBack()的方法,所以我假设我会输入以下内容:

$.ajax({
  type: "POST",
  url: http://localhost/project/main/postback,
  data: data,
  success: success,
  dataType: dataType
});

however I have no idea how to pass the PHP variables using the code or what to enter in the data or datatype fields, once again I apologise if this has been answered but I can't seem to figure this out.

但是我不知道如何使用代码传递PHP变量或者在数据或数据类型字段中输入什么,我再次道歉,如果这已经得到了回答,但我似乎无法解决这个问题。

Many thanks in advance

提前谢谢了

can someone tell me if I'm overcomplicating this if I perform the above can I simply grab all of the data back on the controller using: $_POST

可以有人告诉我,如果我执行上述操作过于复杂,我可以使用以下命令获取控制器上的所有数据:$ _POST

3 个解决方案

#1


0  

You can use jquery for this

你可以使用jquery

In php page use

在php页面使用

<a href='#' onclick="showDiv('<?php echo $val; ?>')" class="classname">Edit</a>

Jquery function

<script>
function showDiv(id)
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {                       
        //do something as it is equivalent to an ajax success function
    }
}
xmlhttp.open("GET","your-php-page.php?id="+id,true);
xmlhttp.send();
</script>

#2


0  

PHP variables can be passed either as a variable like this

PHP变量可以作为这样的变量传递

localhost/project/main/postback/variable

In this variable should be treated as the parameter we are passing. So here You could use

在这个变量中应该被视为我们传递的参数。所以在这里你可以使用

'localhost/project/main/postback/'.<?=$variable?>.''

else you could assign the variable value to any hidden field in the form and can be access it from posted form data itself as $this->input->post('hiddenfieldname');

否则你可以将变量值分配给表单中的任何隐藏字段,并且可以从发布的表单数据本身访问它,如$ this-> input-> post('hiddenfieldname');

#3


0  

enclose your url with single quotes.

用单引号括起你的网址。

<script>
$(document).ready(function(){
$("#sel").change(function(){
var data=(this).val();
 $.ajax({
      type: "POST",
      url: 'http://localhost/project/main/postback',
      data: data,
      success: function(data){
    alert(data);
    }

    });

})

})
</script>



  //html part  
    <select id="sel">
    <option>1</option>
    <option>2</option>
    </select>

#1


0  

You can use jquery for this

你可以使用jquery

In php page use

在php页面使用

<a href='#' onclick="showDiv('<?php echo $val; ?>')" class="classname">Edit</a>

Jquery function

<script>
function showDiv(id)
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {                       
        //do something as it is equivalent to an ajax success function
    }
}
xmlhttp.open("GET","your-php-page.php?id="+id,true);
xmlhttp.send();
</script>

#2


0  

PHP variables can be passed either as a variable like this

PHP变量可以作为这样的变量传递

localhost/project/main/postback/variable

In this variable should be treated as the parameter we are passing. So here You could use

在这个变量中应该被视为我们传递的参数。所以在这里你可以使用

'localhost/project/main/postback/'.<?=$variable?>.''

else you could assign the variable value to any hidden field in the form and can be access it from posted form data itself as $this->input->post('hiddenfieldname');

否则你可以将变量值分配给表单中的任何隐藏字段,并且可以从发布的表单数据本身访问它,如$ this-> input-> post('hiddenfieldname');

#3


0  

enclose your url with single quotes.

用单引号括起你的网址。

<script>
$(document).ready(function(){
$("#sel").change(function(){
var data=(this).val();
 $.ajax({
      type: "POST",
      url: 'http://localhost/project/main/postback',
      data: data,
      success: function(data){
    alert(data);
    }

    });

})

})
</script>



  //html part  
    <select id="sel">
    <option>1</option>
    <option>2</option>
    </select>