向控制器提交表单值 [英] Submitting form values to controller
问题描述
这应该是非常简单的,但我只是不能得到它。
我学习codeigniter,我有一个表单下面的代码
< body>
< form name =userinputaction =form_reader.phpmethod =post>
Name< input type =textname =username> < br />
< input type =submitvalue =Submit>
< / form>
code> form_reader.php 在我的控制器文件夹。我收到一个
404 Not Found
错误。解决方案将您的值发送到控制器中的函数
< form name =userinputaction =form_reader / save_userinputmethod =post>
在控制器中创建一个名为save_userinput的函数:
<?php
class Form_reader extends CI_Controller {
public function save_userinput()
{
//代码到这里
//例如:获取形式的post值:
$ form_data = $ this-> input-> post();
//或只是用户名:
$ username = $ this-> input-> post(username);
//然后做任何你想要的:)
}
}
?>
希望有帮助。请务必查看CI文档,真的很好。任何更多的问题,只是问:)
编辑:想出来。
< form name =userinputaction =index.php / form_reader / save_userinput method =post>
我习惯了没有index.php,我通过使用.htaccess文件(如 this one),所以我忽略了那。
或者,您可以使用形式助手:
使用
this-> load-> helper('form')
将它加载到控制器中,然后使用它而不是HTML; form>
tag:< ;? echo form_open('form_reader / save'); ?>
This should be something really simple but I just can't get it.
I'm learning codeigniter and I have a form with following code
<body> <form name ="userinput" action="form_reader.php" method="post"> Name <input type="text" name="username"> <br/> <input type="submit" value="Submit"> </form>
I have a controller called
form_reader.php
in my controllers folder. I get a404 Not Found
error. What am I doing wrong ?解决方案Send your values to a function in your controller
<form name ="userinput" action="form_reader/save_userinput" method="post">
in your controller, make a function called "save_userinput":
<?php class Form_reader extends CI_Controller { public function save_userinput() { //code goes here // for example: getting the post values of the form: $form_data = $this->input->post(); // or just the username: $username = $this->input->post("username"); // then do whatever you want with it :) } } ?>
Hope that helps. Make sure to check out the CI documentation, it's really good. Any more questions, just ask :)
EDIT: Figured it out. Use this opening form tag instead:
<form name ="userinput" action="index.php/form_reader/save_userinput" method="post">
I'm used to not having the index.php there, I remove it by using a .htaccess file (like this one), so I overlooked that. It works here with that small edit in the action attribute.
Alternatively, you could use the form helper:
Load it in your controller by using
this->load->helper('form')
and then use this instead of the HTML<form>
tag:<? echo form_open('form_reader/save'); ?>
这篇关于向控制器提交表单值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!