What I have is a list of client names which when I click them I want some mysql data to load in the div next to their names and so on,
I know already I can do this with an AJAX request by binding an onlclick function to divs that their names are stored in and then using that to trigger the Ajax request to get the data and then append it to the DIV on the page.
I'm wondering if there's any other way to do the same thing which is light weight and fast and efficient?
You have either:
AJAX
iframe
Load whole page
Those are really your only choices.
Related
I wonder if it is possible to run a PHP file in the background on my website?
What I want to do is to show a form with inputs and dropdowns. The content of the dropdown is taken from a table in a MySQL database. Next to the dropdown is an Edit button which opens a Bootstrap Modal and shows all the content in the table for the dropdown. Here I can make changes to the table. Then I want to go back to the form and apply the changes I jsut made (i.e. select a new post from the dropdown).
I know how to do all of this - BUT - when I save the changes to the dropdown table, a php file is called to perform the SQL syntax and then redirects the user back to the form. Every input you have made in the form is lost since the page is reloaded.
Is there a way to make it work like I want?
I understand that you need to implement Ajax to do the background operation. Ajax request runs in background, its entry and exit is handled through Javascript functions . See here : http://www.w3schools.com/ajax/
As some already noted, Ajax is the way to go in this case.
An idea would be to use ajax to send the modal data to the server (saving the new options for the dropdown) when the user clicks in the "save" button (or anything equivalent). When this ajax call finish successfully, you could reload the dropdown with another ajax call (or even better, save the modal data in a temporary structure in JavaScript and use it to update the dropdown once the first ajax call is successful, hence avoiding some work on the server)
Note that the page won't reload in the whole process.
The simplest and the easiest way to do this is to open the modal in a new window.
example
Modal
And you can attached this to your select option value with a on-click event.
I am designing a web page. I want to have the options(of another drop-down list on same page) based on the selection made by the user.
I am providing a drop-down list to the user called Application. Depending on the Application, I want to query sql and want to show only the options that are valid for selected Application in another drop-down list.
I want to get the value of Application on the same page (one that user has selected) and by getting that value will query the sql accordingly.
Once the page is loaded, the PHP cannot do anything more.
You cannot use PHP based on user interaction.
The only way to do so is through javascript.
Using ajax, you can retrieve data from a certain URL on your javascript.
A procedure would be like this:
User selects something
The javascript(ajax) loads a URL
In that URL, use your PHP to fetch a certain query
The result would be sent to javascript, so display the result.
Here's a good page explaining ajax:
http://wabism.com/ajax-tutorial-with-jquery/
you have to call an ajax function on the onchange event of dropdown that gets data from database.
ajax is your friend. Either you can use raw javascript or jquery( easier ). Your steps would be..
1. Write a php code block, most probably a function, which takes the selected application option from post, queries the database and outputs the result.
2. In your current page, write a js code that fires the ajax request each time user selects an option. This ajax will send the selected option to the php script by post method and u can grab the output upon the success of the request.
$('.selectBox').on('change', function(){
// do your ajax here.
});
something like that..
I have created a page with Mysql rows displayed as a dynamicly created image grid.
The grid display i have full control over, but i need to take the (Mysql) Row ID from each image displayed and use it in a pop up of some sort.
I don't like having a full page for each Mysql row.
It could be passing it on to a new smaller info page(dynamic) or it could be a popup with the info(dynamic).
I would prefer the popup, since i would guess this is easier on the eyes.
So what i need a suggestion on a popup function, which will load/set the row ID on click.
I have seen Jquery solutions for making popups, but i need to pass in the variables allready set by each displayed image. and this is where i fail.
Thanks in advance.
/Niels
**
This is one of the things i have tried, but i can't really figure out the way to pass it in the right way:
echo "<p>Show"
When you render the page in PHP, you could use a data attribute <div data-mydata='foo' /> to store the ID you need. Then, with jQuery, use the data() function to access the data and render whatever popup you need.
HTML 5 data attribute: http://ejohn.org/blog/html-5-data-attributes
jQuery Reference: http://api.jquery.com/data
On one of my pages I pull a table of data from the database and display it to the user, giving them the option to update the values for each row. My question is what is the best way to do this?
Currently, i'm stuck between two options. Option one is to display one form with all of the inputs. My other idea was to have each row be a form itself. Is there an advantage to one over the other? Right now i'm leaning towards the the latter of the two - as it seems like much less overhead to only POST data for one row as opposed to all of them for a single update.
You should certainly define separate forms for every row. No need to send all the data all the time.
Semantically, they are also separate forms. It is also easier to handle the data you get on the server side.
you could set it up so that when a record is clicked the data for that record(id,name,other...) are loaded into a form in a jQuery ui dialog then have two buttons on the dialog (save,cancel)
Save: use ajax to send the data to a php data helper which will update the record in the database
cancel: clear the fields in the form and close the dialog
I have created a page in PHP, in that i need to load a dropdown2 corresponding to the value of dropdown1 without refreshing the page. The values for those dropdown's are from the database.
I dont want to refresh the page for every click.
two when you have sent your page to the client, PHP is done. all you got there is JavaScript.
Ajax: you can use Ajax calls, when your first dropdown is selected. define a function in JavaScript, so when an option in the first dropdown is selected, it will call a URL, and send the ID of the selected item to that URL, and fetch the XML result and use that to populate the second dropdown.
JavaScript Arrays: this approach is like the other, the difference is that all data of both dropdowns are already sent to the client in the page, as Javascript objects, or arrays. by selecting an option from the first dropdown, your JavaScript function will populate the second dropdown using local Arrays, or Objects, instead of calling an Ajax call.
the second method has the benefit that changing the second dropdown is faster, and will not require another connection. but the first time the page is loading will take longer, because all of your dropdowns data should be loaded first. I have used the second method in some of my pages, but if you have a tremendous amount of data to fetch for each option of your first dropdown, the better way is the first one.
I recommend using a well-known framework for these, like jQuery. it will ease all of your
work. you can call ajax calls, and change children of the second dropdown easily.
edit:
in the first method (AJAX call) I said load data from XML. I meant making an AJAX connection to the server, to some PHP page that will accept an argument, like the value of selected option in the first select tag, and then searches the database on the server, fetches related results and returns an XML document that have all the vlues for the second select tag. and in your JavaScript function that made the Ajax request, you can parse that XML and create option tags for second select tag on the fly, based on the result XML. your PHP script accepts a value and can do anything based on that value. the XML part, is just a transfer tool.