How to update a page dynamically with database information using php - php

I'm coding my first eCommerce site and in the planning process I've run into a problem. Basically on the product screen the user is given 3 drop down menus for style, color, and finish. Now based on these I want to display the relevant information from the database to display; price and description. I'm not sure how to go about doing this. Is there a way to dynamically query the database without refreshing the page or using a submit that requires more user interaction?

You need to use ajax ajax can load the info you want.
Here is an exmaple of what you want with selection box, with a tutorial.
http://www.w3schools.com/php/php_ajax_database.asp
but then you need to replace the sql query with the query you need so it sends back the information of the product with the right style, color and finish.

Related

web technique for lookup another table

I'm new to web development and I've been studying cakephp and I have this situation where I want to know the best practice.
Imagine a user creating a new Customer in the application, in the form he has to choose the customer's sales group, and this table has so many records, that's using a drop down list isn't a viable option.
I was thinking in the user press a button, then open a modal window. This new window would have a grid, with search options (for filtering the data) and the user would choose one, and go back to the original form, bringing back the sales group selected by the user.
What's the name of this technique, and is it a good option? How to do this in cakephp?
Best regards
Your question is about the front-end, so it doesn't matter if you are using CakePHP or another PHP framework.
What you want to do is make an ajax call to the server on opening the modal window, so it populates its' contents with your grid. Then, proceed as planned.
On the other hand, you can use something like Select2. This way you can have searchable drop-down menu right in your page and the user won't need to open a modal window. It's just more convenient for the user.
You can check the examples for Select2 here.

How to execute mysql_fetch_row() upon a button click

I am tying to put some user data from the database using php.
The database contains so many record and i am displaying it using mysql_fetch_row() and this gives me the fields of first row.
Now the problem is a have two buttons namely next and previous. And when a click next the fields in the next row has to be obtained the they should be inserted into page without loading the page once again. I am facing problem with this.
I am using php for server side programming and html, css for client side programming.
I think that no one will give you the answer. Too many to write. You need to read about ajax (this is the technology that will allow you to do some action without page reloading).
You need to write javascript(you can use some library like jquery) function which will open some php page with the data that you want and then update your site.
Here you have some nice tutorial:
http://www.w3schools.com/jquery/jquery_ajax_intro.asp

Load data into textbox on selection of dropdown list

I have a student management system project, where i am taking student enrollment form online using custom php apps.
When i am inserting enrollment form at the very first beginning of the form there is a drop down list of student iD. my client wants if he / she selects the ID of a particular student from the drop down list, his data will appear in to the text box automatically based on selection.
I know that here i require javascript but i am not sure how to do that.i have searched but didn't found any thing relevant exactly. Can any help me out.
Thanks
You need to first capture the onchange event of the drop down list. When this event fires, you will need to run javascript that makes an AJAX call to the server in order to retrieve the student's data based on the selected student in the drop down list. You will need PHP/MySQL server side code to handle this request. When the AJAX call returns with the data, you will need more javascript to populate the textbox where you want the data displayed.
I won't write the code for you, but hopefully this is enough to point you in the right direction.
Edit: If you need more details, lookup how to create an AJAX call in jQuery. Your requirements are not uncommon, so you should be able to find examples that are very near to what you need. See a relevent tutorial: jQuery AJAX Tutorial

Is there a way I can change where a PHP page selects from a MYSQL database by using a form?

I have a website and I want to make it easier for someone to change certain information being shown without them having to edit the HTML/PHP and using FTP.
At the moment I have this information in a php file which is included in the MYSQL query.
It would be a lot easier if this was done using a form, say a text field where a person can type the table name and it updates on the main page and starts displaying that table instead.
Sorry if I haven't explained this well. :(
I have a good news for you.
Every php/mysql-driven site in the world is made this exact way - to edit site contents using HTML form.
Even first PHP version name was PHP/FI, stands for Form Interpreter.
Even better, a site user doesn't have to deal with mysql - it's all being done in PHP. No need to type table names into form field - all table names already written in PHP code.
Usual PHP application being connected to just one mysql database - so, no need to choose.
As for the tables, it's being done this way: a user selects some human-readable matter, like "Latest news" and being redirected to the PHP script called, say, news.php. this script runs a query for the news table in the database and outputs some HTML formatted news highlights!
Even more, you don't even need to program! There are plenty of ready-made programs, such as Wordpress
store what you want to be editable in a mysql text field.
remove tags you dont want him to see
in the form echo the editable information in a textarea
have him edit
add tags
update the mysql
note depending on the users knowledge depends on how many tags you would like to remove/add. the less per a field the easier.
on more complicated things i like to have the person log in. if he has permission then all the editable fields have an edit button. if he clicks it it goes to a page with a form that he can use to edit that 1 field

Php Two Combo box in a form control each other. How?

On php page in a form, One combo box has list of my Customer from mysql table customer. Another combo box has invoiceno stored in invoice table which has respective customer records. I want to select customer from first combo box and filter invoiceno from the second one according to the customer. Any one help me for php or java or Jquery or both codeings? Means if I select customer1 then in the second combo box should show all invoiceno respective to the custermer1. No Refresh or ReLoad or Post form Pl. If I get the first selection in a php variable format example $customer, it is enough for me. Thanks for any one help me.
Based on what is given, i think one is only restricted to pushing you in the right direction. In a case where you there a large number of customers, it most likely be that you are working with a database, thus the following process:
The Page of the selects is where you will need to make an AJAX request on change of the select element.
Using your JavaScript of your favorite JavaScript library you'll make the request passing a value that you'll query your database for.
Ofcourse you'll need to configure your PHP for GET or POST depending of you AJAX request, then query the database.
Format the databases output to be a valid HTML of your selected element.
Earlier you'll have configured your AJAX script to populate the proper element once the request has been successful.
You dont want to POST to the script. You dont want to invoke any server side activity. And you want to get the value user selects into a PHP variable. From what I understand, this means, you dont clearly get where PHP plays a role. The way you want it, you might want to use XAJAX : http://www.xajax-project.org But even this causes various REQUESTs to the PHP script internally.
I would suggest the below:
Do a natural join in the SQL query.
For a customer C1, there might be 100 Invoice numbers I1. The result of the query may be outputted in a JSON format. Something like this:
"RESULT" : [
"C1" : ["I1", "I2", I3"],
"C2" : ["I11", "I22", I33"]
]
ALL the data will be sent to the browser. This data can be stored as a Javascript Object. Use JSON.parse("<PHP response here>");
Whenever the users selection changes in a combo box, have a function in Javascript to load the corresponding values in the second list.
EDIT: In case, you are dealing with a larger database, and you expect a larger dataset, I would HIGHLY recommend XAJAX -- Simple and easy! :-)
If AJAX is not an option, you must load all the things (include customer & invoice), then you can use pure javascript/css to do that. It's a dirty work around, but it works.
First, let says that you have n customer, so you will have 1 combo box to select the customer; and n combo box for the invoices that associated with them. Those invoice combobox may have id = their ids in database.
Hide all the invoice combobox, with css : display: none
Use javascript: onchange to get the change event in the customer combobox, then show the approriate invoice combobox(display:block). This can be done easily by css property of Jquery, or simply manipulate by javascript replace function (to replace the html class of the combobox)
I'm sure that this way works, but the price is that you must load all data of customers & invoices, which maybe huge. So that if possible, you should try the AJAX approach like Thrustmaster & Babiker propose.

Categories