I have some problem with position off my tables.
here you can see what is looking now:
http://postimg.org/image/auv0lum57/
My controller:
?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Manage_products extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('Products_model');
$this->load->library('form_validation');
}
public function index()
{
session_start();
$this->data['products'] = $this->Products_model->get_all();
$this->data['title'] = 'Product Management';
$this->data['message'] = $this->session->flashdata('message');
$this->load->view('main_view', $this->data);
$this->load->view('manage_products', $this->data);
}
view off manage_products
Products
table, td, th
{
border:1px solid green;
}
th
{
background-color:green;
color:white;
}
#infoMessage p{
padding: .8em;
margin-bottom: 1em;
border: 2px solid #ddd;
background: #FFF6BF;
color: #817134;
border-color: #FFD324;
text-align: center;
}
</style>
</head>
<body>
<table width="700" border="0" align="center">
<tr>
<td><h2>Parduotuvės </h2></td>
</tr>
<tr>
<td><div id="infoMessage"><?php echo $message;?></div></td>
</tr>
<tr>
<td><input name="New" type="button" value="Pridėti naują" onclick="window.location='product/add'" /></td>
</tr>
</table>
<form name="frmproduct" method="post">
<input type="hidden" name="rid" />
<input type="hidden" name="command" />
<table width="700" align="center">
<tr>
<th width="150"><strong>Parduotuvės pavadinimas</strong></th>
<th><strong>Prekės identifikatorius</strong></th>
<th><strong>Nuoroda</strong></th>
<th><strong>Redaguoti</strong></th>
<th><strong>Ištrinti</strong></th>
</tr>
<?php
foreach ($products as $product){
$product_id = $product['id'];
?>
<tr>
<td><?php echo $product['pavadinimas'] ?></td>
<td><?php echo $product['prekesid'] ?></td>
<td><?php echo $product['linkas'] ?></td>
<td><a href='product/edit/<?php echo $product_id ?>'>Redaguoti</a></td>
<td>
<?php
echo anchor('product/delete/'.$product_id, 'Delete', array('onClick' => "return confirm('Ar tikrai norite ištrinti?')"));
?>
</td>
</tr>
<?php
}
?>
</table>
</form>
</body>
</html>
I think problem is with line 22. I must load main_view, but my table must be in main_view, not in bottom. How to fix this problem?
I think I under stood correct try something like
return $this->load->view('name', null, true) or $this->load->view('name', $data, true)
could try and use null or true, return for tables.
Related
I have multiple divs , show/hide on button click div , inside one of these divs I have a table and to filter this table to search a specific user I use a PHP code and a submit form the problem is after submitting the form the page refreshes and the 1st div appears although the request worked and when I revisit the table div the filtering appears. I used this code to stop the refresh but the form is not being submitted:
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
$(function () {
$('#myform').on('submit', function (e) {
e.preventDefault();
$.ajax({
type: 'post',
url: 'admin.php',
data: $('#myform').serialize(),
success: function () {
alert('table is filtered');
}
});
});
});
</script>
and here is my form:
<div class="in5 n5" id="users">
<br>
<form method="post" action="" id="myform" >
<table style="width:25%;height: 13%;table-layout: fixed; font-family: signika;background-color:white;color:white;border-bottom:1px solid white;">
<tr>
<td width="30%" style="text-align:center;">
<button style="width:100%;height: 100%;border:0; font-size: 20px;font-family: signika;background: transparent;">Show all</button>
</td>
<td>
<div style="height:100%;width: 100%;">
<input type="text" name="valueToSearch" placeholder="Search user" style="height:100%;width: 70%; font-size: 20px;" >
<input value="Search" id="search" name="search" type="submit" style="width:30%; height: 100%; float: right; background-color: gray; color:white; font-size: 20px; ">
</div>
</td>
</tr>
</table>
<table style="width:100%;height:10%; background-color: white; font-size: 20px; border-bottom: 1px solid #cccccc; table-layout: fixed;border-collapse: collapse;">
<tr>
<td style="width:3%;">ID</td>
<td style="width:10%;">Username</td>
<td style="width:10%;">Email</td>
<td style="width:10%;">First name</td>
<td style="width:10%;">Last name</td>
<td style="width:5%;">Gender</td>
<td style="width:10%;">Birthday</td>
<td style="width:8%;">Country</td>
<td style="width:7%;">City</td>
<td style="width:10%;">Bio</td>
<td style="width:5%;">Access</td>
<td style="width:7%;">Picture</td>
<td style="width:5%;">Ban user</td>
</tr>
<?php while($row = mysqli_fetch_array($search_result)):?>
<tr >
<td style="width:3%; overflow: hidden;" title="<?php echo $row[0]; ?>">
<input style="border:0;background: transparent; font-size: 20px;" readonly="readonly" name="id" value="<?php echo $row[0]; ?>">
</td>
<td style="width:10%; overflow: hidden;" title="<?php echo $row[1]; ?>"><?php echo $row[1]; ?></td>
<td style="width:10%; overflow: hidden;" title="<?php echo $row[2]; ?>"><?php echo $row[2]; ?></td>
<td style="width:10%; overflow: hidden;"title="<?php echo $row[4]; ?>"><?php echo $row[4]; ?></td>
<td style="width:10%; overflow: hidden;"title="<?php echo $row[5]; ?>"><?php echo $row[5]; ?></td>
<td style="width:5%; overflow: hidden;"title="<?php echo $row[6]; ?>"><?php echo $row[6]; ?></td>
<td style="width:10%; overflow: hidden;"title="<?php echo $row[7]; ?>"><?php echo $row[7]; ?></td>
<td style="width:8%; overflow: hidden;"title="<?php echo $row[8]; ?>"><?php echo $row[8]; ?></td>
<td style="width:7%; overflow: hidden;"title="<?php echo $row[9]; ?>"><?php echo $row[9]; ?></td>
<td style="width:10%; overflow: hidden;"title="<?php echo $row[11]; ?>"><?php echo $row[11]; ?></td>
<td style="width:5%; overflow: hidden;">
<?php if($row['12']=="moderate"){ ?>
<a href="lock.php?id=<?php echo $row[0]; ?>">
<img class="img2" src="icons/access.png" ></a>
<?php } else{ ?>
<a href="access.php?id=<?php echo $row[0]; ?>">
<img class="img2" src="icons/locked.png" >
</a> <?php } ?>
</td>
<td style="width:7%; overflow: hidden;">
<a href="#image" title="See profile picture" class="cover" >
<?php if($row[10]==""){ ?>
<img class="cimg2 " src="images/profile.png"> <?php
} else { ?>
<img class="cimg2 " src="img/<?php echo $row[10] ?>" >
<?php }?>
</a>
</td>
<td style="width:5%; overflow: hidden;">
<a title="Delete user" href="deleteuser.php?id=<?php echo $row[0]; ?>" style="border:0;background: transparent;">
<img src="icons/ban.png"></a></td>
</tr>
<?php endwhile;?>
</table>
</form>
and the PHP code:
<?
if(isset($_POST['search']))
{
$valueToSearch = $_POST['valueToSearch'];
// search in all table columns
// using concat mysql function
$query = "SELECT * FROM `r` WHERE CONCAT(`id`, `username`,`email`) LIKE '%".$valueToSearch."%'";
$search_result = filterTable($query);
}
else {
$query = "SELECT * FROM `r`";
$search_result = filterTable($query);
}
// function to connect and execute the query
function filterTable($query)
{
$connect = mysqli_connect("localhost", "root", "", "x");
$filter_Result = mysqli_query($connect, $query);
return $filter_Result;
}
the problem is on submit the page is not refreshing and the alert appears but also the table is not filtering.
Use on click not on submit and input type button
$('#myform').on('click', function (e) {
Oops! You missed a concept also ! When using ajax you need to manipulate your DOM(Document Object Model) by yourself on callback functions
You are missing return false; at the end of submit event. The form submit event when customized and e.preventDefault() is used then your implementation overrides the default behavior thus it needs to tell the form if it needs to submit the whole page or not.
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
$(function () {
$('#myform').on('submit', function (e) {
e.preventDefault();
$.ajax({
type: 'post',
url: 'admin.php',
data: $('#myform').serialize(),
success: function () {
//filter the table using javascript code by changing the elements etc.
alert('table is filtered');
}
});
return false;
});
});
</script>
If this not works then there must be some uncertainty like there may be some **runtime error ** which is preventing your script to propagate.
im developing a project my own and i use codeigniter as my framework, i'm pretty begginer to php and codeignier. i created a confirm box in jquery ,but it's and html alert. so how to do that? don't laugh at me guys. thanks.
view
<?php
$_instance = get_instance();
$attributes = array('id' => 'main_form');
?>
<?php echo form_open('registration/reset',$attributes);?>
<style>
span{
color: red;
}
#main_tb{
margin-top:100px;
}
</style>
<table cellspacing='10' id>
</table>
<div id='main_form'>
<form id="main_form" name="form">
<table CELLSPACING=2 CELLSPACING=10 id='main_tb' >
<tr>
<td></td>
<td id="error"></td>
</tr>
<tr>
<td>User Name</td>
<td> <select class="cmb" style="width: 200px;" id="cmb_user" name="cmb_user"> </select></td>
</tr>
<tr>
<td></td>
<td><?php echo $this->session->flashdata('reset_error'); ?></td>
</tr>
<tr>
<td></td>
<td> <input type="submit" id="save" value="reset password"/></td>
</tr>
</table>
</form>
</div>
<?php echo form_close();?>
script
$j('#main_form').submit(function() {
// event.preventDefault();
var status = confirm("Are you sure? This cannot be undone");
if(status == false){
return false;
}
else{
return true;
}
});
controller
function reset(){
$password =$_POST['cmb_user'];
echo $username = $this->input->post('cmb_user');
$data_array = array(
'id_user' => $this->input->post('cmb_user'),
);
// // echo 'sdsd';
// echo $_POST['cmb_user'];
//
$this->load->model('registration/registration_model');
$this->registration_model->reset_pass($data_array);
$this->session->set_flashdata('reset_error', ' <br><span style="font-size: 10px;background-color: #FFFFFF;color:#ff0000;border:solid 1px #ff99cc;padding:2px;border-radius: 5px 5px 5px 5px">Reset Successfull</span>');
redirect('/registration/admin_reset');
}
this function works great. but i want to add jquery fancy popup like sweet alert. thank's again..
Add the following code into your view
<script src="https://cdn.rawgit.com/t4t5/sweetalert/master/dist/sweetalert.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.rawgit.com/t4t5/sweetalert/master/dist/sweetalert.css">
<script language="javascript">
sweetAlert("Oops...", "Something went wrong!", "error");
</script>
Hi i have been trying to update a specific record on my database. After getting it's id that's passed in url using "baseurl" so far I can pass the variable but cannot seem to update the record. Here's my controller class update_request_record.php which holds the function Update function that is used to fetch the entire record from the database, which the loads the Update_request_view_page.php which holds the form that will enable a user to update the required fields. so far I have encountered the problem of the form not displaying at all, please help.Thanks
<?php
class Update_Request_Record extends CI_Controller {
public function Update_Request_Record()
{
parent::__construct();
}
function Update()
{
$id = $this->uri->segment(3);
$this->load->database();
$query = $this->db->get_where('test_request', array('id' => $id));
return $query->result();
$this->load->view('update_request_view'.$query);
}
function Submit()
{
$this->load->model('update_requestmodel');
if($this->input->post('submit')){
$this->update_requestmodel->Update();
}
//redirect('home');
}
}
?>
Here's my Model
<?php
class Update_Requestmodel extends CI_Model {
// model constructor function
function __construct() {
parent::__construct(); // call parent constructor
$this->load->database();
}
function Update()
{
$new_data = array(
'applicant_name'=>$this->input->post('applicant_name'),
'applicant_address'=>$this->input->post('applicant_address')
);
$this->db->update('test_request', $new_data);
//redirect('home');
}
}
?>
This is from my list view page that passes the id of a record to the controller Update_request_record.php
<table class="list_header" bgcolor="#ffffff" border="0" width="960px" cellpadding="4px">
<?php foreach($query as $row): ?>
<tr>
<td style="border-right: dashed 1px #c0c0c0;text-align: center;border-bottom: solid 1px #c0c0c0;"><?php echo $row->id; ?>.</td>
<td style="text-align: left;border-bottom: solid 1px #c0c0c0;" ><?php echo $row->testing_reason;?></td>
<td style="text-align: left;border-bottom: solid 1px #c0c0c0;"><?php echo $row->applicant_name;?></td>
<td style="text-align: left;border-bottom: solid 1px #c0c0c0;"><?php echo $row->authorizer_name;?></td>
<td style="text-align: left;border-bottom: solid 1px #c0c0c0;"><?php echo $row->received_by;?></td>
<td style="text-align: left;border-bottom: solid 1px #c0c0c0;"><?php echo $row->test_required;?></td>
<td style="text-align: left;border-bottom: solid 1px #c0c0c0;"><?php echo $row->test_specification;?></td>
<td style="text-align: left;border-bottom: solid 1px #c0c0c0;"><?php echo $row->laboratory_number;?></td>
<td style="text-align: left;border-bottom: solid 1px #c0c0c0;"><?php echo $row->date;?></td>
<td style="text-align: left;border-bottom: solid 1px #c0c0c0;">Edit</td>
</tr>
<?php endforeach; ?>
</table>
Here's my form update_request_view.php
<div id="analysis_request" class="analysis_request" >
<?php echo validation_errors(); ?>
<?php echo form_open('update_request_record/Submit',array('name'=>'test_request_update_form'));?>
<table class="table_form" width="750px" height="500px" border="0" cellpadding="4px" align="center">
<tr>
<td colspan="7" align="center" style="padding:8px;border-bottom: solid 1px #bfbfbf;">
<b><h3>UPDATING TEST REQUEST FORM</h3></b>
</td>
</tr>
<tr>
<td align="center" style="border-right: dashed 1px #bfbfbf;"><b>1.</b></td>
<td colspan="2"><b>Name of Applicant</b></td>
<td colspan="4"><textarea rows="1" cols="50" name="applicant_name" value="<?php echo $query->applicant_name;?>"></textarea></td>
</tr>
<tr>
<td align="center" style="border-right: dashed 1px #bfbfbf;"></td>
<td colspan="2"><b>Address of Applicant</b></td>
<td colspan="4"><textarea rows="1" cols="50" name="applicant_address"></textarea></td>
</tr>
<tr>
<td style="border-top: dotted 1px #bfbfbf;text-align: center;" colspan="7" ><input type="submit" name="submit" value="Submit"></td>
</tr>
</table>
</form>
</div>
</div>
Post your $id from your form in an hidden input named id here, Then try this:
Add this line inside your form:
<?php echo form_hidden('id', $query->id) ?>
Then in your model do the following change:
$this->db->where('id', $this->input->post('id'));
$this->db->update('test_request', $new_data);
Just as a suggested improvement to your code to make it slightly more DRY, you could throw this into your model:
$column_mapping = array(
'db_col1' => 'input_field1'
//more columns/fields
);
foreach ($column_mapping as $k => $v) {
$new_data[$k] = $this->input->post($v);
}
Personally, I see a whole bunch of repeated code such as 'something' => $this->input->post('something'); and think "AHHHH!".
This is my code; I will tell you the issue then.
<?php
//checking for perfect loging
session_start();
$user_name = $_SESSION['username'];
$user_pass = $_SESSION['password'];
require ("connection.php");
if ( $user_name == '' ) {
header('location:home.php');
exit();
}
/////////////////////////////////////
?>
<?php
//collecting posted variables by pressing addanother button
if(isset($_POST['adnother'])) {
$date = $_POST['date'];
$billnmbr = $_POST['billnmbr'];
$itemcode = $_POST['itemcode'];
$itemname = $_POST['itemname'];
$exdate = $_POST['exdate'];
$eachprice = $_POST['eachprice'];
$itmtotal = $_POST['itmtotal'];
$wasFound=false;
$i=0;
// check for >>>>>>>>>>> if the session bill_array array is not set or cart array is empty <<<<<<<<<<<<<<<
if(!isset($_SESSION["bill_array"]) || count($_SESSION["bill_array"]) < 1 ) {
// Run if the bill_array is empty or not set
$_SESSION["bill_array"]= array(1 => array("date"=> $date, "billnmbr"=> $billnmbr, "itemcode"=> $itemcode, "itemname"=> $itemname, "exdate"=> $exdate, "eachprice"=> $eachprice, "itmtotal"=> $itmtotal));
} else {
// Run if the bill has at least one item in it
foreach($_SESSION["bill_array"] as $each_item) {
$i++;
while(list($key,$value)=each($each_item)){
if($key=="itemcode" && $value == $itemcode){
// That item is in cart already so push a error message in to screen
$wasFound = true;
?>
<script type="text/javascript">
var error = "<?= $wasFound ?>";
if(error == "true") {
alert("You trying to add same item twice")
}
</script>
<?php
}//close if condition
}//close while loop
}//close foreach loop
//if the next item is not in the bill and then add it to the bill_array
if($wasFound==false){
array_push($_SESSION["bill_array"],array("date"=> $date, "billnmbr"=> $billnmbr, "itemcode"=> $itemcode, "itemname"=> $itemname, "exdate"=> $exdate, "eachprice"=> $eachprice, "itmtotal"=> $itmtotal));
}//clos if condition
}//close else statment
}//close ifisset
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Front-end Billing</title>
<link href="main.css" rel="stylesheet" type="text/css" media="all" />
<style type="text/css">
.mainheder {
font-size: 36px;
font-weight: bolder;
color: #00C;
text-align: center;
}
#headertopic {
position:absolute;
left:304px;
top:1px;
width:395px;
height:44px;
z-index:1;
background-color: #999900;
}
#mainmenue {
position:absolute;
left:208px;
top:78px;
width:614px;
height:48px;
z-index:10000;
}
#dateandtime {
position:absolute;
left:790px;
top:251px;
width:208px;
height:82px;
z-index:1;
}
#redcross {
position:absolute;
left:377px;
top:321px;
width:247px;
height:239px;
z-index:0;
}
#usrlogin {
position:absolute;
left:4px;
top:216px;
width:265px;
height:184px;
z-index:1;
}
#address {
position:absolute;
left:2px;
top:462px;
width:204px;
height:155px;
z-index:2;
}
#logdas {
position:absolute;
left:10px;
top:92px;
width:197px;
height:58px;
z-index:1;
}
.logdas {
font-weight: bold;
color: #00F;
}
#pagetheam {
position:absolute;
left:332px;
top:49px;
width:341px;
height:24px;
z-index:1;
text-align: center;
font-size: 18px;
font-weight: bolder;
color: #0CF;
text-decoration: underline;
}
#billingitems {
position:absolute;
left:11px;
top:186px;
width:489px;
height:293px;
z-index:1;
}
#printlayout {
position:absolute;
left:531px;
top:186px;
width:211px;
height:322px;
z-index:1;
text-align: center;
}
.printlayoutshopname {
font-size: 14px;
font-weight: bold;
color: #000;
}
.billaddrs {
font-size: 10px;
}
.bilnmbr {
font-size: 10px;
font-weight: bold;
text-align: left;
}
</style></head>
<body>
<div id="wrap">
<div id="headertopic" class="mainheder">Accoutnig for Phamacy</div>
<p> </p>
<p> </p>
<div id="mainmenue"> <?php // include 'index.html' ;?> </div>
<div id="logdas"><p class="logdas">Logged in as : <?php echo $user_name; ?></p>
<p class="logdas">Date : <?php echo date("Y-m-d") ?></p>
</div>
<div id="pagetheam">Front-end Billing </div>
<div id="billingitems">
<form id="form1" name="form1" method="post" action="biling.php">
<?php $data = mysql_query("SELECT * FROM billnumber ") ;
$info = mysql_fetch_array( $data );
$oldnumber = $info['bill_number'];
$oldnumber= $oldnumber + 1;
$Transaction_number = "PM". $oldnumber ;
?>
<table width="490" height="282" border="0">
<tr>
<td width="160"><input type="hidden" name="date" id="date" value=" <?php echo date("Y-m-d") ?> " /><?php echo date("Y-m-d") ?></td>
<td width="47"> </td>
<td width="69"> </td>
</tr>
<tr>
<td width="206">Bill Number</td>
<td width="160"><input type="hidden" name="billnmbr" id="billnmbr" value="<?php echo $Transaction_number; ?>" /><?php echo $Transaction_number; ?></td>
<td width="47"> </td>
<td width="69"> </td>
</tr>
<tr>
<td>Item Code</td>
<td><input type="text" name="itemcode" id="itemcode" tabindex="1" /></td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Item Name</td>
<td><input type="text" name="itemname" id="itemname" tabindex="2" /></td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Expier Date</td>
<td><input type="text" name="exdate" id="exdate" /></td>
<td> </td>
<td> </td>
</tr>
<tr>
<td>Item Price</td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td>Each :
<input type="text" name="eachprice" id="eachprice" /></td>
<td>Total :
<input type="text" name="itmtotal" id="itmtotal" /></td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="adnother" id="adnother" value="add another item" tabindex="5" /></td>
<td><input type="submit" name="submit" id="submit" value="Submit" /></td>
<td> </td>
</tr>
</table>
</form>
</div>
<div id="printlayout">
<p><span class="printlayoutshopname">Yasitha Pharacy<br />
</span><span class="billaddrs">22,Colombathanthiri Mawatha, Ethulkotte, Kotte.</span></p>
<table width="211" border="0">
<tr>
<td width="103"><span class="bilnmbr">Bill No: <?php echo $Transaction_number; ?></span></td>
<td colspan="2" class="bilnmbr">Date: <?php echo date("Y-m-d") ?></td>
</tr>
<?php
if(isset($_POST['adnother'])) {
//taking items in the bill to variables if the bill_array is not empty.
$cartOutput="";
if(!isset($_SESSION["bill_array"]) || count($_SESSION["bill_array"]) < 1 ) {
$cartOutput = "<h2 align='center'> Bill is still empty </hd>";
}
else {
$i = 0;
foreach ($_SESSION["bill_array"]as $each_item ): ?>
<tr>
<td class="bilnmbr">Item code: </td>
<td colspan="2" class="bilnmbr"><?php echo $each_item['itemcode']; ?></td>
</tr>
<tr>
<td colspan="3" class="bilnmbr">Item Name:</td>
</tr>
<tr>
<td colspan="3" class="bilnmbr"><?php echo $each_item['itemname']; ?></td>
</tr>
<tr class="bilnmbr">
<td>Each</td>
<td width="98">Qty</td>
<td width="98">Total</td>
</tr>
<tr class="bilnmbr">
<td><?php echo $each_item['eachprice']; ?></td>
<td>20</td>
<td><?php echo $each_item['itmtotal']; ?></td>
</tr>
</table>
<?php endforeach; ?>
<?php
}
}
////////////////////////////////////////
?>
<p>
</p>
</div>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<?php //closing div for wrapper?>
</div>
</body>
</html>
I used two dimensional array for insert item details to a session array, after inserting a item I want its details to be displayed in a separate table (right hand side) there is no problem with one item, when I add a item it is viewing in the perfect place, but when I add the second item it is going under that table without creating new row. What is the wrong with what I had done?
I found my error
i ended my foreach loop like this
</tr>
</table>
<?php endforeach;
}
}
////////////////////////////////////////
?>
but now i corrected to this way
</tr>
<?php endforeach;
}
}
////////////////////////////////////////
?>
</table>
now it's all ok.... thank you guys for wasting your time for my silly mistake... sorry.... :(
I am trying to connect with social web sites like facebook,twitter,linkedin etc using Hybridauth
This is what I do to connect with social web sites. I am new to PHP just started to learn and doing this.
index.php
<?php
# start a new PHP session
session_start();
// we need to know it
$CURRENT_URL = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
// print "CURRENT_URL $CURRENT_URL";
// change the following paths if necessary
$config = dirname(__FILE__) . '/hybridauth/config.php';
require_once( "hybridauth/Hybrid/Auth.php" );
try{
$hybridauth = new Hybrid_Auth( $config );
}
catch( Exception $e ){
echo "Ooophs, we got an error: " . $e->getMessage();
}
$provider = "";
// handle logout request
if( isset( $_GET["logout"] ) ){
$provider = $_GET["logout"];
$adapter = $hybridauth->getAdapter( $provider );
$adapter->logout();
header( "Location: index.php" );
die();
}
// if the user select a provider and authenticate with it
// then the widget will return this provider name in "connected_with" argument
elseif( isset( $_GET["connected_with"] ) && $hybridauth->isConnectedWith( $_GET["connected_with"] ) ){
$provider = $_GET["connected_with"];
$adapter = $hybridauth->getAdapter( $provider );
$user_data = $adapter->getUserProfile();
// include authenticated user view
include "authenticated_user.php";
die();
} // if user connected to the selected provider
// if not, include unauthenticated user view
include "unauthenticated_user.php";
authenticated_user.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="en">
<head>
<style type="text/css" media="screen">
<!--
BODY {
margin: 10px;
padding: 0;
}
H1 {
margin-bottom: 2px;
font-family: Garamond, "Times New Roman", Times, Serif;
}
FIELDSET {
border: 1px solid #ccc;
padding: 1em;
margin: 0;
}
LEGEND {
color: #666666;
}
-->
</style>
</head>
<body>
<br /><br />
<center>
<h1>Welcome <?php echo $user_data->displayName; ?></h1>
<br />
<br />
<table width="600" border="0" cellpadding="2" cellspacing="2">
<tr>
<td valign="top"><fieldset>
<legend>Connected user badge</legend>
<table width="100%" border="0" cellpadding="2" cellspacing="2">
<tr>
<td width="100" rowspan="5">
<?php
if( $user_data->photoURL ){
?>
<img src="<?php echo $user_data->photoURL; ?>" title="<?php echo $user_data->displayName; ?>" border="0" width="100" height="120">
<?php
}
else{
?>
<img src="avatar.png" title="<?php echo $user_data->displayName; ?>" border="0" >
<?php
}
?>
</td>
<td width="60" nowrap><div align="right"><strong>Provider</strong></div></td>
<td align="left" ><?php echo $adapter->id; ?></td>
</tr>
<tr>
<td nowrap><div align="right"><strong>Identifier</strong></div></td>
<td align="left"><?php echo $user_data->identifier; ?></td>
</tr>
<tr>
<td nowrap><div align="right"><strong>Dispaly name</strong></div></td>
<td align="left"><?php echo $user_data->displayName; ?></td>
</tr>
<tr>
<td nowrap><div align="right"><strong>Email</strong></div></td>
<td align="left"><?php
if( $user_data->email ){
echo $user_data->email ;
}
else{
echo "not provided by " . $adapter->id ;
}
?></td>
</tr>
<tr>
<td nowrap valign="top"><div align="right"><strong>Profile URL</strong></div></td>
<td align="left" valign="top"><?php echo $user_data->profileURL; ?></td>
</tr>
<tr>
<td align="right"><strong>Update Status</strong></td>
<td align="center"><input name="updatestatus" id="updatestatus" /></td>
</tr>
</table>
</fieldset></td>
</tr>
<tr>
<td valign="top" align="right">
<img src="arrow.gif" align="texttop" style="margin-top:-5px;" >
Log me out
</td>
</tr>
</table>
</center>
</body>
</html>
unathenticated.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="en">
<head>
<style type="text/css" media="screen">
<!--
BODY {
margin: 10px;
padding: 0;
}
H1 {
margin-bottom: 2px;
font-family: Garamond, "Times New Roman", Times, Serif;
}
FIELDSET {
border: 1px solid #ccc;
padding: 1em;
margin: 0;
}
LEGEND {
color: #666666;
}
INPUT {
display: block;
font-family: Arial, verdana;
padding: 7px;
border: 1px solid #999;
margin: 10px;
}
/* Gradient 2 */
.textbox{
background-image: url(images/bg.png);
border: 1px solid #6297BC;
width: 230px;
}
-->
</style>
</head>
<body>
<br /><br />
<center>
<h1>Social web sites integration</h1>
<br />
<br />
<table width="380" border="0" cellpadding="2" cellspacing="2">
<tr>
<td valign="top"><fieldset>
<legend>Sign-in form</legend>
<table width="00%" border="0" cellpadding="2" cellspacing="2">
<tr>
<td><div align="right"><strong>login</strong></div></td>
<td><input type="text" name="textfield" id="textfield" class="textbox"/></td>
</tr>
<tr>
<td><div align="right"><strong>password</strong></div></td>
<td><input type="text" name="textfield2" id="textfield2" class="textbox"/></td>
</tr>
<tr>
<td> </td>
<td><div align="center">
<input type="submit" value="Sign-in" />
</div></td>
</tr>
</table>
</fieldset></td>
</tr>
<tr>
<td valign="top" align="right">
<!--img src="arrow.gif" align="texttop" style="margin-top:-5px;" -->
<!-- CODE REQUIRED BY THE WIDGET -->
<link media="screen" rel="stylesheet" href="widget/css/colorbox.css" />
<script src="widget/js/jquery.min.js"></script>
<script src="widget/js/jquery.colorbox.js"></script>
<script>
$(document).ready(function(){
$(".widget_link").colorbox({iframe:true, innerWidth:430, innerHeight:222});
});
</script>
<!-- /WIDGET -->
<!--
LINK TO THE WIDGET
return_to: call back this page after authenticatin the user
ts: nocache
-->
Or sign-in with Social web sites
</td>
</tr>
</table>
<br /><br /><br />
<!--b>Note:</b> This is just a proof of concept! it works good enough to try out on Firefox or on Chrome.-->
</center>
</body>
</html>
So please help me how to go about this?, Here I do display user information like first name and last name and picture url. I want to add another text box which will enable to update user status or post wall.
From the User Guide: Update User Status
This method exists:
$adapter->setUserStatus("");
Note From User Guide:
Currently only Facebook, Twitter, Identica, LinkedIn, QQ, Sina, Murmur, Pixnet and Plurk support this feature
You could create a form on authenticated_user.php and add a text field with ID "status_update". When it's submitted, you could check for it and run the setUserStatus method.
// if the user select a provider and authenticate with it
// then the widget will return this provider name in "connected_with" argument
elseif( isset( $_GET["connected_with"] ) && $hybridauth->isConnectedWith( $_GET["connected_with"] ) ){
$provider = $_GET["connected_with"];
$adapter = $hybridauth->getAdapter( $provider );
// *****************************************************
// NEW CODE TO UPDATE USER STATUS
if( isset( $_GET["status_update"] ) && !empty( $_GET["status_update"] ) ) {
$adapter->setUserStatus( $_GET["status_update"] );
}
$user_data = $adapter->getUserProfile();
// include authenticated user view
include "authenticated_user.php";
die();
} // if user connected to the selected provider