我们可以在POST中获得链接吗? [英] Can we get the link in POST?
问题描述
我的网站上有一个引导导航栏,其中的下拉列表中填充了数据库中的一些数据。
I have a bootstrap navbar on my website, in which a dropdown is filled with some data from database.
现在,当我单击该链接时,我想在POST中将该数据发送到我的控制器。这可能吗?
Now When i click on that link, i want to sent that data in POST to my controller. Is this possible ?
这是我的下拉菜单:
<ul class="dropdown-menu">
<li><a href="#">
<?php
foreach($sites as $site) {
echo "<li>".$site->site_key."</li>";
}
?>
</a></li>
</ul>
更新后的代码:
<form id="hiddenForm" method="post" style="display: none">
<input name="linkAddress" value="<?php echo $site->site_key; ?>">
<input name="otherData" value="">
</form>
<script>
var specialLinks = document.getElementsByClassName("specialLink");
var hiddenForm = document.getElementById('hiddenForm');
Array.prototype.forEach.call(specialLinks, function(link) {
link.onclick = function(event) {
event.preventDefault();
var req = new XMLHttpRequest();
req.addEventListener('load', function() {
var response = req.responseText;
//Do something with response, like change a part of the page
});
//Set the page to send the request to
req.open('POST', 'recieve.php');
//Specify that we're using URL-encoded coded parameters
req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
//Send the URL-encoded POST data.
req.send('linkAddress=' + link.href + '&otherData=' + someOtherData());
}
</script>
推荐答案
经过长时间的讨论,这就是我想要的
after long discussion, here is i get what he want to
- 他想将变量发送给单击控制器(我使用Jquery)
所以这是我的演示根据您的问题
so here is my demo based on your issue
请注意,该演示中您需要最新的jquery,请参阅外部js
note in that demo you need latest jquery, please see external js
所以我更改了html使其变得容易
so i change your html to make it easy
<ul class="dropdown-menu">
<a href="#" class="specialLink" id="54th-65hy">
<li>54th-65hy</li>
</a>
<a href="#" class="specialLink" id="HT45-YT6T">
<li>HT45-YT6T</li>
</a>
</ul>
请参阅我将您的值设为id
这是我的js
$( ".specialLink" ).click(function() {
var value = this.id; //get value for throw to controller
alert(value);
$.ajax({
type: "POST", //send with post
url: "<?php echo site_url('welcome/post) ?>", //for example of mine , using your controller
data: "value=" + value, //assign the var here
success: function(msg){
alert(msg);
}
});
});
查看我保存到具有名称值的变量中的ID以发送到控制器欢迎/发布
see the id i save into variable with name value to send to controller welcome/post
然后是控制器
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index()
{
}
public function post()
{
$value=$this->input->post('value');
echo $value;
}
}
让您知道ajax,注释演示中的我的js将是这样
to make you know ajax, just comment my js in demo will be like this
$( ".specialLink" ).click(function() {
var value = this.id; //get value for throw to controller
alert(value);
$.ajax({
type: "POST", //send with post
url: "<?php echo site_url('welcome/post) ?>", //for example of mine , using your controller
data: "value=" + value, //assign the var here
success: function(msg){
alert(msg);
}
});
});
编辑:
1.在您知道演示之后,首先使演示成为显示值将答案中的js复制并粘贴到codepen
2.在演示中,我在js部分
中的pust控制器中3.请在js文件中看到演示数据: value = + value,
Edit : 1. make the demo become show the value first after you know it just copy and paste js in answer into codepen 2. in the demo i pust controller in js part 3. please see in js file demo data: "value=" + value,
这篇关于我们可以在POST中获得链接吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!