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);
...
Related
I am making admin panel and need to pass a input filed data using php class method like this:
<input type="text" class="form-control" id="title" name="title" onchange="<?php $UpdateObj->SetTitle(); ?>" placeholder="Enter title">
can anyone tell how is it possible. So on change value get to DB.
can anyone tell how is it possible.
It's not.
You can't mix HTML (<input>) or JavaScript (onchange="...") with PHP (<?php ... ?>).
Either use AJAX (which is what you actually want), or use SetTitle() method after submitting the form (which is what you do not want).
Also, learn on basics about HTTP protocol and life of requests, it might give you a hint when is PHP turn, and when is the HTML turn.
I have a have in PHP and I have common fields such as 'Name' and 'Surname'.
Now when the user visits the page e.g. http://www.example.com/form.php the form fields 'Name' and 'Surname' are empty.
I would like to now have a link similar to this http://www.example.com/form.php?name=John
so that when the client hits the link the PHP form will now have the name field already filled with 'John' in it.
I know this can be done in HTML but how can I do it in PHP?
Just to let to know I do not own the PHP form - I just want a link from my website to fill the PHP form (which I do not have control over).
Thanks in advance.
Can be done using $_GET
An associative array of variables passed to the current script via the URL parameters.
e.g.:
<? php
if(isset($_GET['name']))
{
$test = $_GET['name'];
}
?>
<html>
<body>
<form>
<input type="text" name="test" value="<?php if(isset($test)){echo "$test";}?>"/>
</form>
</body>
</html>
Note: code isnt tested or anything.. Also, there are possible security risks with getting values from your URL (can be considered user input), so make sure you are aware of that and how to prevent
You could store that value and then when you're about to output the input fields
you just pass along the stored value.
$name = $_GET['name'];
// ... later on
echo '<input type="text" value="'.$name.'"/>';
By using $_GET superglobal
<input name="name" value="<?php echo !empty($_GET['name']) ? $_GET['name'] : '';?>" />
<input name="surname" value="<?php echo !empty($_GET['surname']) ? $_GET['surname'] : '';?>" />
You can use the get method in php to get the name and make use of it
You can retrive this information by the $_GET["name"] function, or $_REQUEST["name"].
Reserver variables
Be carefull with those operations, you might have validation a/o security problem.
Note: if you are not sure that the "name" variable is set or not, you have to use also the
isset function to test it.
You can use the $_GET superglobal, so your input could look like this:
<input type="text" name="name" value="<?php if(isset($_GET['name'])) { echo $_GET['name']; } ?>" />
The $_REQUEST superglobal does a similar thing but I would just use $_GET.
It looks like everyone's answers here assume you are building the form yourself, which doesn't appear to be the case based on your question.
The thing that you want to do may or may not be possible. If the form accepts certain kinds of parameters in certain ways, you may be able to hook in to that functionality and set it up so that when someone clicks a link on your page, that information gets passed to the other page.
One way forms can accept this information is in the form of a "get" request. With this method, values are passed as part of the url, as in your example: http://www.example.com/form.php?name=John. Assuming your page has access to a php variable called $name, you can create a link from your code to build this kind of url like this:
Sign up!
If the page does not accept get parameters in this way (and I have a hard time imagining that they would), you may have to try other techniques to send along the information (assuming that they will even accept it!). The two other ways I imagine you could do this are by passing the value with "post" or creating a cookie for the page. If you tell us what page you are trying to set up this behavior on, we might be able to examine it and give you a better answer.
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.
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>
I'm new to code igniter. I'm following the form validation tutorial found at:
http://www.codeignitor.com/user_guide/libraries/validation.html
I find I am typing too much in the view to get the form to re-populate. For example, here's what one of my INPUT fields look like:
<input type="text" name="email" value="<?=$this->validation->email ? $this->validation->email : $array_db_values['email'] ?>" />
Basically, when the form first loads, it should show a value from the database. If the user alters the value and it fails validation, then the form should post the erroneously submitted value.
Does code igniter have a "cleaner" way of rendering this kind of output? Otherwise, I'm going to do something like this in my controller:
$array_db_values = getdbresults();
if($_POST['submit'])
foreach($_POST as $key=>$val)
$array_db_values[$key] = $val;
That way, if postback data exists, it will always override database values.
Then input fields in my view can simply be:
<input type="text" name="email" value="<?=$array_db_values['email'] ?>" />
Is there a better/native CI way to handle this scenario?
I don't think a easier way exists, the best way IMO is to do something like this in your controller:
if (isset($_POST['submit']))
{
$values = $_POST;
}
else
{
$values = getdbresults();
}
No need to loop through the $_POST array, for short:
$values = (isset($_POST['submit'])) ? $_POST : getdbresults();
In your view, you do the same:
<input type="text" name="email" value="<?=$values['email'] ?>" />
Or use the Form helper:
echo form_input('email', $values['email']);
Have you guys looked into the Datamapper Overzealous project?
http://www.overzealous.com/dmz/
It handles validation at the model level. In your form fields, you can set the value to come from the model (either from the database or from your previous post).
<input type='text' name='email' value='<?=$object->data_item ?>' />
I'm pretty new to CodeIgniter as well, but adding this library has made things much easier. That project also comes with a form extension that could really make things easier, but I haven't played much with it yet.
I'mm doing exactly the same as what you are doing. It's a bit cumbersome but not too bad imho.
I felt the same way about laboring over forms, check out macigniter's Form Lib in the Ignited Code forums. It saves a ton of time and is really well-written.