Passing information from forms and your own information - php

How do I pass information from my form plus some additional data when submitted the form. For
example if I am using PHP
I have a form and I set the method to GET; now all the fields in the form will be sent in the URL, now suppose if I want to keep a track of how many times the submit is clicked, for that if I pass the variable count along with all the data through URL, how do I do that?
How do I append this data to the existing form's data?
OR suppose if I have a field in my form where I allow to user to enter name of their employs; initially it will show 4 fields for the data, but if I click the submit type button that says MORE, then it will display 4 more fields, and similarly if again he presses more it shows 12,
so I was thinking maybe I could sent a variable $count along with the form data through URL, but I don't know how to do it.

I would user a hidden field with the "count" value. It will then be passed with all the other form input variables but won't actually be visible to the user on in the form:
<input name="field_name" type="hidden" value="cout_value_that_changes" />
You can change - Field_name - to what ever you want it to be and the value should be incremented every time the submit button is pressed - incremented with PHP -
something like:
$count++;
value="<?php echo $count;?>"

Best solution for your requirement seems Use of JQuery.
Add your fields dynamically using JQuery.
For ref. you may check : http://docs.jquery.com/Main_Page
Without using JQuery, you can have Hidden field which would contain count of the field.
we have to pass count in GET request and while showing field in php, check count field.

Related

Fetching value from codeigniter form field among input fields of same name

I don't need any code, I just need the right way to do it.
What I am doing?
Here is the thing. I am making a software for a pharmacy (Drug Store). I have created a tab for shortlist for the drug. See the picture.
enter image description here
What problem I am facing?
In the last column, I have a form field to input drug or add stock. But you can see there are many form fields for so many rows. When I click to "Confirm" button, it's not gonna work cause the table has been generated using loop. So, for all the fields there are same name.
How can I take the value of input field right above the submit button. I mean when I click confirm button it will take same row's input field not others.
How can I do it? No code needed. I am using php codeigniter framework.
Thanks
create one variable like '$i' and increase it and append it on last of input name.as well as give id to submit button and append id with $i.
for eg. name_1,name_2,submit_2,submit_2
when you submit just get the id of submit button. Then get the number from id eg.2,3.take input from the same input variable id. You can do it using ajax. So it will be done without refresh page.

Store value from input field without sending form

I need to store value from input field in form one. Reason why I need that is when I send form 2 in the same file, all filled input fields in form 1 is blank. My thinking is to read value from form 1 and in form 2 make hidden field with that value. When I send form 2, I can $_POST that hidden field as value in form 1. Now I use jquery:
<script type="text/javascript">
$('#search_field').val('<?=$value?>');
var = $('#search_field').val();
</script>
All works if I in that field echo $_GET value. In some cases I need that, but if $_GET is no set, I need to fill that field by myself. How can I store value what I filled by myself?
First form is search form, what sends data to search engine.
Second form is calendar fields, what specifies time for search results. I have 3 choises - all, 30 days, 10 days. When I click 10 days, then calendar fills out automatic.
<form>bla bla 1</form>
<form>bla bla 2</form>
No, problem. Depending on what you need.
And if you really have to "submit" data without submitting a form you have to use AJAX/Javascript (OR use a normal link [=GET] for that).

Post Array Data from Multiple Selects to Mysql

