Jump to content

Displaying without a second reload


djp1988

Recommended Posts

hi,I have a guestbook type of system, and visitors type in a message and send, on the same page is a list of all the messages, but when I type in and send (by GET), the page relaods and the info is sent and is inserted into the DB, however you have to reload the page to see it in the list of messages, how can I bypass this?

Link to comment
Share on other sites

Welcome to AJAX. The idea is you will send an undercover HTTP query to your script, your script will process the new data the way it already does, but instead of refreshing the page, it will send just enough data in a response (also received undercover) that your page can update only the relevant data, leaving the rest of the page untouched.Search the forum for ajax and you'll find some routines, or look at the school.

Link to comment
Share on other sites

Another good option, if you're not familiar with AJAX, is to get the values in another page, store in the DB and redirect with a meta tag to the previous page. That way, you wouldn't have to manually reload the page.For example, you have a page called guestbook.html with Name and Country in a form:

<form action="receive.php" method="post">Name <input type="text" name="name"><br>Country <input type="text" name="country"><br><input type="submit" value="Sign Guestbook"> <input type="reset" value="Clear Fields"></form>

As you see, the action of the form goes to receive.php.In receive.php you could have something like:

$name=$_POST['name'];$country=$_POST['country'];$conect=mysql_connect("server","name","password");mysql_select_db("db_name",$connect);$query="insert into guestbook values ('$name','$country');mysql_query($query);mysql_close($connect);echo "Message stored...<meta http-equiv=\"refresh\" content=\"2;URL=guestbook.html\">";

Link to comment
Share on other sites

Just insert into the database before you do anything else. Then when you load the rest of the page to display everything the new record will have already been inserted. In fact, all PHP processing should be finished before you start to output any HTML code. Look at this page, I have this page to add markers to a Google map and display people who have left feedback. There is not a single line of HTML sent to the browser:

