let user disable certain array items? - php

This is the situation, I've got a big array ( 280 items ) which creates a really big form by going trough loops.
I want the user to be able to disable certain items, so they can 'pre make' their form ( and skip the not used ones ), You are probably going to say i can just remove what's not needed. But they need different items from the array every time they use it.
So how would i make a page where they can just use checkbox to 'change' the form. ( if possible at all.)
Thanks,
Mike
edit:
Did not think of sharing code, here:
The array : http://pastebin.com/EnwHsqtK
the form : http://pastebin.com/y2XSFBG4
the page which makes a .txt file from the given answers. http://pastebin.com/UaUcsj2z ( not sure if anyone would need this. )
Might be a bit messy. I'm new in PHP, and only programming for a year. Also dont mind the language please.

If you want to permanently record form preferences/settings for each user, you'd want to create an additional table or column(s) in your database for this, give the users additional checkboxes on the form to indicate their preferences, receive this input and store it in the database, and of course finally disable certain fields based on their settings.
But if you just want to give the users a temporary way to disable certain fields (with no preferences saved permanently), you would use JavaScript in your output. You would add more form controls (checkboxes or buttons or whatever) to the HTML and then add JavaScript code snippets into that HTML to disable form elements when the user clicked on the controls. This kind of making changes when users click is called "triggers that fire based on events". The most commonly-used event is called "OnClick()" and the JavaScript code for it will execute when a user clicks on something.
Many folks who use JavaScript also find it helpful to use the functions in the jQuery library instead of raw JavaScript. To do this, you just add one line of HTML to the top of your page to include the JavaScript libraries from a publicly-hosted server, then you can use jQuery commands in your page.
The only thing to remember when you first start using JavaScript/jQuery is that it only runs on the client browser - its code cannot talk directly to the server, the database, or many things you can access in PHP. JavaScript/jQuery is specifically used for making your HTML pages more interactive. Plain HTML doesn't have much razzle-dazzle. JavaScript allows users to do things like enable and disable form fields on-the-fly.

Related

best design practice in PHP for navigating from one form to another

I have an application in which I display a form so a user can search for client records based on last name. After entering search parameters, the record or records (there could be multiple clients with the same last name) are displayed. I then want the user to be able to select a client record, possibly with a radio button, and hit one of two buttons: Display details, or Create Reservation. The Display Details button should cause a new display with details of the selected record. The Create Reservation button should cause a new form, with its own handling, to be displayed.
Now, I know I can set things up according to this login
<?php
if (display button was pressed)
{
php code to retrieve more data and display details
}
else if (create reservation button was pressed)
{
php code to generate and display the reservation form, with appropriate handling
}
?>
display the original form with the search results
The problem is, I end up with really ugly, hard to read code because the php code to generate and display the reservation form is lengthy, and needs its own validation, database interaction, and form handling. The code, to my Java-oriented eye, looks ugly and non-modular. Plus, the code for handling the reservation form is icky, with lots of flag setting to determine if we are in form entry mode or form handling mode. I would like a much cleaner way to do this. So my question is, what is the best practice for handling the situation where there are multiple buttons and the action associated with each button is complex?
I could call a function, obviously, but I still end up with the ugly flags determining which state the script is in (are we displaying the reservation form or handling it?). I could create another php file and include it, but the ugliness persists. Or, I could use header, and pass the client record id in a session variable to the new php script. But that would mean a second, unnecessary retrieve from the database to get the client information again.
All the code examples I see on the web show very simple processing after a form button is pressed. What is the best way to do complex processing and displaying a second form based on a button press?
Have you considered using a framework like Laravel for your site. It would seem to me that you must be doing this "manually". With the complexities you described, having a system with routes and "build-in" functionality (like Eloquent ORM) might serve to simplify things for you.
I would go for using ajax and a rich jQuery plugin (or some other framework) to do what you want.
Basically you will handle lists and the functionality that you mentioned with the php reading data and jQuery scripts to dysplay it. And the information that you have to show would be through ajax. Or when you want to edit.
Here is a cleaner example of what you need:
http://jqueryui.com/dialog/#modal-form

Editing information from a MYSQL database from website frontend.

I wonder is someone can help, I'm building a website, which is driven from a database. It will consist of user submitted information.
Currently all the information is pulled from a record in the database and is being output via a PHP echo, what i would like too do is add a feature that would allow me to edit the information if incorrect from the websites front end.
I have seen many websites have some form of edit icon next to information in there databases, when clicking this icon the echoed text changes from text to a text field and you are able to update the field being echoed from the database.
Im a designer so have limited knowledge of how functionality for this kinda feature might work.
please could anyone let me know how something like this might be achieved.
many thanks.
You would need to build some kind of javascript functionality to allow the in-place editing of those data bits. One possible solution is a jQuery plugin like jEditable.
Then you need to build a server-side script in something like PHP or ruby where it would take the submitted information and update the database.
well the process is the same for the front-end and back-end. it depends whether you want you build a password protected editable forms or just editable for everyone.
One way you can do this is
echo your information into text inputs
give them a css class that removes the border and makes it transparent
make it readonly(so someone couldnt tab into it and change it)
add a javascript onclick event that changes the class to a normal
text box that is not readonly
add a javascript onchange event that uses ajax to save the new
information into the database when they are done typing, or press enter
after the ajax is done turn the text box back to the first css class
EDIT also add a onblur event that changes it back as well
you could even change the cursor for the text input to a pointer instead of the default (text) cursor so that is looks like you can click on it.
.
now html5 has contenteditable attribute which you can set for elements
simple example:
www.hongkiat.com/blog/html5-editable-content/
simpler demo:
https://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_global_contenteditable

