Log in

View Full Version : seperating client side from sever side



mutago
05-27-2014, 05:00 PM
Am testing login via json php using mysql deprecated.
This code works excellent when all is runing on the sever side.

now i have seperated the client side from the server side.
The server side which is login.php is hosted on my web hosting account
login.config


<?php
class db{
function connect(){
mysql_connect("localhost","root","");
mysql_select_db("log_db");
}
}
?>




<?php
session_start();
require 'mdb.php'; // link to your database class
$db = new db;
$json = array();
if(isset($_REQUEST['email']) && isset($_REQUEST['password'])){
$email = $_REQUEST['email'];
$password = $_REQUEST['password'];
$db->connect();
$sql = sprintf("SELECT id FROM members WHERE email='%s' AND password='%s'",mysql_real_escape_string($email),$password);
$sql = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($sql)){
$uid = mysql_result($sql,0);
$_SESSION['id'] = $uid;
$json['status'] = true;
$json['message']= "Logging in...";
}else{
$json['status'] = false;
$json['message']= "Email and password did not match.";
}
}
echo json_encode($json);
?>




while my client side is stiil on my local system.
json.js


$(function(){
$('#login').submit(function(){
$.ajax({
url: 'http://example.com/login.php',
data: $(this).serialize(),
dataType: 'json',
beforeSend: function(){$('button').after('<img src="throbber.gif" id="throbber"/>');},
success: function(response){
$('#throbber').fadeOut();
if(response.status){
$.when(function(){
$('#responseText').css({'border':'1px solid #A2D246','background':'#EBF8A4'}).html(response.message).fadeIn('fast');
}).then(function(){
window.location.href='myaccount.php';
});
}else{
$('#responseText').css({'border':'1px solid #F69','background':'#FCC'}).text(response.message).fadeIn('fast');
}
}
});
return false;
});
});




<html><head>


<script src="json.js">
</script>
</head><body>


<div id="responseText" style="padding:5px;display:none;word-wrap:break-word;margin:0 0 5px 0;"></div>

<form action="" method="post" id="login">
<table>
<thead>
<tr><th colspan="2">Customer Login</th></tr>
</thead>
<tbody>
<tr><td align="right">Email:</td><td><input type="text" name="email" /></td></tr>
<tr><td align="right">Password:</td><td><input type="password" name="password" /></td></tr>
<tr><td> </td><td><button type="submit">Login</button></td></tr>
</tbody>
</table>
</form>
</body></html>


Am trying to use the client side which is on the local system with me to connect to login.php which on the internet on my hosting account but its not working. it seems i cannot connect to login.php

what is wrong. Can't i use json this way

jscheuer1
05-28-2014, 12:43 PM
Same origin policy. You cannot use AJAX this way:


$(function(){
$('#login').submit(function(){
$.ajax({
url: 'http://example.com/login.php',

unless that (above) code is on http://example.com

But you seem to indicate that it's on the localhost. If so, it will be access denied for an AJAX call to http://example.com.