Getting $_POST fields before loading form php - php

I want to get all of the fields that will be submitted in $_POST, but before the page is loaded
$data['event'] = $_POSTKEYSFORFORM; //i don't care if they are empty
$this->load->view('event_v/form',$data);
In my template I have values set to $event-key, which causes an error obviously if the key does not exist.
Thanks!

From what I know you cannot "grab the keys" of the POST before you send the POST request.
The controller has no way of knowing what's loaded in the view itself.
If it's that critical to you, You can pass the POST fields as an array to the view, have them echo out in the view, and so have perfect control(if they change, they change in one place only).
It's far from ideal as it's not dynamic to the amount of fields.
Other dirty hacks include having JS auto submit the form once and then collect the POST fields.
Why do you need the post fields without values though, Answering that question might make it easier for us to help out.

Related

Other ways to return data other than string $_GET string parameters?

So im asking what options do I have other than redirecting GET variables when Im trying to repopulate a form?
Im thinking I can create session variable and use that also. Am I correct that these are the ONLY 2 ways?
What I have now is:
if ( count($m) > 0 ) {
// there is an error in fields filled out so we are sending user back to form.
$_SESSION["myarray"] = $m;
header("location: ./edit.php?datefield=".$datefield."&text=".$entry."&flag=".$flag);
}
but this reveals my variable to the user. I want to avoid this.
UPDATE
I re-worded my question and the code to be clearer.
I also like found that the solution for me was to use an include('edit-error.php'). Which is basically the original edit.php with everything stripped out but the code needed to generate the form, and I populated edit-error.php with the needed variables. I never had this in my tool box before so I am grateful to the user who suggested it.
One way that you can consider for hide information (not totally, but more stronger than GET) of user is create a JSON with your infos and send a POST request to your page.
Personally, I still preferring sessions, but there are other ways.
The solution I used was provided my user pala_ , so thanks pala_ I up voted your comment.
I used include('edit-error.php'). Which is basically the original edit.php with everything stripped out but the code needed to generate the form, and I populated edit-error.php's form with the needed variables. I never had this in my tool box before so I am grateful to the pala_ who suggested it.
This allows me to conduct server side validation and redisplay the form again with user input, so they can resubmit. I was using header(Location) with paramaters to submit as GET and I didnt want that. Using include() is a perfect solution for me.

Symfony2 passing variables $_post

I have a form which upon submit must redirect to another form to complete further details.
My problem is that i cant find out how to pass the post variables from the first form to the second.
In my controller I have something like this :
$urls = $_POST['app_frontbundle_urls']['urls'];
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
// var_dump($urls);die();
return $this->redirect($this->generateUrl('purchase_new'));
I cant use an array to pass the variables in GET as I absolutely need these variables in post as there will be a hidden field and a textarea
How can i do this ?
My var_dump here does print my variables but how to get it to the other controller?
Thanks for your help
Why don't you store them in the session or the request ? And you flush the session/request once you used them.
If you absolutely must have access to the data from earlier form submissions in order to process the next form submission I would use a Controller Event Listener to load the data you just saved and place it inside a service on the Container.
StackOverflow answer explaining how to do this
Symfony Cookbook article on how to do this
However, I would recommend that you redesign your form so that it doesn't need to know the data that was posted previously in order to process itself. Having forms that depend on one another's input is problematic because validation will depend on what was posted previously. If you just need to kick off a process at the end of the form process then just save each result, validating as you go through the various controllers and then in the last form do the extra processing as well as validating that the other forms were completed.

Prepopulating Yii form, from GET/POST

My problem is I have an HTML page that includes a short form on it. What I'd like is when this page posts/gets into my Yii model form, to be able to grab and pre-populate the empty form for the model with the values from the incoming form...
I dont think I can use the pagination widget because my initial page is HTML. Is there some way I can just pull this POST value in if it's set rather than the model.
Im still new to Yii so if this is a simple answer, I apologize
Edit: To clarify, my initial form has a few values from my main page form. The HTML page has 3 fields, say first name, last name, email address. That form then posts in to my full page form, which asks for additional information to complete the model. I'm hoping though that I can pre-populate the first and last name in the new php/yii form.
Basically I'm hoping when I first render _form.php I can grab a GET/POST value and assign it to the current model, or add it as a default value on the form.
"I'm hoping ... I can grab a GET/POST value and assign it to the current model"
You sure can. Do this in your controller and you'll be set:
$model->first_name = $_GET['first_name'];
Of course, you may also want to validate those values and set a model scenario to make sure you don't end up with bad data being passed in by an attacker. Otherwise you could run into XSS attacks. But the ability to directly assign model attributes is nice and powerful ...

Using same php file to show form and receive the form's data

I have a php page that generates a form. The action attribute of the form is the page itself. After the user submits the form, the same page is loaded, but this time a POST variable is set, so the page runs another script to deal with the incoming data from the form. I do this by using a conditional fork:
if(isset($_POST['var'])){
generate form
}else{
insert $_POST data into database
}
I'd like to know if this is ok or a bad idea.
I agree with Ignacio. Other than that it looks like a fairly standard approach if you don't need more complexity. One very important thing: make sure you are validating and sanitizing that data before it goes into the database.
The bad part is setting the action attribute to the script. Omitting it completely indicates to the browser that it should be posted to the same URL.
You might even want to go to the extent of checking whether the data was submitted thru AJAX to differentiate it from a regular form submission:
if ( $_SERVER['X_REQUESTED_WITH']=='XMLHttpRequest' )
// AJAX

Pagination and $_POST

I want to do a search with pagination, but I don't know how to 'store' the data in the $_POST array, should I do it with sessions?
Rolensen
If you are doing a search, you are trying to GET data from the server, and not send data to it -- which means you probably should use GET, and not POST.
Also, it would allow your users to bookmark the result pages (or send those links by e-mail, IM, ...), which is always nice ; and, also, use the back/forward buttons of the browser without getting an alert box, which is nice too ^^
(Oh, and, BTW, it would help solve your problem ;-) )
Yes, you can use sessions or hidden fields and even better GET method in your form.
It is possible to use both GET and POST in form, just add appropriate attribute method to form tag:
<form action="index.php?page=5" method="POST">
So the pager links are submit buttons while rest of the data is stored in hidden fields. But that's not a good way to do this because you cannot pass someone link (on IM for example) to your search results.
But the best way is to store somewhere POST input data (look here: http://www.symfony-project.org/plugins/, when you input your query once, it is stored and remembered so you dont need to fill form multiple times)

Categories