[eluser]solid9[/eluser]
Hi guys,
<b>header</b>
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>barterswapping.com</title>
<link rel="stylesheet" type="text/css" href="<?php echo base_url('css/style.css'); ?>" media="screen" />
<link rel="stylesheet" type="text/css" href="<?php echo base_url('css/topmenu.css'); ?>" media="screen" />
<link rel="stylesheet" type="text/css" href="<?php echo base_url('css/header.css'); ?>" media="screen" />
<link rel="stylesheet" type="text/css" href="<?php echo base_url('css/body.css'); ?>" media="screen" />
<link rel="stylesheet" type="text/css" href="<?php echo base_url('css/footer.css'); ?>" media="screen" />
[removed][removed]
[removed][removed]
</head>
<b>controller</b>
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Ajax_post extends CI_Controller {
public $data = array(
'username' => '',
'header_message' => '',
'member_menu' => '',
'comments' => ''
);
public function __construct() {
parent::__construct();
$this->load->helper('form');
$this->load->helper('html');
$this->load->helper('url');
$this->load->model('ion_auth_model');
$this->load->model('bs_model');
$this->load->library('ion_auth');
$this->load->library('form_validation');
$this->load->library('email');
$this->load->library('pagination');
$this->load->library('table');
$this->load->library('comment');
$this->data['header'] = $this->load->view('header', null, TRUE);
$this->data['footer'] = $this->load->view('footer', null, TRUE);
$this->data['leftnav'] = $this->load->view('body_leftnav', null, TRUE);
$this->data['midnav'] = '';
$this->data['rightnav'] = $this->load->view('body_rightnav', null, TRUE);
//setting default values
$this->session->set_userdata($this->data['header_message'], '');
$this->session->set_userdata($this->data['username'], '');
}
function index()
{
$this->load->view('ajax_post_view');
}
function post_action()
{
if(($_POST['username'] == "") || ($_POST['password'] == ""))
{
$message = "Please fill up blank fields";
$bg_color = "#FFEBE8";
}elseif(($_POST['username'] != "admin") || ($_POST['password'] != "asdf")){
$message = "Username and password do not match.";
$bg_color = "#FFEBE8";
}else{
$message = "Username and password matched.";
$bg_color = "#FFA";
}
$output = '{ "message": "'.$message.'", "bg_color": "'.$bg_color.'" }';
echo $output;
}
function post_action2()
{
if(($_POST['name'] != "") || ($_POST['email'] != ""))
{
$message = "Data captured:";
$bg_color = "#FFEBE8";
echo 'name: ', $_POST['name'];
echo '<br>email: ', $_POST['email'];
}
$output = '{ "message": "'.$message.'", "bg_color": "'.$bg_color.'" }';
echo $output;
}
}
<b>view</b>
Code:
<center>
<?php
echo $member_menu;
?>
</center>
<table border="0">
<tr>
<td align="left">
<b>Published by: </b> <?php echo $users->username; ?>
</td>
</tr>
<tr>
<td align="left">
<h3>Offering</h3>
</td>
</tr>
<tr>
<td align="left">
<div class='show_offer'>
<?php echo $swap->offering; ?>
</div>
</td>
</tr>
<tr>
<td align="left">
</td>
</tr>
<tr>
<td align="left">
<h3>Seeking</h3>
</td>
</tr>
<tr>
<td align="left">
<div class='show_offer'>
<?php echo $swap->seeking; ?>
</div>
</td>
</tr>
</table>
<br><br>
<center><h3>Post a comment</h3></center>
<div id="main">
<?php
/*
$comments = array();
$result = mysql_query("SELECT * FROM comments ORDER BY comid ASC");
while($row = mysql_fetch_assoc($result))
{
$comments[] = new Comment($row);
}
*/
//Output the comments one by one:
foreach($comments as $c){
echo $c->markup();
}
?>
<div id="form_message"><h2>Message Here</h2><br></div>
<div id="addCommentContainer">
<form name="addCommentForm" id="addCommentForm" method="post">
<div>
<input type="hidden" name="swapid" value="<?php echo $swapid ?>">
<label for="name">Name </label>
<input type="text" name="name" id="name" /><br>
<label for="email">Email </label>
<input type="text" name="email" id="email" /><br>
<label for="body">Comment </label>
<textarea name="comment" id="comment" cols="20" rows="5"></textarea><br>
<input type="submit" value="Submit" name="offer_submit" id="offer_submit" />
</div>
</form>
<br><br>
</div>
</div>
<b>js</b>
Code:
$(document).ready(function(){
$("#offer_submit").click(
function(){
var name=$("#name").val();
var email=$("#email").val();
$.ajax({
type: "POST",
url: "http://barterswapping.com/ajax_post/post_action2",
dataType: "json",
data: "name="+name+"&email;="+email,
cache:false,
success:
function(data){
$("#form_message").html(data.message).css({'background-color' : data.bg_color}).fadeIn('slow');
}
});
return false;
});
});
<b>
The problem here is when I press the submit button it reload the whole page,
which I'm expecting not to. Since this is AJAX.
And it's not displaying back the captured data.
I hope someone will help me.
</b>
Thanks in advanced.