I have a form-1 which has 4 fields. when the user inputs data in these and hits submit, he is taken to form 2 for further selection of more items from multiple selection box. after selections are complete, he is prompted to update and on updating, all the data has to go to a third form for processing.
currently i am passing the single fields data from second form to third form by <input type="hidden" name="abc" value="<?php echo $x[0] ?>">
I am getting stuck as to how to retreive all multiple selected items from the array, perform a calculation on them and then post to mysql and then update the user with posted information.
or is there a better way of doing this, pl. guide me. my fields are:-
first page
customer id - single selection field
date - input
segment selection - single selection field
second page
items inputs - itemid, quantity,price (these are in one row and user will dynamically add or delete rows based on requirements. i have done this through Javascript)
now after all this, i want to gather all details of customer, segment, items(id,quantities,prices) and then post them to mysql.
If you want to use separate pages you can use either hidden inputs fields or sessions to pass along their selections. With sessions, you'd just store the array of data in $_SESSION and use session_start() on each page to get the session from the previous page. With hidden inputs, you can store them just like you would with session, and when they click POST you will rewrite them into the form. Are you stuck on specific aspect of doing this?
On the final page use either the session or the hidden fields (depending on your chosen method) + the final POST, to query MySQL.
Note: As Zirak mentioned in the comments, you could also do this using a single page. You'd use one of the same methods described above, except it would post to itself rather than to another page. This might be a faster/better way to code the page... If you opt for the single page method just ensure that you make it possible to go back, both through their browsers back button and a link you provide.

How do I exclude certain form fields upon submission of the form without disabling the field

If I have a form with the following 3 fields:
First Name Last Name Date Of Birth
When the user fills in all 3 fields and submits you will get the URL
http: //fakeURL.php?firstName=Fred&lastName=Flintstone&DateOfBirth=2/5/1952
However, if the user only fills in First Name and Date Of Birth you will get
http: //fakeURL.php?firstName=Fred&lastName=&DateOfBirth=2/5/1952 (where lastName has no value)
How do I achieve
http: //fakeURL.php?firstName=Fred&DateOfBirth=2/5/1952 (where lastName and value are removed from the URL)
I don't want to disable the input field upon using onsubmit. Is there a better way than disabled the input field?
Please help...
Just remove the "name" attribute from the input element.
// using jQuery
$('#inputId').removeAttr('name');
// native Javascript
document.getElementById('inputId').removeAttribute('name');
You must either:
Remove or disable the field from the form before submitting it
Don't submit the form, instead redirect to a URL you construct from the form yourself
Make an AJAX request instead of leaving the page
Aside from those options, you can't submit this form via GET without all the inputs becoming part of the URL. That's part of the HTML and HTTP specifications.
if you are working on angular and using (ngModel), Remove the name attribute in the input field and add
[ngModelOptions]="{standalone: true}"
You should use forms with the GET method only when the new page is supposed to be bookmarked and passed around.
Since you are talking about you taking input from the user (and I presume you also store that input in a database or some similar permanent storage), you should be using POST instead.

PHP: form action on same page, still show same until refresh

Yes, Im having a little edit profile page, index.php?mode=profile. Lets take the username in the editprofile form as example. The username is already in the username-field. So i changed from "Peter" to "Tom" and press save.
The action is ?mode=profile&edit=true. So now when i have pressed save it has updated the column in the db from Peter to Tom. But this field keeps having the value "Peter" until if i do press refresh (or f5), then "Tom" will appear. Like it hasnt updated in the database anything, although it did but it still shows Peter until next refresh.. like it caches, but it shouldnt cache nothing?
Any help on this? Is it because its on the same "page" / file? what can i do
I think you fetch the data first, put it in the form and then update the database contents. You should update the database contents first when you submit the form. The fetch and creation of the form should be the next step.
Most likely you're rendering the page before doing any of the profile modifications. Make sure you're dealing with changes to the profile before you render it (i.e., all your database calls to update values should be before the SELECT statements to render the page).
HTTP provides two methods : POST and GET. With URL starting with ?param= you are actually using GET. "GET" should be used if and only if the form processing is idempotent query (ie the page content is NOT altered by the query).
If your form modifies data, you should use POST method.
What you can do is in each input field of your form just echo post values for respective fields.
For eg -
lets take an example of username field -
<input type="text" name="username" value='<?php echo $_POST['username']?>' />
By this way if you post any field then that value will be displayed and not from cache.
In my above example i haven't cleaned the post data, make sure that you use cleaned post data
in actual scenario.

Categories