Post non-input fields in a form? - php

This seems like a simple thing, and maybe I'm just not thinking straight today, but right now I don't see it: How do I post data from a form (in a PHP application) that is not an input field?
The reason I need this is I have a form where the user adds some information in input fields, and this should then update other values in the form based on what the user has entered (doing calculations on this input). This data should then on submit be posted, along with the input from the user.
I tried using form labels, but could not get it to work. For one I couldn't get the value of the form in the jQuery using either .val() or .text(). And I'm not sure if I could get the values of the label in the CodeIgniter function anyway. I also tried simple <p> tags with ids, but that didn't work. I guess it has to be an element with the name attribute...
I'm using a helper in CodeIgniter to get the form values, like so:
$this->input->post('user')
This works fine for input fields, but as explained I need it for non-input elements. Of course I could have input fields that I update in jQuery, but there's a risk that the users will think they should fill them in...
So how do you do it?

How about using <input name="user" type="hidden"> and use Jquery to store the value in there.

Why are you storing input information in non-user-interface elements? Anything you want to be POSTed should be in an input field. Labels are not input elements, they are, well, labels. They label things. What exactly are you doing that you think you can't use input fields? You can disable them, set them to read-only mode, and modify their values in a similar way that you'd modify the text in any other element.

Related

Multi-select does not retain value after post in codeigniter

I have a form in my application with a multi select. I'm using CI's form helper to build my forms, so the build of the element looks like this:
return form_multiselect('authors[response][]', $faculty->get_all_for_multiselect(),
$pre_selected, $additional_attributes);
This is all well and good if the items are in the database ($pre_selected gets existing responses). However, I'm also running the form through CI's form validation, and when that happens, if validation fails, then the multi select loses the values that had been selected.
I'm sure this is something simple that I'm just over looking, so hopefully someone can help me out here.
Adding more information
The field is marked as required so it is going through the validator (although it will always pass as I'm automatically selecting the current user).
(I'm assuming $pre_selected is an array of values?)
You can reset selected values after failed form submit using the $_POST array.
Since you're already using $pre_selected, you should be able to use the following:
return form_multiselect('authors[response][]', $faculty->get_all_for_multiselect(),
array_unique(array_merge($pre_selected, $_POST['response'])), $additional_attributes);

Disable or Read-Only - a select box issue

On this form, I have some input field and select boxes that should not be edited by some users.
However, I need those unable fields to be inserted anyway.
I'm thinking about passing those values to a hidden field (should this be done in js ?);
And submit the form by using those hidden fields instead.
Is this a good approach ? Please advice.
You can use hidden fields to pass the data in to your form, but bear in mind that if someone's planning mischief, they can edit the values with tools like Firebug and submit them anyway, so relying on hidden fields may lead to issues.
If you want to be properly secure, you'll have to do everything on the server side - check to see if the client has permission to access those fields when they submit the form. If they do, take the values they've submitted; otherwise, use default values stored in your PHP code instead.
Just put them in hidden field:
echo $form->hiddenField($model,'property');
Just use hidden feilds within your form, like so:
<input type="hidden" name="id" value="00001" />
No js is required.
I would go with disabled on checkboxes and maybe readonly on text fields. The thing is that you KNOW what value they should have when they are submited. So on server side you can just ignore them and use the value you have choosen. And ignore the data if someone is manipulating the post request.

Form autofill if value exist in database using AJax

I have a form I need to auto fill it using Ajax and php. Suppose My unique field is mobile number. So when form appears firstly person has to fill mobile number. If mobile number exist in the database than all the rest field retrieve its value that is his name, email etc, making all the text fields disable.
my approach to this work is on blur effect I can send value through AJAX. but how call values in array. I have called only single value through echo; but have not called array back from java script page.
Secondly I need jquery to fill all form with respective values and disable particular fields in form.
Please give me some more idea to make this approach better and provide some hint to implement it efficiently.
have you tried Autocomplete from JQuery UI?
http://jqueryui.com/demos/autocomplete/
it is very easy to use, it uses ajax and can be easly used with php. Tell if you will have any problems.
A simple way to do this is to separate your page into 2 forms. The first form will contain only the mobile number field, while the second form will not be initially visible. This way, the user will be forced to fill in his mobile phone before doing anything else.
Submitting the first form will trigger an ajax call to your php file which will check whether the mobile phone exists in the database or not. If it exists, it will return a pre-filled form. If not, it will return an empty form. Therefore, there is no need to change the values of the fields with javascript. Now, all you have to do is to take the response of the php and put it into a div under the first form.

Find out what type of form field was used to enter info in a form with PHP

Using PHP; is there a way to check what type of form field was used to enter info in a form.
For example: was it submitted via a list/menu, radio button, text field, textarea, or checkbox.
I have this info stored in the database; but I'm trying to see if there is a way to do it without querying the database or using hidden form fields to pass the field type. Basically is there a function that already does this?
I don't know of one though I'm sure someone else might pop up with an answer. But if the forms in question are of your own design you could name the inputs as checkbox_ or textarea_ prepended to your normal name. Then parse them on the form processing side.
The data entered into a form will be submitted as a set of key:value pairs only.
With standard HTML form elements only there is no way of telling what type of form field was used to gather a particular value.
Only name/value pairs are passed through the Post data, so you would need to mark it in the field name to give your server script a hint of what it is. You could do this with a prefix/postfix. Depending on where you are in your project, you may want to look into using a framework and taking advantage of the advanced form handling options that they can give you.

filter search results with another form

so here's the problem
I have one form, it outputs search results after submit, with this URL
http://localhost/thi/search/results?keyword=barma&search=1&minprice=nomin&maxprice=nomax&minroom=nomin&maxroom=nomax&minyear=nomin&maxyear=2010
Now I have another form, which is supposed to filter the results from the previous form by getting the current url of the search results and appending to it the GET variables submitted by this form.
I tried filtering before, but that is with anchor links. How am I supposed to do it with forms? Anny approaches?
Just populate the form with the existing data. If it is something that can be changed, then use a text input, a select, or whatever and set the default value to whatever is in the URL. If it can't be changed, use a hidden input.

Categories