<?phprequire_once 'include/global.conf.php';if ($sess->userid == 0){  header('Location: login.php');  exit();}$content = array(  'page_title' => 'Site Admin',  'hide_markers' => true,  'hide_contacts' => true);$page_mode = form_var('page_mode');$edit_id = intval(form_var('edit_id'));$del_id = intval(form_var('del_id'));$c_edit_id = intval(form_var('c_edit_id'));$c_del_id = intval(form_var('c_del_id'));$errorstr = '';$msgstr = '';################################################################################ Map Makers###############################################################################if ($page_mode == 'add_marker' || $page_mode == 'edit_marker'){  $content['hide_markers'] = false;  $title = form_var('title');  $lat = floatval(form_var('lat'));  $long = floatval(form_var('long'));  $marker_content = form_var('content');    if ($title == '')    $errorstr .= 'You must give a title';  if ($errorstr == '')  {    $marker = array(      'title' => $title,      'lat' => $lat,      'long' => $long,      'content' => $marker_content    );    if ($page_mode == 'add_marker')    {      if ($db->insert('markers', $marker))        $msgstr = 'Marker added';    }    else    {      if ($db->update('markers', $marker, "id={$edit_id}"))      {        $edit_id = 0;        $msgstr = 'Marker updated';      }    }  }  else  {    $content['title'] = $title;    $content['lat'] = $lat;    $content['long'] = $long;    $content['content'] = $marker_content;  }}elseif ($page_mode != 'edit_marker' && $edit_id > 0){  $content['hide_markers'] = false;  $db->sql('SELECT * FROM markers WHERE id=%d');  $db->add_param($edit_id, $esc=false);  $marker = $db->select();  $content['title'] = $marker[0]['title'];  $content['lat'] = $marker[0]['lat'];  $content['long'] = $marker[0]['long'];  $content['content'] = $marker[0]['content'];}if ($del_id > 0){  $content['hide_markers'] = false;  $conf = form_var('conf');  if ($conf == 'yes')  {    $db->sql('DELETE FROM markers WHERE id=%d');    $db->add_param($del_id, $esc=false);    $db->delete();    $msgstr = 'Marker deleted';  }  else  {    $db->sql('SELECT title FROM markers WHERE id=%d');    $db->add_param($del_id, $esc=false);    $marker = $db->select();        if ($marker && count($marker) > 0)    {      $msgstr = '<b>Delete Marker</b><br><br>';      $msgstr .= "Are you sure you want to delete {$marker[0]['title']}?<br><br>";      $msgstr .= "[<a href=\"admin.php?del_id={$del_id}&conf=yes\">Delete</a>]   ";      $msgstr .= "[<a href=\"admin.php\">Cancel</a>]";    }  }}############################################################################################################################################################### Contacts###############################################################################if ($page_mode == 'edit_contact'){  $content['hide_contacts'] = false;  $name = strip_tags(form_var('name'));  $email = strip_tags(form_var('email'));  $phone = strip_tags(form_var('phone'));  $state = strip_tags(form_var('state'));  $rv_type = strip_tags(form_var('rv_type'));  $project = strip_tags(form_var('project'));  $comments = strip_tags(form_var('comments'));  $contact_type = intval(form_var('contact_type'));  if ($errorstr == '')  {    $contact = array(      'name' => $name,      'email' => $email,      'phone' => $phone,      'state' => $state,      'rv_type' => $rv_type,      'project' => $project,      'comments' => $comments,      'contact_type' => $contact_type    );    if ($db->update('contacts', $contact, "id={$c_edit_id}"))    {      $c_edit_id = 0;      $msgstr = 'Marker updated';    }  }}elseif ($c_edit_id > 0){  $content['hide_contacts'] = false;  $db->sql('SELECT * FROM contacts WHERE id=%d');  $db->add_param($c_edit_id, $esc=false);  $contact = $db->select();  foreach ($contact[0] as $k => $v)    $content[$k] = $v;}if ($c_del_id > 0){  $content['hide_contacts'] = false;  $conf = form_var('conf');  if ($conf == 'yes')  {    $db->sql('DELETE FROM contacts WHERE id=%d');    $db->add_param($c_del_id, $esc=false);    $db->delete();    $msgstr = 'Contact deleted';  }  else  {    $db->sql('SELECT name FROM contacts WHERE id=%d');    $db->add_param($c_del_id, $esc=false);    $contact = $db->select();    if ($contact && count($contact) > 0)    {      $msgstr = '<b>Delete Contact</b><br><br>';      $msgstr .= "Are you sure you want to delete {$contact[0]['name']}?<br><br>";      $msgstr .= "[<a href=\"admin.php?c_del_id={$c_del_id}&conf=yes\">Delete</a>]   ";      $msgstr .= "[<a href=\"admin.php\">Cancel</a>]";    }  }}###############################################################################$db->sql('SELECT * FROM markers ORDER BY title ASC');$markers = $db->select();foreach ($markers as $k => $m){  if ($m['id'] != $edit_id)  {    $m['content'] = strip_tags($m['content']);    if (strlen($m['content']) > 60)      $m['content'] = substr($m['content'], 0, 57) . '...';  }  $markers[$k] = $m;}$content['markers'] = $markers;$content['dataset_key_markers'] = 'id';$content['dataset_selected_markers'] = $edit_id;$db->sql('SELECT * FROM contacts WHERE contact_type=1 ORDER BY id ASC');$content['buyers'] = $db->select();foreach ($content['buyers'] as $k => $m){  if ($m['id'] != $edit_id)  {    $m['comments'] = nl2br($m['comments']);    $m['contact_date'] = date($site_cfg['tstamp'], $m['contact_date']);  }  $content['buyers'][$k] = $m;}$content['dataset_key_buyers'] = 'id';$content['dataset_selected_buyers'] = $c_edit_id;$db->sql('SELECT * FROM contacts WHERE contact_type=2 ORDER BY id ASC');$content['sellers'] = $db->select();foreach ($content['sellers'] as $k => $m){  if ($m['id'] != $edit_id)  {    $m['comments'] = nl2br($m['comments']);    $m['contact_date'] = date($site_cfg['tstamp'], $m['contact_date']);  }  $content['sellers'][$k] = $m;}$content['dataset_key_sellers'] = 'id';$content['dataset_selected_sellers'] = $c_edit_id;$db->sql('SELECT * FROM contacts WHERE contact_type=3 ORDER BY id ASC');$content['owners'] = $db->select();foreach ($content['owners'] as $k => $m){  if ($m['id'] != $edit_id)  {    $m['comments'] = nl2br($m['comments']);    $m['contact_date'] = date($site_cfg['tstamp'], $m['contact_date']);  }  $content['owners'][$k] = $m;}$content['dataset_key_owners'] = 'id';$content['dataset_selected_owners'] = $c_edit_id;$content['errorstr'] = $errorstr;$content['msgstr'] = $msgstr;$page = new Page();$page->set_template('admin.tpl');$page->process($content);$page->output();?>

The second-to-last line processes the variables from PHP and generates the HTML from the template file, and the last line sends it to the browser. All processing is done before anything at all is done with the HTML output. And all inserts/updates/deletes are done before the selects.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...