Way to re-populate checkboxes on page? - php

I have a bit of an odd/unique situation, where I am currently looking for a way to 're-populate' checkboxes on my page.
First some background on how the page/app works.
1.) state1: You get simple search form. Enter in first or last or state, etc., hit submit.
It queries a DB and posts back to the SAME Page (?mode=results).
Inside of a MAIN container DIV, I loop through the returned records, and create (echo) a TABLE with a certain tabular layout to parse the values from each record/row (name, date, ID#, etc).
If I have 5 records returned from DB, I loop through and create 5 tables right after each other, populating each with the unique data from the record/row.
In each one of these tables I create, along with the data from the DB record/row. I also create/add in several check boxes.
Using some jQuery and on $(document).ready, I listend for the onClick event for all these checkboxes. So when clicked, I grab some data from the checkbox, as well as some parent info, and do the following:
a.) I create/add another element to the stage
b.) Make an Ajax call to an external .php script, so that I can update the SESSION with my array I have build (collecting all the user checks/input)
I build the array in JS/jQuery and use the Ajax call to get this data into my PHP session (anyone knows a way to NOT use this external .php script to update the SESSION var, let me know please!)
So to re-cap so far: There is no "FORM" (tags), just check boxes in multiple tables. There really is no 1-button (submit) event that POSTS all the checkbox data anywhere at one time. Each time a checkbox is clicked (or un-clicked), I update my SESSION array. That if/when a user IS ready to 'check out' (for lack of better term) (leave the page and go to another page), the SESSION data/array is 'current' and the data is ready to be used on any following pages.
so far everything posted above works just fine.
The question I have is: If the user wants to go BACK to this MAIN PAGE with all the checkboxes, what nice, simple/elegant solutions do I have to re-populate all these checkboxes?
I'm thinking the only approach I have is the check and see if this SESSION array exists when main page loads (meaning it's a re-visit, not the first time), and try to loop through this array and somehow target elements on my stage to find the correct table, then the checkbox ID?
Usually use jQuery to walk the DOM - not PHP?
And what about toggling the checkbox with PHP?
Is there a nice way to do this?

I don't really see your problem. I recapitulate:
You have a page with several <table> coming from your database (1 row = 1 <table>)
In these tables you have checkboxes
When you click on a checkbox, you do an ajax call to save the clicked checkbox in session
Your question is: how can I repopulate checkboxes when the use come again on the page
I think you already have the answer: just use what you stored in your session. When you build your <table>, and your checkboxes, test if they are in session:
<input type="checkbox" <?php if (!empty($_SESSION['blabla'][$myCurrentCheckboxID])) echo 'checked'; ?> />
If you want something more elegant, just create a nice PHP class or list of functions to generate your checkbox.

thanks for the suggestions.. this is what worked for me in the end (so far)
function sessionCheck($recordID, $checkBoxID){
for($r = 0; $r< count($_SESSION['userPicks']); $r++){
if($_SESSION['userPicks'][$r]['recordid'] == $recordID){
for($z=0; $z < count($_SESSION['userPicks'][$r]['itemsordered']); $z++){
if($_SESSION['userPicks'][$r]['itemsordered'][$z]['name'] == $checkBoxID){
return 'checked="checked"';
}else{
return "";
}
}
}else{
return "";
}
}
}
usage for MY project is like this:
<?=sessionCheck($row["id"] ."-A","Testimonial History") ?>

Related

POST Data Without Form

Basically I have a bunch of data I get from a database and put onto my page in a table. Right now I have the user type in the name, session, etc. in the table and that is sent as post data into the next PHP page, which I then use to lookup more stuff in the DB and so on and so forth.
Obviously that's not a great user experience; it would be much easier to simply CLICK the item in the table and everything gets sent automatically into the next page.
I'm not sure how I'd go about doing this.
My tables are first and last names for now, so if you click a certain row it should go to the next page sending each cell as data.
EDIT: Some examples:
Traditionally you do this with a form
<form method="post" action="pageDataIsGoingTo.php">
to send data to the next page. However, I don't want to do this with a form; but rather when they click a URL and/or button that sends the data. I can "hide" the data from view I suppose, but I still don't know the function to actually go ahead and do that.
Would I make a javascript button/function that sets something in an invisible form?
You can use invisible/hidden form fields.
That might be your best guess.
Javascript would be a good solution if you wanted an ajax POST call, but you want to load other page.
So hidden form fields are your solution.
Parallel with table data.
You need to embed hidden fields and your visible item row within a form
(so each item row contains also a form & hidden form fields and visible submit button,
which you can style with css)
This presuming that your table contains more items which you can choose to send.
Although I would do this with backbone & jquery and do it all in ajax.

