How to retrieve HTML table data in PHP? - php

I'm creating a dynamic table where a user can add records(rows) and enter data. Table cells consists listboxes, text input, file input fields. When a user submits this data I'm getting only the last record(row) data through POST variable in PHP file. Even I tried to get the data using a hidden variable but no use. Any help is appreciated.

You need to make sure the names of your inputs are unique. In your situation where you have more than one set of inputs that are the same I would use HTML arrays.
<input name="my_input[]" />
The PHP manual has a set of examples where this technique is put into action.

Related

How to find out which page is sending the $_POST form data?

I have to write a quiz application in which I have different pages which send the selected answers to a 'results.php' which then queries the database to the related table to compare the answers with the 'answerkey' table.
I want to selectively query different tables from a database depending on which page the post data came from.
Say if the data came from 'functions.php', i would query the 'functions' table.
How can this be implemented?
I want to selectively query different tables from a database depending on...
Simply include a hidden field in the form which contains whatever flag you would use to determine this condition. Something as simple as:
<input type="hidden" name="sourcePage" value="Functions" />
Then in the server-side code you could retrieve that value:
$_POST['sourcePage']
Basically, the information being sent to the server is already in a form, so when you want to include more information just include it in that form.
You can do this easily by adding a second form input field which is hidden from the user.
For example:
<input type="hidden" name="the_page_requester" value="name_of_page" />
And in your php code you can do the following:
$pageRequester = $_POST['the_page_requester']
The global variable $_SERVER['HTTP_REFERER'] should contain the URL from which the request was submitted
I think this solution is better:
$table = pathinfo($_SERVER['SCRIPT_NAME'])['filename'];
now you can use $table to Assemble your sql.

Post non-input fields in a form?

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.

passing a dynamic html table to php as an array

I have a dynamic html table( rows are added dynamically using java script) and I want to pass whole table to a php script as an array.Is there a way to insert the table data to an array?
I have tried using phpTableExtractor but it dosen't extract dynamically added rows.
I don't see any other reliable solution but creating a HTML form along with the table and have the user click a submit button to save the table. Then you can read all content from $_POST and store it in a database.
Another solution would be to use AJAX requests to store the table content every time the focus changes or something like that. Will make your page dependent on JavaScript though.
What if you fill a JS array at the same time you create the table?
You will use the html just for display but behind you have the data you send to php.

PHP - multiple file upload

I am building a simple CMS for a client. They need the ability to manage employee profiles.
Each profile needs an image.
Currently I have a form for adding employees, and associating an uploaded image with a single record is easy.
Below the record add form, I have a list of existing records (and a thumbnail of the image). These records are printed between form tags, I've got a checkbox next to each record (marking it will delete it when the form submits).
I want to use this form to also UPDATE records; deletions occur first, then $_POST data is parsed and records updated.
When a record has no image associated with it, instead of a thumbnail, a file input tag is printed. Because there is a variable number of records, the file tags are all named image[] so I can easily loop through them.
Question: how do I correlate $_FILES data with $_POST data? Do I have to name each file input to image_<?=$record_id?> to determine which record the file belongs to?
Your solution looks good, i would add record id as index, for ex:
image[<?=$record_id?>];
You can then correlate by array index
instead of
<input name="image[]">
use assocative array, for ex.:
<input name="image[id_<?=$record_id?>]">
Using the default fieldname[] notation, you can't control what indexes PHP assigns to the server-side representations in POST/GET/REQUEST/FILES. PHP'll just add them sequentially and if there's a gap in your form, it'll be gone once the data hits the server.
You can, hoever, FORCE indexes, so that fieldname[7] and newimage[7] all relate to the same fieldset in your form.
You will need to name each file input, and access it via $FILES['unique_name'].

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.

Categories