View Full Version : Javascript
stewartf
05-26-2009, 10:40 AM
So i had my page working great. then we decided that it loaded entirely too slow...so now i use ajax. everything was great until...i needed to add a record to my database. the record which is added to the database is displayed in the bottom of the screen in a table created using .php. all this worked great before since .php would refresh the page each time you executed an action. so the table just displayed the contents of the database each time the page loaded.
with ajax, the page doesn't have to reload. so now the record has been added to the datbase fine but doesn't show up since the page doesn't refresh.
i was thinking that i would just create a javascript that would take care of it, but then remembered that i have been programming javascript for a total of 3 days!
here is what i would like to happen:
the user clicks the add button and the record which was added shows up in the table below without having to reload the entire page.
pghcollectibles
05-26-2009, 11:32 AM
dont you use php to add the item to the db? the ajax pulls data from a file. what is the ajax in your site actually doing? there may be other reasons for your page to load slow. do you have lots of pictures? are they being resized to display in the page?
here (http://www.w3schools.com/php/php_ajax_database.asp) is an example of pulling the data from the database. another script would add data then reload the screen
how do you write ajax if you dont know javascript?
stewartf
05-26-2009, 11:42 AM
I am using ajax to send the data to a php page which then enters ther data into the database.
Before I starting using ajax, the page was exclusivly using php, so the entire page refreshed each time and so the 'result box' would be refreshed also. Now that ajax is being used, I am changing the DOM with javascript based on what ajax returns. All has worked well, except that the only way for me to display the record which was added is to reload the page which I would like to avoid if possible.
stewartf
05-26-2009, 11:45 AM
I am having troule explaining myself. Sorry if this is coming out as a garbled mess! :)
stewartf
05-26-2009, 11:47 AM
As for my javascript, it is amateur at best which is why I am sure that I am having this problem.
pghcollectibles
05-26-2009, 11:53 AM
look at the example i gave you. after your script updates the db, that script would pullt he correct data from the db
stewartf
05-26-2009, 12:00 PM
That is actually the example that started to make ajax make sense for me.
the php script which is creating the box is directly on the page which is why I had a problem. I was hoping to preserve the box as it was, but I guess since the page is being changed so dramatically I will have to have build it this way.
Thanks for the help. Sometimes you just need a knock on the head. :)
pghcollectibles
05-26-2009, 05:13 PM
if you get stuck somewhere feel free to post the code you have so far. ideally you would have a php page making the layout, a js page to do the xmlhttp and the php page that queries the database.
like in this code:
<select name="users" onchange="showUser(this.value)">
<option value="1">Peter Griffin</option>
<option value="2">Lois Griffin</option>
<option value="3">Glenn Quagmire</option>
<option value="4">Joseph Swanson</option>
</select>could look like sort of like this:
<select name="users" onchange="showUser(this.value)">
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con){
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
$result = mysql_query("SELECT * FROM Persons");
$i=1;
while($row = mysql_fetch_array($result)){
echo "<option value='{$i}'>{$row['FirstName']} {$row['LastName']}</option>";
$i++;
}
?>
</select>
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.