I've watched video tutorial for a little MVC framework(if i can call it framework) - http://net.tutsplus.com/tutorials/php/create-your-first-tiny-mvc-boilerplate-with-php/
,i download the source code and changed a little bit the files but I can't figure out how to call a specific method as CodeIgniter's form_open() and make a simple login system.Thanks in advance!
So ,how to send POST data from a form to the controller?
PS:I don't want to reinvent the wheel, just practicing
I also used this TinyMVC to learn MVC structure,
On your HMTL form:
<form method="POST" action="yoururl.com/index.php">
<input type="text" name="username" id="username"/> // name or ID.
<input type="submit" value="submit"/>
</form>
Then in your controller or method just use
$username = $_POST('username');
You have to post to index.php, and it will go to your controller for you to pick up with there. You can also pick it up in a method using the same example.
Related
While building PHP application i have reached the point where i should work on search engine script, however i got few questions about how to.
I have seen many guides and tutorials regarding that matter but none of them explained which method to take.
So options are:
Form with post method which provides posted keywords into function that does query
<?php
$keywords = preg_split('/[\s]+/', $_POST['keywords']);
$products->search($keywords);
?>
<form action="" method="post">
<input type="text" name="keywords">
<input type="submit" value="submit">
</form>
Form with get method which alters http address like this ?keywords=x+y+z, keywords are being passed from $_GET in that case
<?php
$keywords = preg_split('/[\s]+/', $_GET['keywords']);
$products->search($keywords);
?>
<form action="" method="get">
<input type="text" name="keywords">
<input type="submit" value="submit">
</form>
Form with post method which upon submit does redirect to the same page but with ?keywords=x+y+z addon
<?php
header('Location: file.php?keywords=' . $_POST['keywords']);
exit();
?>
<form action="" method="post">
<input type="text" name="keywords">
<input type="submit" value="submit">
</form>
<?php
$keywords = preg_split('/[\s]+/', $_GET['keywords']);
$products->search($keywords);
?>
From what i have learned pure post method is pretty bad if you want to implement pagination. When switching page via $_GET ?page=2, post no longer holds and search results are gone, i gather that i would have to switch pages via post as well, so im confused which approach should i conduct, thank you for all the tips:)
If you are doing a search, the correct method would be a GET, see the description of the various request methods from w3. On that base a GET is more appropriate then a POST.
Having said that, you are retrieving some sort of resource and you could argue that the page is part of this, so either as you seem to propose you can do
/search?keywords=HAL&page=2
or this uri based version where page is part of the definition of the resource:
/search/2/?keywords=HAL
Or even go all out as
/search/keyword1/keyword2/2
(I'm not a fan of the last one per se, I favor the middle or the first; it all has to do with what you define as the resource you are trying to get, and what are the parameters. I'm sure the web is full of discussions that would be rather long-winded to post here)
Realise that you are not talking about $_GET vs $_POST. these are PHP implementations. You should decide what METHOD you will accept as request, and you can then look at those globals to find out where the information lies.
My advice would be to
read up on those methods (see the link to w3)
while you're at it, check out filter_input
I'm trying to submit my form. My form was called by this function:
function controller1()
{
$code = $this->uri->segment(3);
$data['content'] = $code;
$this->load->view(FRONTEND_DEFAULT, $data);
}
But when I submit my form, it goes back to the controller1 method, but I want it to go to another form. My form is:
<form id="form1" action="controller2" method="POST">
<input type="text" name="textbox">
<input type="submit" value="submit" name="submit">
</form>
I want it to go to controller2 but it goes to controller1 :
http://my.website.com/controller1/controller2
How do I prevent it from going back to controller1?
EDIT: I am using codeigniter as my framework.
Since you're using CodeIgniter, why not just read their manual? It shows you how to quickly and easily put together a form:
http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#theform
Here is a copy & paste for a form to be properly pointed to your controller:
<?php echo form_open('controller/method'); ?>
Details on the form helper
You need to specify either a relative or absolute url
see http://www.w3schools.com/tags/att_form_action.asp
i.e. action="example.htm" or action="http://www.example.com/example.htm"
Your action="controller2" is likely failing to resolve and going back to the page that output the form though I would've thought it would give an error if it couldn't resolve that action correctly
I am trying to learn mvc framework in php but then i could not find out a way in passing the form data to a php function in another php page. I am submitting the form and receiving it in the same page and then I am trying to pass the form data to the controller function which will handle the validation. I cannot figure out how should I pass the form data to the other function.
I can pass each data as parameter but then that would be lengthy if there are lots of data in the form. I was wondering if I could pass the form as an object (something like we pass object of structures) to another function and then use the data suitably. I have put in a code module below:
<?php
include('controller.php');
$controller = new Controller($model);
if (isset($_GET['formButton']))
$controller->submitButtonClicked();
?>
<form name="details" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label for="txt1">First Name:</label>
<input type="text" name="txt1" id="txt1"/>
<label for="password">Password:</label>
<input type="password" name="password" id="password"/>
<input type="submit" name="formButton" id="formButton" value="Submit"/>
</form>
Any help would be very helpful.
If you really want to work with MVC model view controller than i suggest you the codeigniter everything is well defined easy to use visit the official site of codeigniter.
http://ellislab.com/codeigniter.
I hope you will like to work with codeigniter try it and get rid of alot of coding.
The $_GET and $_POST superglobals are already arrays of the submitted form data, so you can simply use these in your controller. Just make the form submit to the controller file directly: this is cleaner and there's no need to pass the $_GET or $_POST (you should probably use post, but I don't know the context).
I assume you're building your own MVC from scratch. If so, you could do a handler.php controller, that every form submits to. This could loop the posted data like so:
// define Input class somewhere and include
$input = new Input();
foreach($_POST as $field => $value)
{
$input->$field = $this->validate($value);
}
In validate() you would do some general validation. Then you could use this new Input object wherever you need the input data. This is a very primitive example of how premade frameworks like CodeIgniter and Laravel use an Input helper class, and of course you can expand on this. Or better yet, save some extra work and utilize a good known framework like those mentioned in your project :)
First of all, why are you using $_GET?
Second, the $_GET is a global, which should be available in any class used by your system (doesn't matter if it's in another file or not).
Otherwise, you can simply just pass them on to the method of the class, if you want to do that for some reason:
...
$controller->submitButtonClicked($_GET);
...
In native PHP I can include a javascript code to change the action of a form sent in case I need to direct the user to which page he selects to go like this
<form action="change.php" method="post" name="form">
<input type="submit" value="Click to Page1" onclick="form.action='page1.php';return true;"/>
<input type="submit" value="Click to Page2" onclick="form.action='page2.php';return true;"/>
</form>
I would like to do the same in case I must use codeigniter or cakephp. Someone could help me with this problem ?
CodeIgniter is a backend technology. What you're writing is front end. You're pretty much all set; there isn't really much for you to change. You could, theoretically use CI's form helper, but it's unnecessary...personally, I never use it.
Unless you've removed the index.php file, change the form.action from page1.php and page2.php to index.php/mycontroller/myfunction.
The whole form idea though is sort of flawed; you don't really need it. Why not just use:
onclick="window.location.replace('index.php/mycontroller/myfunction');"
Then you can remove the form all together.
I have a index.html where I would like to submit some coordinates that can be passed upon to separate PHP file; where it could perform a query. I am new to this.
HTML:
Xmax<input type="text" name="Xmax" size="15">
Ymax<input type="text" name="Ymax" size="15">
<input type=SUBMIT name="submit" VALUE="Submit">
PHP query:
$query = "SELECT * FROM state WHERE LONG_HI<$_POST["Ymax"] AND LAT_HI<$_POST["Xmax"];
$result = mysql_query($query);
So is there a way to perform remote action from this HTML file to the specified PHP file?
Well, Forms can do the job. Is'nt it?
Yes
Either make an HTML form to accept the Xmax and Ymax parameters, and set the form action to the PHP file;
Or use AJAX to pass the data in the background and receive a response.
If both of these concepts are foreign to you, and you don't know JavaScript, get comfortable with the first option first.
Would you please describe in detail what you are about to do?
do you have a html form?
What kind of request do you do, clicking a link, sending the form?
The query does not contain any of the variables...
could you please post excerpts of the code? single lines are useless in most cases.
Regards,
Mario
use action attribute in FORM element to specify where the request will be sent to.
<form action="another.php" method="POST">
Xmax<input type="text" name="Xmax" size="15">
Ymax<input type="text" name="Ymax" size="15">
<input type=SUBMIT name="submit" VALUE="Submit">
</form>
You just add few line with your code because to transfer any variable value from one form to another page we have to use 'form' method. So, we have to add form tag with your code. Transferring of data from one page to another page (any type of page like php, jsp, aspx etc) is done by two methods mainly - one of them is Post and another one is Get.
Difference between both the method is quite simple. In Post method, data from one page to another page travels in hidden form whereas Get is basically used to transfer value by displaying it at url. Post method example: user-name and password, and Get Method: any query fired at Search Engine.
<form name="form" action="filename.php" method="POST" >
//Your Code
</form>