What technology use to make a webpage to update in real time?

I want to make a webpage where an user can add the title from a book he has read. These changes are reflected in real time on a list that contains all books he has introduced on the database, without the need to press any "reload" button. By example: there is no need to refresh (F5) the page to see the last book added.
I don't know if I can do this in PHP or in any other language, so I would like to know which is the best suited for something like this.
Thank you.
I think you are looking for Ajax. Would be able to asynchronously update the section of the page (the post in this case) without the need for page refresh.
You will want to do this with javascript, using the onchange event, and for a discussion on this you can look at: Call Javascript onchange event by programatically changing textbox value.
Basically, you react to the data being changed, then just send it immediately over using ajax to the server, but, you need to be aware of two things.
First, how will you handle errors, such as there is no book with that title, or the length is too long. I tend to put the error message in or by the place where they had the bad data.
The other is that you need to pass back the id when the data was inserted, so that when they change it again you can just do an update, so you will need to store that. I tend to put the database id I need in the element id, but you can keep it in an array in javascript, since it will maintain state for you.

Editable table in PHP

In a PHP application I'm building, I'd like to have an 'editable' table. The idea is that each row will have an edit button, which, when clicked, will replace certain fields with text fields and select lists and change to a save button. When the user clicks save, the data data should be validated and changed if appropriate.
I'm mainly tackling this as a learning project (I'm aware there's a ton of stuff already out there) and to see if I can get anything 'cool' working. I've created a PHP table-generating class that can take an array of objects as a datasource, and can have columns created based on those class methods.
e.g.
$table = new Table($dataSource);
$table->addColumn('Name', 'getName');
$table->addColumn('Amount Due', array('getOrdersManager', 'getTotalAmountDue')); //First calls getOrdersManager() on each data item and then calls the getTotalAmountDue() on the result
I'd like to try my hand at extending this to be able to the table row and have those changes reflect on the corresponding object in the data source.
I don't really have very much experience with AJAX although it's clearly going to play a very important role in getting this to work correctly.
Any tips on how I should approach such a task?
Edit: I'm not really interesting in looking at Ajax libraries at this point (I do have some experience with jQuery). I'm more interested in learning the basics of Ajax at this point.
my tip is to use jquery(does most of the heavy lifting for you and is easy to learn).
The idea is that each row will have an
edit button, which, when clicked, will
replace certain fields with text
fields and select lists and change to
a save button
http://api.jquery.com/click/
When the user clicks save, the data
data should be validated and changed
if appropriate.
http://api.jquery.com/jQuery.post/
Some things to be aware of/think about:
Are you going to send every field change to the server, or only the whole row (the latter is more resource efficient, but not necessarily as accurate)
How are you going to ensure the data displayed stays accurate even if the update to the server fails for some reason (either a network failure or a DB/validation error)
How will you ensure the user has permission to update the record and that you don't open a security hole by allowing the AJAX responder just to update whatever record it is told to. My approach has been that if a record is shown in the interactive table then the user has the permission to update it, so a cache of record IDs is held in the session when the table is created
Are you going to load options dynamically? If you don't, then a long table can end up containing a lot of HTML because of repetition of the select controls, but again it is more resource efficient not to have a request every time a user clicks into a dropdown. One compromise might be to put the options into a hidden HTML field and load them dynamically into the correct place when a user clicks a dropdown

Multi page ordering form php or ajax

I'm looking to create a multi page ordering form the first page would contain some dropdown and text fields, the second page would contain more text fields, the third page would be an order summery with paypal payment option.
I'm just wondering what the best way to create this order form is. I've used sessions in the past but never with users entering in text and picking items from drop downs, does anyone have any resources for doing this? Or does anyone know of a jquery or other ajax example or plugin I might be able to use and modify.
any insight would be a big help.
thanks
The simplest technique might be to use hidden form fields to carry fields from previous screens through to the final screen.
Just make sure you validate all the values when the final screen is submitted to make sure that the user hasn't twiddled the data.
You don't need to do pagination at all if you don't won't to. Just use css to show/hide the "pages". It doesn't sound like you have to save the "state" at any point.
But if you want to do multiple pages, use a session or a cookie to track the user. Then save the data to a database a mark it as incomplete. On the final page, retrieve it all and show it on the page. The server can't tell if a request is ajax or not, so it doesn't matter what you use for submission.

Categories