PHP:How to save checkbox values temporary before submission

I'm a bit rusty in PHP,
I'm curently doing an PHP assignment where a user can select and save listed images/data/values into their own collection.
I have inputted all the data and printed it out in a repeat region with recordset paging.
I'm confused about how I am supposed to save a checked checkbox temporary before submission as there a more then 1 page as I'm using recordset paging to output the options.(Meaning: i have selected 2 values in the first page then i click next page to select balance values and finally submit my selection)
TIA
I have read an article on storing in session , that is the solution I guess, but I wonder how I'm supposed to send the value to the session when chaging the page (recordset paging generated by Dreamweaver)
To clarify the previous answers, you will most likely want to create a new $_SESSION variable for each check-box and associate a boolean with it.
You can store the result of a form post in PHP's $_SESSION variable.
Read this post for more information: Storing Form Data as a Session Variable
Also, there has to a tutorial or something in Google Land.
If you need to save the form results without submitting the form, try a JavaScript/AJAX approach. The idea is that you actually do submit the form, but in a behind-the-scenes kind of way (that is, the user never notices it). Essentially, you're going to want to build a "autosave" functionality. Look at these two posts:
Autosaving Form Input Using Prototype and PHP
AJAX Autosave functionality
They probably won't fit your needs exactly, but they should give you a good idea of what you should do.
Note: both of these posts use a timer to trigger their autosave functionality. I would suggest tweaking the trigger to detect any changes in your form instead.
Store them in _SESSION and process when needed.

Auto refresh in PHP

I have piece of code which refresh data periodically (after 5 sec.), and put it into a table.
This table has sorting option, and checkbox to select a particular row.
Now problem is when i want to sort or choose a row using checkbox, because of auto refresh it set whole table data in previous position. Means if any data i had sorted will not show sorted and/or checked row will be unchecked again.
Please provide me some suggestion how to handle this issue.
Thanks
Your alternative might be to do an AJAX call to pull the data periodically instead of refreshing the whole page via PHP. That way you'll be able to send the correct parameters to handle the sort towards the logic within PHP.
OR
You can push named parameters/actions within the url for sorting purposes. Then use URL's on the table header and the page reloads with a url of something like:
http://example.com/table/sort:asc
http://example.com/table?sort=asc
And then your logic could appropriately pick the previously selected areas up.
if (isset($_GET['sort'])) {
//Do sorting stuff
}

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.

fetching data from database in randomly generated button

i found it difficult,,,, fetching data from database while a buttons are randomly generated in for each how can i fetch
Without understanding what your question really is, you could go trough the mysql query result like this:
// button_text is a database column in this example
while ($row = mysql_fetch_row($result)){
echo "<button type="button">".$row['button_text']."</button>";
}
But to really help you, you need to rephrase your question!
I'll make some assumptions for what you are trying to do:
1. You have buttons that fetch more info from a db
2. The buttons are "randomly" generated, and each calls different info (pets, cars, etc).
You may or may not be using ajax, but I will describe it basically assuming straight php, and you are using a single page (for clarity in my explanation).
What you need to do is have each button either be a link or a submit for a form. This just depends on whether you want to use GET or POST. The buttons will have php generated links (if GET) or values (if POST). For example, using get the link could be "www.file.php?cat=cars". The button would just have the value of "Cars", and since bother are generated, that shouldn't be an issue keeping them the same.
When the page is now reloaded based on the click, the top of the page has a query in it to get the new info. For example, it would run a query looking for all items that have the car category. Then the new information would be displayed, and the new random buttons would show.
VERY IMPORTANT: Sanitize all GET and POST values before using them in a query

Categories