[英]Passing variables from view to controller (MVC)
In my view i have a foreach statement which grabs all the nessesary data from the database which displays correctly. 在我看来,我有一个foreach语句,该语句从正确显示的数据库中获取所有必要数据。
Here is the view: 这是视图:
<?php
?>
<div class="cmt-container" >
<?php
foreach($results as $row){
$user = $row->user;
$comment = $row->comment;
$date = $row->date;
$name = $row->name;
$joke = $row->joke;
$joke_id = $row->joke_id;
// Get gravatar Image
// https://fr.gravatar.com/site/implement/images/php/
$default = "mm";
$size = 35;
$grav_url = "http://www.gravatar.com/avatar/"."?d=".$default."&s=".$size;
?>
<div class="cmt-cnt">
<img src="<?php echo $grav_url; ?>" />
<div class="thecom">
<h5><?php echo $user; ?></h5><span data-utime="1371248446" class="com-dt"><?php echo $date; ?></span>
<br/>
<p>
<?php echo $comment; ?>
</p>
</div>
</div><!-- end "cmt-cnt" -->
<?php
}
?>
<?php
echo form_open('comments/insertComment');
?>
<div class="new-com-bt">
<span>Write a comment ...</span>
</div>
<div class="new-com-cnt">
<input type="text" id="name-com" name="name-com" value="" placeholder="Name is optional" />
<textarea class="the-new-com" id="the-new-com" name="the-new-com" placeholder="Write your comment here..."></textarea>
<input type="hidden" name="joke_id">
<input class="bt-add-com" type="submit" value="Post comment">
<div class="bt-cancel-com">Cancel</div>
</div>
<div class="clear"></div>
</div><!-- end of comments container "cmt-container" -->
<?php
echo form_close();
?>
My question is, how can i pass the $joke_id variable to the insertComment function in my comments controller. 我的问题是,如何将$ joke_id变量传递给我的注释控制器中的insertComment函数。
I have put the input field as hidden on the joke_id field because i want to assign the ID of a joke to a comment, so the joke will have unique comments. 我将输入字段隐藏在joke_id字段上,因为我想将一个笑话的ID分配给评论,因此该笑话将具有唯一的评论。
If the comments are on the same page as the joke, you can just take the $joke_id and put it in the hidden input: 如果评论与笑话在同一页面上,则只需将$ joke_id放入隐藏的输入中即可:
<input type="hidden" name="joke_id" value="<?php echo $joke_id; ?>">
And when you add a comment (I assume you handle the form datas in the insertComment() function), you can access the joke id by $_POST['joke_id']. 并且,当您添加注释时(假设您在insertComment()函数中处理了表单数据),您可以通过$ _POST ['joke_id']访问笑话ID。 (yes, that's not really secure but if your user can comment any joke, you just have to check that a joke with the id equal to $_POST['joke_id'] exists in the DB and if so, you just insert the comment)
(是的,这不是很安全,但是如果您的用户可以评论任何笑话,您只需检查数据库中是否存在ID等于$ _POST ['joke_id']的笑话,如果是,则只需插入评论)
Is that what you wanted? 那是你想要的吗?
try this.. 尝试这个..
<?php echo form_open('comments/insertComment'); ?>
<div class="new-com-bt">
<span>Write a comment ...</span>
</div>
<div class="new-com-cnt">
<input type="text" id="name-com" name="name-com" value="" placeholder="Name is optional" />
<textarea class="the-new-com" id="the-new-com" name="the-new-com" placeholder="Write your comment here..."></textarea>
<input type="hidden" name="jokeid" value="<?= $joke_id; ?>">
<input class="bt-add-com" type="submit" value="Post comment">
<div class="bt-cancel-com">Cancel</div>
</div>
<div class="clear"></div>
</div><!-- end of comments container "cmt-container" -->
<?php echo form_close();?>
and in your insert comment controller 并在您的插入评论控制器中
public function __construct() {
//codes here
$joke_id = $this->input->post('jokeid');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.