Take the 2-minute tour ×

I have a form on one page, some of the form data is pulled from various tables in my mysql database -- the tables hold the options for different selects, so the following is the way my data will look after the php scripts are run:

 <select name="vehicleStyle">
    <option value="empty" selected="selected">- select -</option>
    <option value="coupe">Coupe</option>

I have a second form that loads over this page in an iframe, this 2nd form allows me to update the data included in this mysql table so that I can add options to the form on the fly. Everything works absolutely fine and wonderful however, if I update (add a value) to this table on the fly, I have to refresh the page in order for the new data to show up in the first form on the main page. I've played with auto-refreshing the page, but that's kind of obnoxious.

Is there a way to add this form data directly into my original page via jquery?

I think twitter does something like this when you add a tweet (I don't use twitter) but it adds your recent tweet onto the top of the page without actually reloading the entire page.

Thanks so much!

share|improve this question
add comment

3 Answers

up vote 1 down vote accepted

I believe the best way is to use the load jQuery function (http://api.jquery.com/load/). It lets you load a page from a url directly from javascript.

Here is what I would do:

  • move the creation of the select to a new url (fillselect.php for exemple)
  • load the page without creating the select: create a div tag instead (name it divforselect for exemple)
  • in the onload javascript event of the page, write this: $("#divforselect").load("fillselect.php") This will result in the div tag innerText set to the content of the fillselect.php page. So the url fillselect.php will not be a complete html page but will only contain the select tag.

After that you can call $("#divforselect").load("fillselect.php") whenever you want to refresh the select!

share|improve this answer
this sounds like what I'm looking for and I appreciate the response but I can't quite wrap my head around what I would actually be doing -- I'm pretty new to javascript / jquery etc -- this fillselect.php page would simply be a blank page? How do I actually get my data into there? Currently the page I'm using like this is a mysql query result that loops through the database to create the selects, I've tried using .load() to load this on submission, but I can't get it working write –  Thomas Sep 23 '10 at 17:32
@thomas -- this might give you some guidance on how to do ajax calls: net.tutsplus.com/tutorials/javascript-ajax/… –  Azeem Michael Sep 23 '10 at 19:12
@thomas: the "fillselect.php" page would be filled with that exact same code you use now to fill your select, you would just put that part in a separate page so that you can load/reload only the select. –  GôTô Sep 24 '10 at 5:52
thanks so much :-D –  Thomas Sep 24 '10 at 16:17
Glad I could help –  GôTô Sep 24 '10 at 16:25
add comment

I think you want to do DOM manipulation using JS. All you have to do is append a new OPTION tag with the user-input value to the SELECT like:

var option = $('<OPTION/>');
$(option).html('INSERT YOUR VALUE HERE');

Remember: this is adding an OPTION just on the client side. So if your actual form submission (in your iframe) fails to push the data to your database, this would represent an inconsistent view for your user( where he would think new OPTION is created but its actually failed).

So, you would have to tie your UI update with your backend response. Typically, you would refresh this UI in the AJAX response of the call updating the backend.

share|improve this answer
add comment

Maybe you should be using AJAX to populate the boxes? Then you could have it refresh the data anytime you want.

jQuery Ajax

share|improve this answer
add comment

Your Answer


